feat: limit post/user flags per day closes #12274

This commit is contained in:
Barış Soner Uşaklı
2024-01-18 17:07:52 -05:00
parent 9e11b92780
commit 2b1fdc56a1
5 changed files with 46 additions and 2 deletions

View File

@@ -99,6 +99,8 @@
"min:rep:aboutme": 0,
"min:rep:signature": 0,
"flags:limitPerTarget": 0,
"flags:postFlagsPerDay": 10,
"flags:userFlagsPerDay": 10,
"flags:autoFlagOnDownvoteThreshold": 0,
"flags:actionOnResolve": "rescind",
"flags:actionOnReject": "rescind",

View File

@@ -23,7 +23,12 @@
"flags.limit-per-target": "Maximum number of times something can be flagged",
"flags.limit-per-target-placeholder": "Default: 0",
"flags.limit-per-target-help": "When a post or user is flagged multiple times, each additional flag is considered a "report" and added to the original flag. Set this option to a number other than zero to limit the number of reports an item can receive.",
"flags.auto-flag-on-downvote-threshold": "Number of downvotes to auto flag posts (Set to 0 to disable, default: 0)",
"flags.limit-post-flags-per-day": "Maximum number of times a user can flag posts in a day",
"flags.limit-post-flags-per-day-help": "Set to 0 to disable, (default: 10)",
"flags.limit-user-flags-per-day": "Maximum number of times a user can flag users in a day",
"flags.limit-user-flags-per-day-help": "Set to 0 to disable, (default: 10)",
"flags.auto-flag-on-downvote-threshold": "Number of downvotes to auto flag posts",
"flags.auto-flag-on-downvote-threshold-help": "Set to 0 to disable, (default: 0)",
"flags.auto-resolve-on-ban": "Automatically resolve all of a user's tickets when they are banned",
"flags.action-on-resolve": "Do the following when a flag is resolved",
"flags.action-on-reject": "Do the following when a flag is rejected",

View File

@@ -211,6 +211,8 @@
"user-already-flagged": "You have already flagged this user",
"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",
"too-many-post-flags-per-day": "You can only flag %1 post(s) per day",
"too-many-user-flags-per-day": "You can only flag %1 user(s) per day",
"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",

View File

@@ -629,8 +629,22 @@ Flags.canFlag = async function (type, id, uid, skipLimitCheck = false) {
throw new Error(`[[error:${type}-flagged-too-many-times]]`);
}
}
const oneday = 24 * 60 * 60 * 1000;
const now = Date.now();
const [flagIds, canRead, isPrivileged] = await Promise.all([
db.getSortedSetRangeByScore(`flags:byReporter:${uid}`, 0, -1, now - oneday, '+inf'),
privileges.posts.can('topics:read', id, uid),
user.isPrivileged(uid),
]);
const allowedFlagsPerDay = meta.config[`flags:${type}FlagsPerDay`];
if (!isPrivileged && allowedFlagsPerDay > 0) {
const flagData = await db.getObjects(flagIds.map(id => `flag:${id}`));
const flagsOfType = flagData.filter(f => f && f.type === type);
if (allowedFlagsPerDay > 0 && flagsOfType.length > allowedFlagsPerDay) {
throw new Error(`[[error:too-many-${type}-flags-per-day, ${allowedFlagsPerDay}]]`);
}
}
const canRead = await privileges.posts.can('topics:read', id, uid);
switch (type) {
case 'user':
return true;

View File

@@ -94,9 +94,30 @@
[[admin/settings/reputation:flags.limit-per-target-help]]
</p>
</div>
<div class="mb-3">
<label class="form-label" for="flags:postFlagsPerDay">[[admin/settings/reputation:flags.limit-post-flags-per-day]]</label>
<input type="number" min="0" class="form-control" data-field="flags:postFlagsPerDay" id="flags:postFlagsPerDay">
<p class="form-text">
[[admin/settings/reputation:flags.limit-post-flags-per-day-help]]
</p>
</div>
<div class="mb-3">
<label class="form-label" for="flags:userFlagsPerDay">[[admin/settings/reputation:flags.limit-user-flags-per-day]]</label>
<input type="number" min="0" class="form-control" data-field="flags:userFlagsPerDay" id="flags:userFlagsPerDay">
<p class="form-text">
[[admin/settings/reputation:flags.limit-user-flags-per-day-help]]
</p>
</div>
<div class="mb-3">
<label class="form-label" for="flags:autoFlagOnDownvoteThreshold">[[admin/settings/reputation:flags.auto-flag-on-downvote-threshold]]</label>
<input type="number" min="0" class="form-control" placeholder="0" data-field="flags:autoFlagOnDownvoteThreshold" id="flags:autoFlagOnDownvoteThreshold">
<p class="form-text">
[[admin/settings/reputation:flags.auto-flag-on-downvote-threshold-help]]
</p>
</div>
<div class="mb-3">