mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-05-06 11:55:53 +02:00
feat: backend methods for rescinding a report, supplemental adds and removing a report now adds to the flag history
This commit is contained in:
47
src/flags.js
47
src/flags.js
@@ -417,7 +417,10 @@ Flags.create = async function (type, id, uid, reason, timestamp, forceFlag = fal
|
||||
const flagId = await Flags.getFlagIdByTarget(type, id);
|
||||
await Promise.all([
|
||||
Flags.addReport(flagId, type, id, uid, reason, timestamp),
|
||||
Flags.update(flagId, uid, { state: 'open' }),
|
||||
Flags.update(flagId, uid, {
|
||||
state: 'open',
|
||||
report: 'added',
|
||||
}),
|
||||
]);
|
||||
|
||||
return await Flags.get(flagId);
|
||||
@@ -553,6 +556,45 @@ Flags.addReport = async function (flagId, type, id, uid, reason, timestamp) {
|
||||
plugins.hooks.fire('action:flags.addReport', { flagId, type, id, uid, reason, timestamp });
|
||||
};
|
||||
|
||||
Flags.rescindReport = async (type, id, uid) => {
|
||||
const exists = await Flags.exists(type, id, uid);
|
||||
if (!exists) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const flagId = await db.sortedSetScore('flags:hash', [type, id, uid].join(':'));
|
||||
const reports = await db.getSortedSetMembers(`flag:${flagId}:reports`);
|
||||
let reason;
|
||||
reports.forEach((payload) => {
|
||||
if (!reason) {
|
||||
const [payloadUid, payloadReason] = payload.split(';');
|
||||
if (parseInt(payloadUid, 10) === parseInt(uid, 10)) {
|
||||
reason = payloadReason;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!reason) {
|
||||
throw new Error('[[error:cant-locate-flag-report]]');
|
||||
}
|
||||
|
||||
await db.sortedSetRemoveBulk([
|
||||
[`flags:byReporter:${uid}`, flagId],
|
||||
[`flag:${flagId}:reports`, [uid, reason].join(';')],
|
||||
|
||||
['flags:hash', [type, id, uid].join(':')],
|
||||
]);
|
||||
|
||||
// If there are no more reports, consider the flag resolved
|
||||
const reportCount = await db.sortedSetCard(`flag:${flagId}:reports`);
|
||||
if (reportCount < 1) {
|
||||
await Flags.update(flagId, uid, {
|
||||
state: 'resolved',
|
||||
report: 'rescinded',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Flags.exists = async function (type, id, uid) {
|
||||
return await db.isSortedSetMember('flags:hash', [type, id, uid].join(':'));
|
||||
};
|
||||
@@ -766,6 +808,9 @@ Flags.getHistory = async function (flagId) {
|
||||
if (changeset.hasOwnProperty('state')) {
|
||||
changeset.state = changeset.state === undefined ? '' : `[[flags:state-${changeset.state}]]`;
|
||||
}
|
||||
if (changeset.hasOwnProperty('report')) {
|
||||
changeset.report = `[[flags:report-${changeset.report}]]`;
|
||||
}
|
||||
|
||||
return {
|
||||
uid: entry.value[0],
|
||||
|
||||
Reference in New Issue
Block a user