mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-01 21:30:30 +01:00
refactor: clone settings before returning
prevents plugins from mistakenly modifying saved settings in cache
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const db = require('../database');
|
||||
const plugins = require('../plugins');
|
||||
const Meta = require('./index');
|
||||
@@ -11,29 +13,31 @@ const Settings = module.exports;
|
||||
Settings.get = async function (hash) {
|
||||
const cached = cache.get(`settings:${hash}`);
|
||||
if (cached) {
|
||||
return cached;
|
||||
return _.cloneDeep(cached);
|
||||
}
|
||||
let data = await db.getObject(`settings:${hash}`) || {};
|
||||
const sortedLists = await db.getSetMembers(`settings:${hash}:sorted-lists`);
|
||||
|
||||
const [data, sortedLists] = await Promise.all([
|
||||
db.getObject(`settings:${hash}`),
|
||||
db.getSetMembers(`settings:${hash}:sorted-lists`),
|
||||
]);
|
||||
const values = data || {};
|
||||
await Promise.all(sortedLists.map(async (list) => {
|
||||
const members = await db.getSortedSetRange(`settings:${hash}:sorted-list:${list}`, 0, -1) || [];
|
||||
const keys = [];
|
||||
|
||||
data[list] = [];
|
||||
values[list] = [];
|
||||
for (const order of members) {
|
||||
keys.push(`settings:${hash}:sorted-list:${list}:${order}`);
|
||||
}
|
||||
|
||||
const objects = await db.getObjects(keys);
|
||||
objects.forEach((obj) => {
|
||||
data[list].push(obj);
|
||||
values[list].push(obj);
|
||||
});
|
||||
}));
|
||||
|
||||
({ values: data } = await plugins.hooks.fire('filter:settings.get', { plugin: hash, values: data }));
|
||||
cache.set(`settings:${hash}`, data);
|
||||
return data;
|
||||
const result = await plugins.hooks.fire('filter:settings.get', { plugin: hash, values: values });
|
||||
cache.set(`settings:${hash}`, result.values);
|
||||
return _.cloneDeep(result.values);
|
||||
};
|
||||
|
||||
Settings.getOne = async function (hash, field) {
|
||||
|
||||
Reference in New Issue
Block a user