Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endorse: Backend changes to display endorsement next to the author of a post in a topic #25

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
reporter: dot
timeout: 25000
exit: true
bail: true
bail: false
2 changes: 2 additions & 0 deletions public/openapi/read/topic/topic_id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ get:
type: boolean
downvoted:
type: boolean
showendorse:
type: boolean
replies:
type: object
properties:
Expand Down
5 changes: 3 additions & 2 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ postsAPI.getVoters = async function (caller, data) {

let hasAdminUpvoted = false;

for (const uid of upvoteUids) {
const isAdmin = groups.isMemberOfGroups(uid, ['administrators']);
const uidPromises = upvoteUids.map(uid => groups.isMemberOfGroups(uid, ['administrators']));
const results = await Promise.all(uidPromises);
for (const isAdmin of results) {
if (isAdmin[0]) {
hasAdminUpvoted = true;
break; // Exit loop as soon as an admin is found
Expand Down
10 changes: 9 additions & 1 deletion src/posts/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const topics = require('../topics');
const plugins = require('../plugins');
const privileges = require('../privileges');
const translator = require('../translator');
const groups = require('../groups');

module.exports = function (Posts) {
const votesInProgress = {};
Expand Down Expand Up @@ -83,14 +84,21 @@ module.exports = function (Posts) {
Posts.getVoteStatusByPostIDs = async function (pids, uid) {
if (parseInt(uid, 10) <= 0) {
const data = pids.map(() => false);
return { upvotes: data, downvotes: data };
return { upvotes: data, downvotes: data, showendorse: data };
}
const endorsements = await Promise.all(pids.map(async (pid) => {
const upvoteUids = await db.getSetMembers(`pid:${pid}:upvote`);
const uidPromises = upvoteUids.map(uid => groups.isMemberOfGroups(uid, ['administrators']));
const results = await Promise.all(uidPromises);
return results.some(isAdmin => isAdmin[0]);
}));
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),
downvotes: data.slice(pids.length, pids.length * 2),
showendorse: endorsements,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/topics/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ module.exports = function (Topics) {
postObj.bookmarked = bookmarks[i];
postObj.upvoted = voteData.upvotes[i];
postObj.downvoted = voteData.downvotes[i];
postObj.showendorse = voteData.showendorse[i];
postObj.votes = postObj.votes || 0;
postObj.replies = replies[i];
postObj.selfPost = parseInt(uid, 10) > 0 && parseInt(uid, 10) === postObj.uid;
Expand Down