mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-13 17:35:46 +01:00
closes #4652
This commit is contained in:
@@ -61,6 +61,8 @@ module.exports = function (Categories) {
|
|||||||
'topics:tag',
|
'topics:tag',
|
||||||
'posts:edit',
|
'posts:edit',
|
||||||
'posts:delete',
|
'posts:delete',
|
||||||
|
'posts:upvote',
|
||||||
|
'posts:downvote',
|
||||||
'topics:delete',
|
'topics:delete',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -90,11 +90,11 @@ module.exports = function (Groups) {
|
|||||||
return callback(new Error('[[error:group-name-too-long]]'));
|
return callback(new Error('[[error:group-name-too-long]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Groups.isPrivilegeGroup(name) && name.indexOf(':') !== -1) {
|
if (!Groups.isPrivilegeGroup(name) && name.includes(':')) {
|
||||||
return callback(new Error('[[error:invalid-group-name]]'));
|
return callback(new Error('[[error:invalid-group-name]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name.indexOf('/') !== -1 || !utils.slugify(name)) {
|
if (name.includes('/') || !utils.slugify(name)) {
|
||||||
return callback(new Error('[[error:invalid-group-name]]'));
|
return callback(new Error('[[error:invalid-group-name]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ var meta = require('../meta');
|
|||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
var user = require('../user');
|
var user = require('../user');
|
||||||
var plugins = require('../plugins');
|
var plugins = require('../plugins');
|
||||||
|
var privileges = require('../privileges');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
var votesInProgress = {};
|
var votesInProgress = {};
|
||||||
@@ -15,16 +16,27 @@ module.exports = function (Posts) {
|
|||||||
return callback(new Error('[[error:reputation-system-disabled]]'));
|
return callback(new Error('[[error:reputation-system-disabled]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (voteInProgress(pid, uid)) {
|
async.waterfall([
|
||||||
return callback(new Error('[[error:already-voting-for-this-post]]'));
|
function (next) {
|
||||||
}
|
privileges.posts.can('posts:upvote', pid, uid, next);
|
||||||
|
},
|
||||||
|
function (canUpvote, next) {
|
||||||
|
if (!canUpvote) {
|
||||||
|
return next(new Error('[[error:no-privileges]]'));
|
||||||
|
}
|
||||||
|
|
||||||
putVoteInProgress(pid, uid);
|
if (voteInProgress(pid, uid)) {
|
||||||
|
return next(new Error('[[error:already-voting-for-this-post]]'));
|
||||||
|
}
|
||||||
|
|
||||||
toggleVote('upvote', pid, uid, function (err, data) {
|
putVoteInProgress(pid, uid);
|
||||||
clearVoteProgress(pid, uid);
|
|
||||||
callback(err, data);
|
toggleVote('upvote', pid, uid, function (err, data) {
|
||||||
});
|
clearVoteProgress(pid, uid);
|
||||||
|
next(err, data);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.downvote = function (pid, uid, callback) {
|
Posts.downvote = function (pid, uid, callback) {
|
||||||
@@ -36,16 +48,27 @@ module.exports = function (Posts) {
|
|||||||
return callback(new Error('[[error:downvoting-disabled]]'));
|
return callback(new Error('[[error:downvoting-disabled]]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (voteInProgress(pid, uid)) {
|
async.waterfall([
|
||||||
return callback(new Error('[[error:already-voting-for-this-post]]'));
|
function (next) {
|
||||||
}
|
privileges.posts.can('posts:downvote', pid, uid, next);
|
||||||
|
},
|
||||||
|
function (canUpvote, next) {
|
||||||
|
if (!canUpvote) {
|
||||||
|
return next(new Error('[[error:no-privileges]]'));
|
||||||
|
}
|
||||||
|
|
||||||
putVoteInProgress(pid, uid);
|
if (voteInProgress(pid, uid)) {
|
||||||
|
return next(new Error('[[error:already-voting-for-this-post]]'));
|
||||||
|
}
|
||||||
|
|
||||||
toggleVote('downvote', pid, uid, function (err, data) {
|
putVoteInProgress(pid, uid);
|
||||||
clearVoteProgress(pid, uid);
|
|
||||||
callback(err, data);
|
toggleVote('downvote', pid, uid, function (err, data) {
|
||||||
});
|
clearVoteProgress(pid, uid);
|
||||||
|
next(err, data);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.unvote = function (pid, uid, callback) {
|
Posts.unvote = function (pid, uid, callback) {
|
||||||
@@ -106,7 +129,7 @@ module.exports = function (Posts) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function voteInProgress(pid, uid) {
|
function voteInProgress(pid, uid) {
|
||||||
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].indexOf(parseInt(pid, 10)) !== -1;
|
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].includes(parseInt(pid, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
function putVoteInProgress(pid, uid) {
|
function putVoteInProgress(pid, uid) {
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ privileges.privilegeLabels = [
|
|||||||
{ name: 'Tag Topics' },
|
{ name: 'Tag Topics' },
|
||||||
{ name: 'Edit Posts' },
|
{ name: 'Edit Posts' },
|
||||||
{ name: 'Delete Posts' },
|
{ name: 'Delete Posts' },
|
||||||
|
{ name: 'Upvote Posts' },
|
||||||
|
{ name: 'Downvote Posts' },
|
||||||
{ name: 'Delete Topics' },
|
{ name: 'Delete Topics' },
|
||||||
{ name: 'Purge' },
|
{ name: 'Purge' },
|
||||||
{ name: 'Moderate' },
|
{ name: 'Moderate' },
|
||||||
@@ -25,6 +27,8 @@ privileges.userPrivilegeList = [
|
|||||||
'topics:tag',
|
'topics:tag',
|
||||||
'posts:edit',
|
'posts:edit',
|
||||||
'posts:delete',
|
'posts:delete',
|
||||||
|
'posts:upvote',
|
||||||
|
'posts:downvote',
|
||||||
'topics:delete',
|
'topics:delete',
|
||||||
'purge',
|
'purge',
|
||||||
'moderate',
|
'moderate',
|
||||||
|
|||||||
22
src/upgrades/1.8.0/vote_privilege.js
Normal file
22
src/upgrades/1.8.0/vote_privilege.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
|
var async = require('async');
|
||||||
|
|
||||||
|
var privileges = require('../../privileges');
|
||||||
|
var db = require('../../database');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'Give vote privilege to registered-users on all categories',
|
||||||
|
timestamp: Date.UTC(2018, 0, 9),
|
||||||
|
method: function (callback) {
|
||||||
|
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
async.eachSeries(cids, function (cid, next) {
|
||||||
|
privileges.categories.give(['posts:upvote', 'posts:downvote'], cid, 'registered-users', next);
|
||||||
|
}, callback);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<th class="arrowed" colspan="3">
|
<th class="arrowed" colspan="3">
|
||||||
[[admin/manage/categories:privileges.section-viewing]]
|
[[admin/manage/categories:privileges.section-viewing]]
|
||||||
</th>
|
</th>
|
||||||
<th class="arrowed" colspan="6">
|
<th class="arrowed" colspan="8">
|
||||||
[[admin/manage/categories:privileges.section-posting]]
|
[[admin/manage/categories:privileges.section-posting]]
|
||||||
</th>
|
</th>
|
||||||
<th class="arrowed" colspan="2">
|
<th class="arrowed" colspan="2">
|
||||||
@@ -61,7 +61,7 @@
|
|||||||
<th class="arrowed" colspan="3">
|
<th class="arrowed" colspan="3">
|
||||||
[[admin/manage/categories:privileges.section-viewing]]
|
[[admin/manage/categories:privileges.section-viewing]]
|
||||||
</th>
|
</th>
|
||||||
<th class="arrowed" colspan="6">
|
<th class="arrowed" colspan="8">
|
||||||
[[admin/manage/categories:privileges.section-posting]]
|
[[admin/manage/categories:privileges.section-posting]]
|
||||||
</th>
|
</th>
|
||||||
<th class="arrowed" colspan="2">
|
<th class="arrowed" colspan="2">
|
||||||
|
|||||||
@@ -651,6 +651,8 @@ describe('Categories', function () {
|
|||||||
'topics:tag': false,
|
'topics:tag': false,
|
||||||
'topics:delete': false,
|
'topics:delete': false,
|
||||||
'posts:edit': false,
|
'posts:edit': false,
|
||||||
|
'posts:upvote': false,
|
||||||
|
'posts:downvote': false,
|
||||||
purge: false,
|
purge: false,
|
||||||
moderate: false,
|
moderate: false,
|
||||||
});
|
});
|
||||||
@@ -678,6 +680,8 @@ describe('Categories', function () {
|
|||||||
assert.deepEqual(data, {
|
assert.deepEqual(data, {
|
||||||
'groups:find': true,
|
'groups:find': true,
|
||||||
'groups:posts:edit': true,
|
'groups:posts:edit': true,
|
||||||
|
'groups:posts:upvote': true,
|
||||||
|
'groups:posts:downvote': true,
|
||||||
'groups:topics:delete': false,
|
'groups:topics:delete': false,
|
||||||
'groups:topics:create': true,
|
'groups:topics:create': true,
|
||||||
'groups:topics:reply': true,
|
'groups:topics:reply': true,
|
||||||
|
|||||||
@@ -72,6 +72,22 @@ describe('Post\'s', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('voting', function () {
|
describe('voting', function () {
|
||||||
|
it('should fail to upvote post if group does not have upvote permission', function (done) {
|
||||||
|
privileges.categories.rescind(['posts:upvote', 'posts:downvote'], cid, 'registered-users', function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
socketPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' }, function (err) {
|
||||||
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||||||
|
socketPosts.downvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' }, function (err) {
|
||||||
|
assert.equal(err.message, '[[error:no-privileges]]');
|
||||||
|
privileges.categories.give(['posts:upvote', 'posts:downvote'], cid, 'registered-users', function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should upvote a post', function (done) {
|
it('should upvote a post', function (done) {
|
||||||
socketPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' }, function (err, result) {
|
socketPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' }, function (err, result) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user