From d5d24594e8b9d04effddbf87432081a668b2b1d5 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 1 Feb 2021 11:45:00 -0500 Subject: [PATCH] feat: allow sorted-lists on multiple pages If multiple sorted-lists were on separate pages, saving one page would erase the sorted-lists saved on the other page. This was caused by naive deletion of the sorted-lists index on settings save. At the same time, a bug was found where if fewer items were passed in, only that many items were removed from the database, leaving leftover orphan data in the database. The logic now: - Only removes sorted-lists if they are passed in (and empty) - Deletes all sorted list items, not just the items passed in. --- src/meta/settings.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/meta/settings.js b/src/meta/settings.js index 70c37c9855..30473bd7ac 100644 --- a/src/meta/settings.js +++ b/src/meta/settings.js @@ -58,14 +58,18 @@ Settings.set = async function (hash, values, quiet) { const sortedLists = Object.keys(sortedListData); if (sortedLists.length) { - await db.delete('settings:' + hash + ':sorted-lists'); await db.setAdd('settings:' + hash + ':sorted-lists', sortedLists); + // Remove provided (but empty) sorted lists from the hash set + await db.setRemove('settings:' + hash + ':sorted-lists', sortedLists.filter(list => !sortedListData[list].length)); + await Promise.all(sortedLists.map(async function (list) { - await db.delete('settings:' + hash + ':sorted-list:' + list); - await Promise.all(sortedListData[list].map(async function (data, order) { - await db.delete('settings:' + hash + ':sorted-list:' + list + ':' + order); - })); + const numItems = await db.sortedSetCard('settings:' + hash + ':sorted-list:' + list); + const deleteKeys = ['settings:' + hash + ':sorted-list:' + list]; + for (let x = 0; x < numItems; x++) { + deleteKeys.push('settings:' + hash + ':sorted-list:' + list + ':' + x); + } + await db.deleteAll(deleteKeys); })); const ops = [];