mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: removed rather redundant flag reporters zset
This commit is contained in:
19
src/flags.js
19
src/flags.js
@@ -363,27 +363,30 @@ Flags.create = async function (type, id, uid, reason, timestamp) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Flags.getReports = async function (flagId) {
|
Flags.getReports = async function (flagId) {
|
||||||
const [reports, reporterUids] = await Promise.all([
|
const payload = await db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1);
|
||||||
db.getSortedSetRevRangeWithScores(`flag:${flagId}:reports`, 0, -1),
|
const [reports, uids] = payload.reduce(function (memo, cur) {
|
||||||
db.getSortedSetRevRange(`flag:${flagId}:reporters`, 0, -1),
|
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) => {
|
await Promise.all(reports.map(async (report, idx) => {
|
||||||
report.timestamp = report.score;
|
report.timestamp = report.score;
|
||||||
report.timestampISO = new Date(report.score).toISOString();
|
report.timestampISO = new Date(report.score).toISOString();
|
||||||
delete report.score;
|
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;
|
return reports;
|
||||||
};
|
};
|
||||||
|
|
||||||
Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) {
|
Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) {
|
||||||
// adds to reporters/report zsets
|
|
||||||
await db.sortedSetAddBulk([
|
await db.sortedSetAddBulk([
|
||||||
[`flags:byReporter:${uid}`, timestamp, flagId],
|
[`flags:byReporter:${uid}`, timestamp, flagId],
|
||||||
[`flag:${flagId}:reports`, timestamp, reason],
|
[`flag:${flagId}:reports`, timestamp, [uid, reason].join(';')],
|
||||||
[`flag:${flagId}:reporters`, timestamp, uid],
|
|
||||||
|
|
||||||
['flags:hash', flagId, [type, id, uid].join(':')],
|
['flags:hash', flagId, [type, id, uid].join(':')],
|
||||||
]);
|
]);
|
||||||
|
|||||||
33
src/upgrades/1.15.0/remove_flag_reporters_zset.js
Normal file
33
src/upgrades/1.15.0/remove_flag_reporters_zset.js
Normal file
@@ -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,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user