mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-08 07:40:05 +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';
|
'use strict';
|
||||||
|
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
const Meta = require('./index');
|
const Meta = require('./index');
|
||||||
@@ -11,29 +13,31 @@ const Settings = module.exports;
|
|||||||
Settings.get = async function (hash) {
|
Settings.get = async function (hash) {
|
||||||
const cached = cache.get(`settings:${hash}`);
|
const cached = cache.get(`settings:${hash}`);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return cached;
|
return _.cloneDeep(cached);
|
||||||
}
|
}
|
||||||
let data = await db.getObject(`settings:${hash}`) || {};
|
const [data, sortedLists] = await Promise.all([
|
||||||
const sortedLists = await db.getSetMembers(`settings:${hash}:sorted-lists`);
|
db.getObject(`settings:${hash}`),
|
||||||
|
db.getSetMembers(`settings:${hash}:sorted-lists`),
|
||||||
|
]);
|
||||||
|
const values = data || {};
|
||||||
await Promise.all(sortedLists.map(async (list) => {
|
await Promise.all(sortedLists.map(async (list) => {
|
||||||
const members = await db.getSortedSetRange(`settings:${hash}:sorted-list:${list}`, 0, -1) || [];
|
const members = await db.getSortedSetRange(`settings:${hash}:sorted-list:${list}`, 0, -1) || [];
|
||||||
const keys = [];
|
const keys = [];
|
||||||
|
|
||||||
data[list] = [];
|
values[list] = [];
|
||||||
for (const order of members) {
|
for (const order of members) {
|
||||||
keys.push(`settings:${hash}:sorted-list:${list}:${order}`);
|
keys.push(`settings:${hash}:sorted-list:${list}:${order}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const objects = await db.getObjects(keys);
|
const objects = await db.getObjects(keys);
|
||||||
objects.forEach((obj) => {
|
objects.forEach((obj) => {
|
||||||
data[list].push(obj);
|
values[list].push(obj);
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
({ values: data } = await plugins.hooks.fire('filter:settings.get', { plugin: hash, values: data }));
|
const result = await plugins.hooks.fire('filter:settings.get', { plugin: hash, values: values });
|
||||||
cache.set(`settings:${hash}`, data);
|
cache.set(`settings:${hash}`, result.values);
|
||||||
return data;
|
return _.cloneDeep(result.values);
|
||||||
};
|
};
|
||||||
|
|
||||||
Settings.getOne = async function (hash, field) {
|
Settings.getOne = async function (hash, field) {
|
||||||
|
|||||||
Reference in New Issue
Block a user