Skip to content

Node.js based forum software built for the modern web

feat: additional sorting options for flags

Julian Lam0c203517025a
diff --git a/install/package.json b/install/package.json
index bac350f1c2..65d0c80fba 100644
--- a/install/package.json
+++ b/install/package.json
@@ -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",
@@ -172,4 +172,4 @@
"url": "https://github.com/barisusakli"
}
]
-}
\ No newline at end of file
+}
diff --git a/public/language/en-GB/flags.json b/public/language/en-GB/flags.json
index 4eeafeeb67..10698842cc 100644
--- a/public/language/en-GB/flags.json
+++ b/public/language/en-GB/flags.json
@@ -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.",
diff --git a/src/controllers/mods.js b/src/controllers/mods.js
index f1fd83a226..88befcb4be 100644
--- a/src/controllers/mods.js
+++ b/src/controllers/mods.js
@@ -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) {
diff --git a/src/flags.js b/src/flags.js
index 9a153b3d6a..3292cdd946 100644
--- a/src/flags.js
+++ b/src/flags.js
@@ -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':
@@ -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;
};