Skip to content

Commit

Permalink
feat: backend methods for rescinding a report, supplemental adds and …
Browse files Browse the repository at this point in the history
…removing a report now adds to the flag history
  • Loading branch information
julianlam committed Oct 12, 2023
1 parent 5e44091 commit bc0f362
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions public/language/en-GB/error.json
Expand Up @@ -211,6 +211,7 @@
"post-flagged-too-many-times": "This post has been flagged by others already",
"user-flagged-too-many-times": "This user has been flagged by others already",
"cant-flag-privileged": "You are not allowed to flag the profiles or content of privileged users (moderators/global moderators/admins)",
"cant-locate-flag-report": "Cannot locate flag report",
"self-vote": "You cannot vote on your own post",
"too-many-upvotes-today": "You can only upvote %1 times a day",
"too-many-upvotes-today-user": "You can only upvote a user %1 times a day",
Expand Down
3 changes: 3 additions & 0 deletions public/language/en-GB/flags.json
@@ -1,5 +1,6 @@
{
"state": "State",
"report": "Report",
"reports": "Reports",
"first-reported": "First Reported",
"no-flags": "Hooray! No flags found.",
Expand All @@ -8,6 +9,8 @@
"update": "Update",
"updated": "Updated",
"resolved": "Resolved",
"report-added": "Added",
"report-rescinded": "Rescinded",
"target-purged": "The content this flag referred to has been purged and is no longer available.",
"target-aboutme-empty": "This user has no "About Me" set.",

Expand Down
47 changes: 46 additions & 1 deletion src/flags.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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(':'));
};
Expand Down Expand Up @@ -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],
Expand Down
1 change: 1 addition & 0 deletions src/routes/write/flags.js
Expand Up @@ -12,6 +12,7 @@ module.exports = function () {

setupApiRoute(router, 'post', '/', [...middlewares], controllers.write.flags.create);

// Note: access control provided by middleware.assert.flag
setupApiRoute(router, 'get', '/:flagId', [...middlewares, middleware.assert.flag], controllers.write.flags.get);
setupApiRoute(router, 'put', '/:flagId', [...middlewares, middleware.assert.flag], controllers.write.flags.update);
setupApiRoute(router, 'delete', '/:flagId', [...middlewares, middleware.assert.flag], controllers.write.flags.delete);
Expand Down

0 comments on commit bc0f362

Please sign in to comment.