feat: additional sorting options for flags

+ upvotes, +downvotes, +replies
v1.18.x
Julian Lam 5 years ago
parent 27426c0629
commit 0c20351702

@ -90,9 +90,9 @@
"nodebb-plugin-spam-be-gone": "0.7.2", "nodebb-plugin-spam-be-gone": "0.7.2",
"nodebb-rewards-essentials": "0.1.3", "nodebb-rewards-essentials": "0.1.3",
"nodebb-theme-lavender": "5.1.0", "nodebb-theme-lavender": "5.1.0",
"nodebb-theme-persona": "10.2.10", "nodebb-theme-persona": "10.2.11",
"nodebb-theme-slick": "1.2.29", "nodebb-theme-slick": "1.2.29",
"nodebb-theme-vanilla": "11.2.5", "nodebb-theme-vanilla": "11.2.6",
"nodebb-widget-essentials": "4.1.1", "nodebb-widget-essentials": "4.1.1",
"nodemailer": "^6.4.6", "nodemailer": "^6.4.6",
"passport": "^0.4.1", "passport": "^0.4.1",
@ -172,4 +172,4 @@
"url": "https://github.com/barisusakli" "url": "https://github.com/barisusakli"
} }
] ]
} }

@ -61,6 +61,11 @@
"sort-newest": "Newest first", "sort-newest": "Newest first",
"sort-oldest": "Oldest first", "sort-oldest": "Oldest first",
"sort-reports": "Most reports", "sort-reports": "Most reports",
"sort-all": "All flag types...",
"sort-posts-only": "Posts only...",
"sort-downvotes": "Most downvotes",
"sort-upvotes": "Most upvotes",
"sort-replies": "Most replies",
"modal-title": "Report Inappropriate Content", "modal-title": "Report Inappropriate Content",
"modal-body": "Please specify your reason for flagging %1 %2 for review. Alternatively, use one of the quick report buttons if applicable.", "modal-body": "Please specify your reason for flagging %1 %2 for review. Alternatively, use one of the quick report buttons if applicable.",

@ -19,7 +19,7 @@ modsController.flags = {};
modsController.flags.list = async function (req, res, next) { modsController.flags.list = async function (req, res, next) {
const validFilters = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick', 'page', 'perPage']; const validFilters = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick', 'page', 'perPage'];
const validSorts = ['newest', 'oldest', 'reports']; const validSorts = ['newest', 'oldest', 'reports', 'upvotes', 'downvotes', 'replies'];
// Reset filters if explicitly requested // Reset filters if explicitly requested
if (parseInt(req.query.reset, 10) === 1) { if (parseInt(req.query.reset, 10) === 1) {

@ -191,6 +191,12 @@ Flags.list = async function (data) {
}; };
Flags.sort = async function (flagIds, sort) { Flags.sort = async function (flagIds, sort) {
const filterPosts = async (flagIds) => {
const keys = flagIds.map(id => `flag:${id}`);
const types = await db.getObjectsFields(keys, ['type']);
return flagIds.filter((id, idx) => types[idx].type === 'post');
};
switch (sort) { switch (sort) {
// 'newest' is not handled because that is default // 'newest' is not handled because that is default
case 'oldest': case 'oldest':
@ -209,7 +215,23 @@ Flags.sort = async function (flagIds, sort) {
flagIds = mapped.map(obj => flagIds[obj.index]); flagIds = mapped.map(obj => flagIds[obj.index]);
break; break;
} }
case 'upvotes': // fall-through
case 'downvotes':
case 'replies': {
flagIds = await filterPosts(flagIds);
const keys = flagIds.map(id => `flag:${id}`);
const pids = (await db.getObjectsFields(keys, ['targetId'])).map(obj => obj.targetId);
const votes = (await posts.getPostsFields(pids, [sort])).map(obj => parseInt(obj[sort], 10) || 0);
const sortRef = flagIds.reduce((memo, cur, idx) => {
memo[cur] = votes[idx];
return memo;
}, {});
flagIds = flagIds.sort((a, b) => sortRef[b] - sortRef[a]);
}
} }
return flagIds; return flagIds;
}; };

Loading…
Cancel
Save