diff --git a/src/posts/votes.js b/src/posts/votes.js index 8e930cc8d0..d19814d0d6 100644 --- a/src/posts/votes.js +++ b/src/posts/votes.js @@ -1,14 +1,14 @@ 'use strict'; -var meta = require('../meta'); -var db = require('../database'); -var user = require('../user'); -var topics = require('../topics'); -var plugins = require('../plugins'); -var privileges = require('../privileges'); +const meta = require('../meta'); +const db = require('../database'); +const user = require('../user'); +const topics = require('../topics'); +const plugins = require('../plugins'); +const privileges = require('../privileges'); module.exports = function (Posts) { - var votesInProgress = {}; + const votesInProgress = {}; Posts.upvote = async function (pid, uid) { if (meta.config['reputation:disabled']) { @@ -25,8 +25,7 @@ module.exports = function (Posts) { putVoteInProgress(pid, uid); try { - const data = await toggleVote('upvote', pid, uid); - return data; + return await toggleVote('upvote', pid, uid); } finally { clearVoteProgress(pid, uid); } @@ -51,8 +50,7 @@ module.exports = function (Posts) { putVoteInProgress(pid, uid); try { - const data = toggleVote('downvote', pid, uid); - return data; + return await toggleVote('downvote', pid, uid); } finally { clearVoteProgress(pid, uid); } @@ -65,8 +63,7 @@ module.exports = function (Posts) { putVoteInProgress(pid, uid); try { - const data = await unvote(pid, uid, 'unvote'); - return data; + return await unvote(pid, uid, 'unvote'); } finally { clearVoteProgress(pid, uid); } @@ -85,13 +82,8 @@ module.exports = function (Posts) { const data = pids.map(() => false); return { upvotes: data, downvotes: data }; } - var upvoteSets = []; - var downvoteSets = []; - - for (var i = 0; i < pids.length; i += 1) { - upvoteSets.push('pid:' + pids[i] + ':upvote'); - downvoteSets.push('pid:' + pids[i] + ':downvote'); - } + const upvoteSets = pids.map(pid => 'pid:' + pid + ':upvote'); + const downvoteSets = pids.map(pid => 'pid:' + pid + ':downvote'); const data = await db.isMemberOfSets(upvoteSets.concat(downvoteSets), uid); return { upvotes: data.slice(0, pids.length), @@ -114,7 +106,7 @@ module.exports = function (Posts) { function clearVoteProgress(pid, uid) { if (Array.isArray(votesInProgress[uid])) { - var index = votesInProgress[uid].indexOf(parseInt(pid, 10)); + const index = votesInProgress[uid].indexOf(parseInt(pid, 10)); if (index !== -1) { votesInProgress[uid].splice(index, 1); } @@ -141,8 +133,8 @@ module.exports = function (Posts) { throw new Error('[[error:not-enough-reputation-to-downvote]]'); } - var hook; - var current = voteStatus.upvoted ? 'upvote' : 'downvote'; + let hook; + let current = voteStatus.upvoted ? 'upvote' : 'downvote'; if ((voteStatus.upvoted && command === 'downvote') || (voteStatus.downvoted && command === 'upvote')) { // e.g. User *has* upvoted, and clicks downvote hook = command; @@ -172,25 +164,24 @@ module.exports = function (Posts) { if (uid <= 0) { throw new Error('[[error:not-logged-in]]'); } - const postData = await Posts.getPostFields(pid, ['pid', 'uid', 'tid']); - - var now = Date.now(); + const now = Date.now(); if (type === 'upvote' && !unvote) { - db.sortedSetAdd('uid:' + uid + ':upvote', now, pid); + await db.sortedSetAdd('uid:' + uid + ':upvote', now, pid); } else { - db.sortedSetRemove('uid:' + uid + ':upvote', pid); + await db.sortedSetRemove('uid:' + uid + ':upvote', pid); } if (type === 'upvote' || unvote) { - db.sortedSetRemove('uid:' + uid + ':downvote', pid); + await db.sortedSetRemove('uid:' + uid + ':downvote', pid); } else { - db.sortedSetAdd('uid:' + uid + ':downvote', now, pid); + await db.sortedSetAdd('uid:' + uid + ':downvote', now, pid); } + const postData = await Posts.getPostFields(pid, ['pid', 'uid', 'tid']); const newReputation = await user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1); if (parseInt(postData.uid, 10)) { - db.sortedSetAdd('users:reputation', newReputation, postData.uid); + await db.sortedSetAdd('users:reputation', newReputation, postData.uid); } await adjustPostVotes(postData, uid, type, unvote); @@ -207,7 +198,7 @@ module.exports = function (Posts) { } async function adjustPostVotes(postData, uid, type, unvote) { - var notType = (type === 'upvote' ? 'downvote' : 'upvote'); + const notType = (type === 'upvote' ? 'downvote' : 'upvote'); if (unvote) { await db.setRemove('pid:' + postData.pid + ':' + type, uid); } else {