mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-08-29 15:35:59 +02:00
feat: #8427, daily downvote limits
This commit is contained in:
@@ -119,18 +119,17 @@ module.exports = function (Posts) {
|
||||
}
|
||||
|
||||
async function unvote(pid, uid, command) {
|
||||
const [owner, voteStatus, reputation] = await Promise.all([
|
||||
const [owner, voteStatus] = await Promise.all([
|
||||
Posts.getPostField(pid, 'uid'),
|
||||
Posts.hasVoted(pid, uid),
|
||||
user.getUserField(uid, 'reputation'),
|
||||
]);
|
||||
|
||||
if (parseInt(uid, 10) === parseInt(owner, 10)) {
|
||||
throw new Error('[[error:self-vote]]');
|
||||
}
|
||||
|
||||
if (command === 'downvote' && reputation < meta.config['min:rep:downvote']) {
|
||||
throw new Error('[[error:not-enough-reputation-to-downvote]]');
|
||||
if (command === 'downvote') {
|
||||
await checkDownvoteLimitation(pid, uid);
|
||||
}
|
||||
|
||||
let hook;
|
||||
@@ -159,6 +158,33 @@ module.exports = function (Posts) {
|
||||
return await vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid);
|
||||
}
|
||||
|
||||
async function checkDownvoteLimitation(pid, uid) {
|
||||
const oneDay = 86400000;
|
||||
const [reputation, targetUid, downvotedPids] = await Promise.all([
|
||||
user.getUserField(uid, 'reputation'),
|
||||
Posts.getPostField(pid, 'uid'),
|
||||
db.getSortedSetRevRangeByScore(
|
||||
'uid:' + uid + ':downvote', 0, -1, '+inf', Date.now() - oneDay
|
||||
),
|
||||
]);
|
||||
|
||||
if (reputation < meta.config['min:rep:downvote']) {
|
||||
throw new Error('[[error:not-enough-reputation-to-downvote]]');
|
||||
}
|
||||
|
||||
if (meta.config.downvotesPerDay && downvotedPids.length >= meta.config.downvotesPerDay) {
|
||||
throw new Error('[[error:too-many-downvotes-today, ' + meta.config.downvotesPerDay + ']]');
|
||||
}
|
||||
|
||||
if (meta.config.downvotesPerUserPerDay) {
|
||||
const postData = await Posts.getPostsFields(downvotedPids, ['uid']);
|
||||
const targetDownvotes = postData.filter(p => p.uid === targetUid).length;
|
||||
if (targetDownvotes >= meta.config.downvotesPerUserPerDay) {
|
||||
throw new Error('[[error:too-many-downvotes-today-user, ' + meta.config.downvotesPerUserPerDay + ']]');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function vote(type, unvote, pid, uid) {
|
||||
uid = parseInt(uid, 10);
|
||||
if (uid <= 0) {
|
||||
|
||||
@@ -32,7 +32,10 @@
|
||||
<div class="col-sm-2 col-xs-12 settings-header">[[admin/settings/reputation:thresholds]]</div>
|
||||
<div class="col-sm-10 col-xs-12">
|
||||
<form>
|
||||
<strong>[[admin/settings/reputation:min-rep-downvote]]</strong><br /> <input type="text" class="form-control" placeholder="0" data-field="min:rep:downvote"><br />
|
||||
<strong>[[admin/settings/reputation:min-rep-downvote]]</strong><br /> <input type="text" class="form-control" placeholder="0"
|
||||
data-field="min:rep:downvote"><br />
|
||||
<strong>[[admin/settings/reputation:downvotes-per-day]]</strong><br /> <input type="text" class="form-control" placeholder="10" data-field="downvotesPerDay"><br />
|
||||
<strong>[[admin/settings/reputation:downvotes-per-user-per-day]]</strong><br /> <input type="text" class="form-control" placeholder="3" data-field="downvotesPerUserPerDay"><br />
|
||||
<strong>[[admin/settings/reputation:min-rep-flag]]</strong><br /> <input type="text" class="form-control" placeholder="0" data-field="min:rep:flag"><br />
|
||||
<strong>[[admin/settings/reputation:min-rep-website]]</strong><br /> <input type="text" class="form-control" placeholder="0" data-field="min:rep:website"><br />
|
||||
<strong>[[admin/settings/reputation:min-rep-aboutme]]</strong><br /> <input type="text" class="form-control" placeholder="0" data-field="min:rep:aboutme"><br />
|
||||
|
||||
Reference in New Issue
Block a user