mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-08-26 21:56:35 +02:00
Merge branch 'master' into develop
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
const validator = require('validator');
|
||||
const _ = require('lodash');
|
||||
|
||||
const db = require('../database');
|
||||
const utils = require('../utils');
|
||||
const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
@@ -306,6 +307,95 @@ postsAPI.unvote = async function (caller, data) {
|
||||
return await apiHelpers.postCommand(caller, 'unvote', 'voted', '', data);
|
||||
};
|
||||
|
||||
postsAPI.getVoters = async function (caller, data) {
|
||||
if (!data || !data.pid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const { pid } = data;
|
||||
const cid = await posts.getCidByPid(pid);
|
||||
if (!await canSeeVotes(caller.uid, cid)) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const showDownvotes = !meta.config['downvote:disabled'];
|
||||
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,
|
||||
};
|
||||
};
|
||||
|
||||
postsAPI.getUpvoters = async function (caller, data) {
|
||||
if (!data.pid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const { pid } = data;
|
||||
const cid = await posts.getCidByPid(pid);
|
||||
if (!await canSeeVotes(caller.uid, cid)) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
let upvotedUids = (await posts.getUpvotedUidsByPids([pid]))[0];
|
||||
const cutoff = 6;
|
||||
if (!upvotedUids.length) {
|
||||
return {
|
||||
otherCount: 0,
|
||||
usernames: [],
|
||||
cutoff,
|
||||
};
|
||||
}
|
||||
let otherCount = 0;
|
||||
if (upvotedUids.length > cutoff) {
|
||||
otherCount = upvotedUids.length - (cutoff - 1);
|
||||
upvotedUids = upvotedUids.slice(0, cutoff - 1);
|
||||
}
|
||||
|
||||
const usernames = await user.getUsernamesByUids(upvotedUids);
|
||||
return {
|
||||
otherCount,
|
||||
usernames,
|
||||
cutoff,
|
||||
};
|
||||
};
|
||||
|
||||
async function canSeeVotes(uid, cids) {
|
||||
const isArray = Array.isArray(cids);
|
||||
if (!isArray) {
|
||||
cids = [cids];
|
||||
}
|
||||
const uniqCids = _.uniq(cids);
|
||||
const [canRead, isAdmin, isMod] = await Promise.all([
|
||||
privileges.categories.isUserAllowedTo(
|
||||
'topics:read', uniqCids, uid
|
||||
),
|
||||
privileges.users.isAdministrator(uid),
|
||||
privileges.users.isModerator(uid, cids),
|
||||
]);
|
||||
const cidToAllowed = _.zipObject(uniqCids, canRead);
|
||||
const checks = cids.map(
|
||||
(cid, index) => isAdmin || isMod[index] ||
|
||||
(
|
||||
cidToAllowed[cid] &&
|
||||
(
|
||||
meta.config.voteVisibility === 'all' ||
|
||||
(meta.config.voteVisibility === 'loggedin' && parseInt(uid, 10) > 0)
|
||||
)
|
||||
)
|
||||
);
|
||||
return isArray ? checks : checks[0];
|
||||
}
|
||||
|
||||
postsAPI.bookmark = async function (caller, data) {
|
||||
return await apiHelpers.postCommand(caller, 'bookmark', 'bookmarked', '', data);
|
||||
};
|
||||
|
||||
@@ -131,6 +131,16 @@ Posts.unvote = async (req, res) => {
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Posts.getVoters = async (req, res) => {
|
||||
const data = await api.posts.getVoters(req, { pid: req.params.pid });
|
||||
helpers.formatApiResponse(200, res, data);
|
||||
};
|
||||
|
||||
Posts.getUpvoters = async (req, res) => {
|
||||
const data = await api.posts.getUpvoters(req, { pid: req.params.pid });
|
||||
helpers.formatApiResponse(200, res, data);
|
||||
};
|
||||
|
||||
Posts.bookmark = async (req, res) => {
|
||||
const data = await mock(req);
|
||||
await api.posts.bookmark(req, data);
|
||||
|
||||
@@ -26,6 +26,8 @@ module.exports = function () {
|
||||
|
||||
setupApiRoute(router, 'put', '/:pid/vote', [...middlewares, middleware.checkRequired.bind(null, ['delta'])], controllers.write.posts.vote);
|
||||
setupApiRoute(router, 'delete', '/:pid/vote', middlewares, controllers.write.posts.unvote);
|
||||
setupApiRoute(router, 'get', '/:pid/voters', [middleware.assert.post], controllers.write.posts.getVoters);
|
||||
setupApiRoute(router, 'get', '/:pid/upvoters', [middleware.assert.post], controllers.write.posts.getUpvoters);
|
||||
|
||||
setupApiRoute(router, 'put', '/:pid/bookmark', middlewares, controllers.write.posts.bookmark);
|
||||
setupApiRoute(router, 'delete', '/:pid/bookmark', middlewares, controllers.write.posts.unbookmark);
|
||||
|
||||
@@ -131,10 +131,10 @@ async function onConnect(socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (socket.uid) {
|
||||
if (socket.uid > 0) {
|
||||
socket.join(`uid_${socket.uid}`);
|
||||
socket.join('online_users');
|
||||
} else {
|
||||
} else if (socket.uid === 0) {
|
||||
socket.join('online_guests');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,105 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const db = require('../../database');
|
||||
const user = require('../../user');
|
||||
const posts = require('../../posts');
|
||||
const privileges = require('../../privileges');
|
||||
const meta = require('../../meta');
|
||||
const api = require('../../api');
|
||||
const sockets = require('../index');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.getVoters = async function (socket, data) {
|
||||
if (!data || !data.pid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const cid = await posts.getCidByPid(data.pid);
|
||||
if (!await canSeeVotes(socket.uid, cid)) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const showDownvotes = !meta.config['downvote:disabled'];
|
||||
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,
|
||||
};
|
||||
sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/voters');
|
||||
return await api.posts.getVoters(socket, { pid: data.pid });
|
||||
};
|
||||
|
||||
SocketPosts.getUpvoters = async function (socket, pids) {
|
||||
if (!Array.isArray(pids)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
const cids = await posts.getCidsByPids(pids);
|
||||
if ((await canSeeVotes(socket.uid, cids)).includes(false)) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
const data = await posts.getUpvotedUidsByPids(pids);
|
||||
if (!data.length) {
|
||||
return [];
|
||||
}
|
||||
const cutoff = 6;
|
||||
const sliced = data.map((uids) => {
|
||||
let otherCount = 0;
|
||||
if (uids.length > cutoff) {
|
||||
otherCount = uids.length - (cutoff - 1);
|
||||
uids = uids.slice(0, cutoff - 1);
|
||||
}
|
||||
return {
|
||||
otherCount,
|
||||
uids,
|
||||
};
|
||||
});
|
||||
|
||||
const uniqUids = _.uniq(_.flatten(sliced.map(d => d.uids)));
|
||||
const usernameMap = _.zipObject(uniqUids, await user.getUsernamesByUids(uniqUids));
|
||||
const result = sliced.map(
|
||||
data => ({
|
||||
otherCount: data.otherCount,
|
||||
cutoff: cutoff,
|
||||
usernames: data.uids.map(uid => usernameMap[uid]),
|
||||
})
|
||||
);
|
||||
return result;
|
||||
sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/upvoters');
|
||||
return await api.posts.getUpvoters(socket, { pid: pids[0] });
|
||||
};
|
||||
|
||||
async function canSeeVotes(uid, cids) {
|
||||
const isArray = Array.isArray(cids);
|
||||
if (!isArray) {
|
||||
cids = [cids];
|
||||
}
|
||||
const uniqCids = _.uniq(cids);
|
||||
const [canRead, isAdmin, isMod] = await Promise.all([
|
||||
privileges.categories.isUserAllowedTo(
|
||||
'topics:read', uniqCids, uid
|
||||
),
|
||||
privileges.users.isAdministrator(uid),
|
||||
privileges.users.isModerator(uid, cids),
|
||||
]);
|
||||
const cidToAllowed = _.zipObject(uniqCids, canRead);
|
||||
const checks = cids.map(
|
||||
(cid, index) => isAdmin || isMod[index] ||
|
||||
(
|
||||
cidToAllowed[cid] &&
|
||||
(
|
||||
meta.config.voteVisibility === 'all' ||
|
||||
(meta.config.voteVisibility === 'loggedin' && parseInt(uid, 10) > 0)
|
||||
)
|
||||
)
|
||||
);
|
||||
return isArray ? checks : checks[0];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -121,15 +121,15 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="card mb-3 pointer" data-container-html='<div class="card"><h5 class="card-header">\{{title}}</h5><div class="card-body">\{{body}}</div></div>'>
|
||||
<div class="card-header d-flex justify-content-between">
|
||||
<div class="card-header d-flex justify-content-between text-nowrap flex-wrap align-items-center">
|
||||
[[admin/extend/widgets:container.card-header]]
|
||||
<div class="d-flex gap-1 color-selector">
|
||||
<button data-class="text-bg-primary" class="btn btn-sm btn-primary"</button>
|
||||
<button data-class="" class="btn btn-sm btn-secondary"</button>
|
||||
<button data-class="text-bg-success" class="btn btn-sm btn-success"</button>
|
||||
<button data-class="text-bg-info" class="btn btn-sm btn-info"</button>
|
||||
<button data-class="text-bg-warning" class="btn btn-sm btn-warning"</button>
|
||||
<button data-class="text-bg-danger" class="btn btn-sm btn-danger"</button>
|
||||
<div class="d-flex gap-1 color-selector" style="height: 18px;">
|
||||
<button data-class="text-bg-primary" class="btn btn-sm btn-primary"></button>
|
||||
<button data-class="" class="btn btn-sm btn-secondary"></button>
|
||||
<button data-class="text-bg-success" class="btn btn-sm btn-success"></button>
|
||||
<button data-class="text-bg-info" class="btn btn-sm btn-info"></button>
|
||||
<button data-class="text-bg-warning" class="btn btn-sm btn-warning"></button>
|
||||
<button data-class="text-bg-danger" class="btn btn-sm btn-danger"></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
@@ -138,9 +138,9 @@
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info pointer" data-container-html='<div class="alert alert-info">\{{body}}</div>'>
|
||||
<div class="d-flex justify-content-between">
|
||||
<div class="d-flex justify-content-between text-nowrap flex-wrap align-items-center">
|
||||
[[admin/extend/widgets:container.alert]]
|
||||
<div class="d-flex gap-1 color-selector">
|
||||
<div class="d-flex gap-1 color-selector" style="height: 18px;">
|
||||
<button data-class="alert-success" class="btn btn-sm btn-success"></button>
|
||||
<button data-class="alert-info" class="btn btn-sm btn-info"></button>
|
||||
<button data-class="alert-warning" class="btn btn-sm btn-warning"></button>
|
||||
|
||||
Reference in New Issue
Block a user