From 5afd5de07d42fd33f039a6f85ded3b4992200e5a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 3 Feb 2022 14:34:22 -0500 Subject: [PATCH] feat: new accounts route to show most downvoted ('controversial') posts --- public/language/en-GB/global.json | 1 + public/language/en-GB/pages.json | 1 + src/controllers/accounts/helpers.js | 5 ++++- src/controllers/accounts/posts.js | 26 ++++++++++++++++++++++++++ src/posts/votes.js | 2 +- src/routes/user.js | 1 + 6 files changed, 34 insertions(+), 2 deletions(-) diff --git a/public/language/en-GB/global.json b/public/language/en-GB/global.json index afcb01d792..3f974d70dc 100644 --- a/public/language/en-GB/global.json +++ b/public/language/en-GB/global.json @@ -70,6 +70,7 @@ "posts": "Posts", "x-posts": "%1 posts", "best": "Best", + "controversial": "Controversial", "votes": "Votes", "x-votes": "%1 votes", "voters": "Voters", diff --git a/public/language/en-GB/pages.json b/public/language/en-GB/pages.json index 9de2a50eb1..6586bf499c 100644 --- a/public/language/en-GB/pages.json +++ b/public/language/en-GB/pages.json @@ -60,6 +60,7 @@ "account/upvoted": "Posts upvoted by %1", "account/downvoted": "Posts downvoted by %1", "account/best": "Best posts made by %1", + "account/controversial": "Controversial posts made by %1", "account/blocks": "Blocked users for %1", "account/uploads": "Uploads by %1", "account/sessions": "Login Sessions", diff --git a/src/controllers/accounts/helpers.js b/src/controllers/accounts/helpers.js index e0462ea2e8..12d82144e5 100644 --- a/src/controllers/accounts/helpers.js +++ b/src/controllers/accounts/helpers.js @@ -155,7 +155,8 @@ async function getCounts(userData, callerUID) { const cids = await categories.getCidsByPrivilege('categories:cid', callerUID, 'topics:read'); const promises = { posts: db.sortedSetsCardSum(cids.map(c => `cid:${c}:uid:${uid}:pids`)), - best: db.sortedSetsCardSum(cids.map(c => `cid:${c}:uid:${uid}:pids:votes`)), + best: Promise.all(cids.map(async c => db.sortedSetCount(`cid:${c}:uid:${uid}:pids:votes`, 1, '+inf'))), + controversial: Promise.all(cids.map(async c => db.sortedSetCount(`cid:${c}:uid:${uid}:pids:votes`, '-inf', -1))), topics: db.sortedSetsCardSum(cids.map(c => `cid:${c}:uid:${uid}:tids`)), }; if (userData.isAdmin || userData.isSelf) { @@ -169,6 +170,8 @@ async function getCounts(userData, callerUID) { promises.blocks = user.getUserField(userData.uid, 'blocksCount'); } const counts = await utils.promiseParallel(promises); + counts.best = counts.best.reduce((sum, count) => sum + count); + counts.controversial = counts.controversial.reduce((sum, count) => sum + count); counts.categoriesWatched = counts.categoriesWatched && counts.categoriesWatched.length; counts.groups = userData.groups.length; counts.following = userData.followingCount; diff --git a/src/controllers/accounts/posts.js b/src/controllers/accounts/posts.js index b266a2b185..d7e2cf000f 100644 --- a/src/controllers/accounts/posts.js +++ b/src/controllers/accounts/posts.js @@ -5,6 +5,7 @@ const user = require('../../user'); const posts = require('../../posts'); const topics = require('../../topics'); const categories = require('../../categories'); +const privileges = require('../../privileges'); const pagination = require('../../pagination'); const helpers = require('../helpers'); const accountHelpers = require('./helpers'); @@ -55,6 +56,27 @@ const templateToData = { const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read'); return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); }, + getTopics: async (sets, req, start, stop) => { + let pids = await db.getSortedSetRevRangeByScore(sets, start, stop, '+inf', '1'); + pids = await privileges.posts.filter('topics:read', pids, req.uid); + const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); + return { posts: postObjs, nextStart: stop + 1 }; + }, + }, + 'account/controversial': { + type: 'posts', + noItemsFoundKey: '[[user:has_no_voted_posts]]', + crumb: '[[global:controversial]]', + getSets: async function (callerUid, userData) { + const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read'); + return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); + }, + getTopics: async (sets, req, start, stop) => { + let pids = await db.getSortedSetRangeByScore(sets, start, stop, '-inf', '-1'); + pids = await privileges.posts.filter('topics:read', pids, req.uid); + const postObjs = await posts.getPostSummaryByPids(pids, req.uid, { stripTags: false }); + return { posts: postObjs, nextStart: stop + 1 }; + }, }, 'account/watched': { type: 'topics', @@ -128,6 +150,10 @@ postsController.getBestPosts = async function (req, res, next) { await getPostsFromUserSet('account/best', req, res, next); }; +postsController.getControversialPosts = async function (req, res, next) { + await getPostsFromUserSet('account/controversial', req, res, next); +}; + postsController.getWatchedTopics = async function (req, res, next) { await getPostsFromUserSet('account/watched', req, res, next); }; diff --git a/src/posts/votes.js b/src/posts/votes.js index 8f3f240314..1a56c4a66d 100644 --- a/src/posts/votes.js +++ b/src/posts/votes.js @@ -258,7 +258,7 @@ module.exports = function (Posts) { const topicData = await topics.getTopicFields(postData.tid, ['mainPid', 'cid', 'pinned']); if (postData.uid) { - if (postData.votes > 0) { + if (postData.votes !== 0) { await db.sortedSetAdd(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.votes, postData.pid); } else { await db.sortedSetRemove(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.pid); diff --git a/src/routes/user.js b/src/routes/user.js index 763b8189c4..937997eba5 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -24,6 +24,7 @@ module.exports = function (app, name, middleware, controllers) { setupPageRoute(app, `/${name}/:userslug/posts`, middleware, middlewares, controllers.accounts.posts.getPosts); setupPageRoute(app, `/${name}/:userslug/topics`, middleware, middlewares, controllers.accounts.posts.getTopics); setupPageRoute(app, `/${name}/:userslug/best`, middleware, middlewares, controllers.accounts.posts.getBestPosts); + setupPageRoute(app, `/${name}/:userslug/controversial`, middleware, middlewares, controllers.accounts.posts.getControversialPosts); setupPageRoute(app, `/${name}/:userslug/groups`, middleware, middlewares, controllers.accounts.groups.get); setupPageRoute(app, `/${name}/:userslug/categories`, middleware, accountMiddlewares, controllers.accounts.categories.get);