Revert "refactor: use Map to track sorted lists in Settings.set()"

This reverts commit 65de2e76b4.
v1.18.x
Julian Lam 4 years ago
parent 65de2e76b4
commit eb96046e97

@ -44,36 +44,37 @@ Settings.getOne = async function (hash, field) {
Settings.set = async function (hash, values, quiet) { Settings.set = async function (hash, values, quiet) {
quiet = quiet || false; quiet = quiet || false;
const sortedLists = new Map(); const sortedLists = [];
for (const key in values) { for (const key in values) {
if (values.hasOwnProperty(key)) { if (values.hasOwnProperty(key)) {
if (Array.isArray(values[key]) && typeof values[key][0] !== 'string') { if (Array.isArray(values[key]) && typeof values[key][0] !== 'string') {
sortedLists.set(key, values[key]); sortedLists.push(key);
delete values[key]; // don't save in primary settings hash
} }
} }
} }
const sortedListKeys = Array.from(sortedLists.keys());
({ plugin: hash, settings: values, quiet } = await plugins.hooks.fire('filter:settings.set', { plugin: hash, settings: values, quiet })); ({ plugin: hash, settings: values, quiet } = await plugins.hooks.fire('filter:settings.set', { plugin: hash, settings: values, quiet }));
if (sortedLists.size) { if (sortedLists.length) {
await db.delete('settings:' + hash + ':sorted-lists'); await db.delete('settings:' + hash + ':sorted-lists');
await db.setAdd('settings:' + hash + ':sorted-lists', sortedListKeys); await db.setAdd('settings:' + hash + ':sorted-lists', sortedLists);
await Promise.all(sortedListKeys.map(async function (list) { await Promise.all(sortedLists.map(async function (list) {
await db.delete('settings:' + hash + ':sorted-list:' + list); await db.delete('settings:' + hash + ':sorted-list:' + list);
await Promise.all(sortedLists.get(list).map(async function (data, order) { await Promise.all(values[list].map(async function (data, order) {
await db.delete('settings:' + hash + ':sorted-list:' + list + ':' + order); await db.delete('settings:' + hash + ':sorted-list:' + list + ':' + order);
})); }));
})); }));
const ops = []; const ops = [];
sortedLists.forEach(function (list) { sortedLists.forEach(function (list) {
list.forEach(function (data, key) { const arr = values[list];
const order = sortedListKeys.indexOf(key); delete values[list];
ops.push(db.sortedSetAdd('settings:' + hash + ':sorted-list:' + key, order, order));
ops.push(db.setObject('settings:' + hash + ':sorted-list:' + key + ':' + order, data)); arr.forEach(function (data, order) {
ops.push(db.sortedSetAdd('settings:' + hash + ':sorted-list:' + list, order, order));
ops.push(db.setObject('settings:' + hash + ':sorted-list:' + list + ':' + order, data));
}); });
}); });
@ -84,9 +85,6 @@ Settings.set = async function (hash, values, quiet) {
await db.setObject('settings:' + hash, values); await db.setObject('settings:' + hash, values);
} }
// Add back sorted list data
values = { ...values, ...Object.fromEntries(sortedLists) };
cache.del('settings:' + hash); cache.del('settings:' + hash);
plugins.hooks.fire('action:settings.set', { plugins.hooks.fire('action:settings.set', {

Loading…
Cancel
Save