feat: new accounts route to show most downvoted ('controversial') posts

isekai-main
Julian Lam 3 years ago
parent 1eaae1d0a7
commit 5afd5de07d

@ -70,6 +70,7 @@
"posts": "Posts", "posts": "Posts",
"x-posts": "%1 posts", "x-posts": "%1 posts",
"best": "Best", "best": "Best",
"controversial": "Controversial",
"votes": "Votes", "votes": "Votes",
"x-votes": "%1 votes", "x-votes": "%1 votes",
"voters": "Voters", "voters": "Voters",

@ -60,6 +60,7 @@
"account/upvoted": "Posts upvoted by %1", "account/upvoted": "Posts upvoted by %1",
"account/downvoted": "Posts downvoted by %1", "account/downvoted": "Posts downvoted by %1",
"account/best": "Best posts made by %1", "account/best": "Best posts made by %1",
"account/controversial": "Controversial posts made by %1",
"account/blocks": "Blocked users for %1", "account/blocks": "Blocked users for %1",
"account/uploads": "Uploads by %1", "account/uploads": "Uploads by %1",
"account/sessions": "Login Sessions", "account/sessions": "Login Sessions",

@ -155,7 +155,8 @@ async function getCounts(userData, callerUID) {
const cids = await categories.getCidsByPrivilege('categories:cid', callerUID, 'topics:read'); const cids = await categories.getCidsByPrivilege('categories:cid', callerUID, 'topics:read');
const promises = { const promises = {
posts: db.sortedSetsCardSum(cids.map(c => `cid:${c}:uid:${uid}:pids`)), 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`)), topics: db.sortedSetsCardSum(cids.map(c => `cid:${c}:uid:${uid}:tids`)),
}; };
if (userData.isAdmin || userData.isSelf) { if (userData.isAdmin || userData.isSelf) {
@ -169,6 +170,8 @@ async function getCounts(userData, callerUID) {
promises.blocks = user.getUserField(userData.uid, 'blocksCount'); promises.blocks = user.getUserField(userData.uid, 'blocksCount');
} }
const counts = await utils.promiseParallel(promises); 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.categoriesWatched = counts.categoriesWatched && counts.categoriesWatched.length;
counts.groups = userData.groups.length; counts.groups = userData.groups.length;
counts.following = userData.followingCount; counts.following = userData.followingCount;

@ -5,6 +5,7 @@ const user = require('../../user');
const posts = require('../../posts'); const posts = require('../../posts');
const topics = require('../../topics'); const topics = require('../../topics');
const categories = require('../../categories'); const categories = require('../../categories');
const privileges = require('../../privileges');
const pagination = require('../../pagination'); const pagination = require('../../pagination');
const helpers = require('../helpers'); const helpers = require('../helpers');
const accountHelpers = require('./helpers'); const accountHelpers = require('./helpers');
@ -55,6 +56,27 @@ const templateToData = {
const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read'); const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read');
return cids.map(c => `cid:${c}:uid:${userData.uid}:pids:votes`); 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': { 'account/watched': {
type: 'topics', type: 'topics',
@ -128,6 +150,10 @@ postsController.getBestPosts = async function (req, res, next) {
await getPostsFromUserSet('account/best', 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) { postsController.getWatchedTopics = async function (req, res, next) {
await getPostsFromUserSet('account/watched', req, res, next); await getPostsFromUserSet('account/watched', req, res, next);
}; };

@ -258,7 +258,7 @@ module.exports = function (Posts) {
const topicData = await topics.getTopicFields(postData.tid, ['mainPid', 'cid', 'pinned']); const topicData = await topics.getTopicFields(postData.tid, ['mainPid', 'cid', 'pinned']);
if (postData.uid) { 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); await db.sortedSetAdd(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.votes, postData.pid);
} else { } else {
await db.sortedSetRemove(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.pid); await db.sortedSetRemove(`cid:${topicData.cid}:uid:${postData.uid}:pids:votes`, postData.pid);

@ -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/posts`, middleware, middlewares, controllers.accounts.posts.getPosts);
setupPageRoute(app, `/${name}/:userslug/topics`, middleware, middlewares, controllers.accounts.posts.getTopics); 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/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/groups`, middleware, middlewares, controllers.accounts.groups.get);
setupPageRoute(app, `/${name}/:userslug/categories`, middleware, accountMiddlewares, controllers.accounts.categories.get); setupPageRoute(app, `/${name}/:userslug/categories`, middleware, accountMiddlewares, controllers.accounts.categories.get);

Loading…
Cancel
Save