Skip to content

Commit

Permalink
refactor: removed rather redundant flag reporters zset
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jul 31, 2020
1 parent 0f2b6f1 commit 6c00ec8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/flags.js
Expand Up @@ -363,27 +363,30 @@ Flags.create = async function (type, id, uid, reason, timestamp) {
};

Flags.getReports = async function (flagId) {
const [reports, reporterUids] = await Promise.all([
db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1),
db.getSortedSetRevRange(`flag:${flagId}:reporters`, 0, -1),
]);
const payload = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1);
const [reports, uids] = payload.reduce(function (memo, cur) {
const value = cur.value.split(';');
memo[1].push(value.shift());
cur.value = value.join(';');
memo[0].push(cur);

return memo;
}, [[], []]);

await Promise.all(reports.map(async (report, idx) => {
report.timestamp = report.score;
report.timestampISO = new Date(report.score).toISOString();
delete report.score;
report.reporter = await user.getUserFields(reporterUids[idx], ['username', 'userslug', 'picture', 'reputation']);
report.reporter = await user.getUserFields(uids[idx], ['username', 'userslug', 'picture', 'reputation']);
}));

return reports;
};

Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) {
// adds to reporters/report zsets
await db.sortedSetAddBulk([
[`flags:byReporter:${uid}`, timestamp, flagId],
[`flag:${flagId}:reports`, timestamp, reason],
[`flag:${flagId}:reporters`, timestamp, uid],
[`flag:${flagId}:reports`, timestamp, [uid, reason].join(';')],

['flags:hash', flagId, [type, id, uid].join(':')],
]);
Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions src/upgrades/1.15.0/remove_flag_reporters_zset.js
@@ -0,0 +1,33 @@
'use strict';

const db = require('../../database');
const batch = require('../../batch');

module.exports = {
name: 'Remove flag reporters sorted set',
timestamp: Date.UTC(2020, 6, 31),
method: async function () {
const progress = this.progress;
progress.total = await db.sortedSetCard('flags:datetime');

await batch.processSortedSet('flags:datetime', async (flagIds) => {
await Promise.all(flagIds.map(async (flagId) => {
const [reports, reporterUids] = await Promise.all([
db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1),
db.getSortedSetRevRange(`flag:${flagId}:reporters`, 0, -1),
]);

const values = reports.reduce((memo, cur, idx) => {
memo.push([`flag:${flagId}:reports`, cur.score, [(reporterUids[idx] || 0), cur.value].join(';')]);
return memo;
}, []);

await db.delete(`flag:${flagId}:reports`);
await db.sortedSetAddBulk(values);
}));
}, {
batch: 500,
progress: progress,
});
},
};

0 comments on commit 6c00ec8

Please sign in to comment.