Skip to content

Commit

Permalink
feat: additional sorting options for flags
Browse files Browse the repository at this point in the history
+ upvotes, +downvotes, +replies
  • Loading branch information
julianlam committed Aug 31, 2020
1 parent 27426c0 commit 0c20351
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions install/package.json
Expand Up @@ -90,9 +90,9 @@
"nodebb-plugin-spam-be-gone": "0.7.2",
"nodebb-rewards-essentials": "0.1.3",
"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-vanilla": "11.2.5",
"nodebb-theme-vanilla": "11.2.6",
"nodebb-widget-essentials": "4.1.1",
"nodemailer": "^6.4.6",
"passport": "^0.4.1",
Expand Down Expand Up @@ -172,4 +172,4 @@
"url": "https://github.com/barisusakli"
}
]
}
}
5 changes: 5 additions & 0 deletions public/language/en-GB/flags.json
Expand Up @@ -61,6 +61,11 @@
"sort-newest": "Newest first",
"sort-oldest": "Oldest first",
"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-body": "Please specify your reason for flagging %1 %2 for review. Alternatively, use one of the quick report buttons if applicable.",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/mods.js
Expand Up @@ -19,7 +19,7 @@ modsController.flags = {};

modsController.flags.list = async function (req, res, next) {
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
if (parseInt(req.query.reset, 10) === 1) {
Expand Down
22 changes: 22 additions & 0 deletions src/flags.js
Expand Up @@ -191,6 +191,12 @@ Flags.list = async function (data) {
};

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) {
// 'newest' is not handled because that is default
case 'oldest':
Expand All @@ -209,7 +215,23 @@ Flags.sort = async function (flagIds, sort) {
flagIds = mapped.map(obj => flagIds[obj.index]);
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;
};

Expand Down

0 comments on commit 0c20351

Please sign in to comment.