From 1603566bcc264c4145adbe3f577144ee11a55cbb Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 25 Jan 2021 11:29:00 -0500 Subject: [PATCH] fix: filtering logic of flags [breaking] When combining filters, the old logic assumed that every filter was exclusive, unless that filter contained multiple items, in which case it was added to a list of "or" filters that returned all matching flags. A fault was discovered in that if you passed in multiple "or" states, it did not return flags with the expected filtering. e.g. open flags, closed flags, flags of cid 1, flags of cid 2 This could return open flags of cid 3, since all of the filters were "OR"'d. This logic change updates the behaviour so disparate OR sets are intersected (ANDed). --- src/flags.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/flags.js b/src/flags.js index 444384c379..1799b379d8 100644 --- a/src/flags.js +++ b/src/flags.js @@ -39,7 +39,7 @@ Flags.init = async function () { if (value.length === 1) { sets.push(prefix + value[0]); } else { - value.forEach(x => orSets.push(prefix + x)); + orSets.push(value.map(x => prefix + x)); } } } @@ -154,7 +154,12 @@ Flags.getFlagIdsWithFilters = async function ({ filters, uid }) { } if (orSets.length) { - const _flagIds = await db.getSortedSetRevUnion({ sets: orSets, start: 0, stop: -1, aggregate: 'MAX' }); + let _flagIds = await Promise.all(orSets.map(async orSet => await db.getSortedSetRevUnion({ sets: orSet, start: 0, stop: -1, aggregate: 'MAX' }))); + + // Each individual orSet is ANDed together to construct the final list of flagIds + _flagIds = _.intersection(..._flagIds); + + // Merge with flagIds returned by sets if (sets.length) { // If flag ids are already present, return a subset of flags that are in both sets flagIds = _.intersection(flagIds, _flagIds);