From 3b529b84b5affa01aca5963f41d39a7585b2b5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 23 Mar 2022 15:10:10 -0400 Subject: [PATCH] feat: add flags.purge --- src/flags.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/flags.js b/src/flags.js index 43e5927c2d..b93902fa63 100644 --- a/src/flags.js +++ b/src/flags.js @@ -463,6 +463,46 @@ Flags.create = async function (type, id, uid, reason, timestamp, forceFlag = fal return flagObj; }; +Flags.purge = async function (flagIds) { + const flagData = (await db.getObjects(flagIds.map(flagId => `flag:${flagId}`))).filter(Boolean); + const postFlags = flagData.filter(flagObj => flagObj.type === 'post'); + const userFlags = flagData.filter(flagObj => flagObj.type === 'user'); + const assignedFlags = flagData.filter(flagObj => !!flagObj.assignee); + + const allReports = await db.getSortedSetsMembers(flagData.map(flagObj => `flag:${flagObj.flagId}:reports`)); + const allReporterUids = allReports.map(flagReports => flagReports.map(report => report && report.split(';')[0])); + const removeReporters = []; + flagData.forEach((flagObj, i) => { + if (Array.isArray(allReporterUids[i])) { + allReporterUids[i].forEach((uid) => { + removeReporters.push([`flags:hash`, [flagObj.type, flagObj.targetId, uid].join(':')]); + removeReporters.push([`flags:byReporter:${uid}`, flagObj.flagId]); + }); + } + }); + await Promise.all([ + db.sortedSetRemoveBulk([ + ...flagData.map(flagObj => ([`flags:byType:${flagObj.type}`, flagObj.flagId])), + ...flagData.map(flagObj => ([`flags:byState:${flagObj.state}`, flagObj.flagId])), + ...removeReporters, + ...postFlags.map(flagObj => ([`flags:byCid:${flagObj.targetCid}`, flagObj.flagId])), + ...postFlags.map(flagObj => ([`flags:byPid:${flagObj.targetId}`, flagObj.flagId])), + ...assignedFlags.map(flagObj => ([`flags:byAssignee:${flagObj.assignee}`, flagObj.flagId])), + ...userFlags.map(flagObj => ([`flags:byTargetUid:${flagObj.targetUid}`, flagObj.flagId])), + ]), + db.deleteAll([ + ...flagIds.map(flagId => `flag:${flagId}`), + ...flagIds.map(flagId => `flag:${flagId}:notes`), + ...flagIds.map(flagId => `flag:${flagId}:reports`), + ]), + db.sortedSetRemove('flags:datetime', flagIds), + db.sortedSetRemove( + 'flags:byTarget', + flagData.map(flagObj => [flagObj.type, flagObj.targetId].join(':')) + ), + ]); +}; + Flags.getReports = async function (flagId) { const payload = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1); const [reports, uids] = payload.reduce((memo, cur) => {