You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB
JavaScript

'use strict';
const db = require('../../database');
const user = require('../../user');
const posts = require('../../posts');
const privileges = require('../../privileges');
const meta = require('../../meta');
module.exports = function (SocketPosts) {
SocketPosts.getVoters = async function (socket, data) {
if (!data || !data.pid || !data.cid) {
throw new Error('[[error:invalid-data]]');
}
const showDownvotes = !meta.config['downvote:disabled'];
const canSeeVotes = meta.config.votesArePublic || await privileges.categories.isAdminOrMod(data.cid, socket.uid);
if (!canSeeVotes) {
throw new Error('[[error:no-privileges]]');
}
const [upvoteUids, downvoteUids] = await Promise.all([
db.getSetMembers(`pid:${data.pid}:upvote`),
showDownvotes ? db.getSetMembers(`pid:${data.pid}:downvote`) : [],
]);
const [upvoters, downvoters] = await Promise.all([
user.getUsersFields(upvoteUids, ['username', 'userslug', 'picture']),
user.getUsersFields(downvoteUids, ['username', 'userslug', 'picture']),
]);
return {
upvoteCount: upvoters.length,
downvoteCount: downvoters.length,
showDownvotes: showDownvotes,
upvoters: upvoters,
downvoters: downvoters,
};
};
SocketPosts.getUpvoters = async function (socket, pids) {
if (!Array.isArray(pids)) {
throw new Error('[[error:invalid-data]]');
}
const data = await posts.getUpvotedUidsByPids(pids);
if (!data.length) {
return [];
}
const result = await Promise.all(data.map(async (uids) => {
let otherCount = 0;
if (uids.length > 6) {
otherCount = uids.length - 5;
uids = uids.slice(0, 5);
}
const usernames = await user.getUsernamesByUids(uids);
return {
otherCount: otherCount,
usernames: usernames,
};
}));
return result;
};
};