From aad0880f7c5ba3267bd9cec94f1a6b8dcc27254d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 8 May 2020 22:36:16 -0400 Subject: [PATCH] feat: add filter:categories.getRecentTopicReplies pass req.query to getRecentTopicReplies --- src/categories/recentreplies.js | 18 +++++++++++++++--- src/controllers/categories.js | 2 +- src/controllers/category.js | 2 +- test/categories.js | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 4ae423f6ac..3a7e37d383 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -62,12 +62,24 @@ module.exports = function (Categories) { } }; - Categories.getRecentTopicReplies = async function (categoryData, uid) { + Categories.getRecentTopicReplies = async function (categoryData, uid, query) { if (!Array.isArray(categoryData) || !categoryData.length) { return; } - const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0); - const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids'); + const categoriesToLoad = categoryData.filter(c => c && c.numRecentReplies && parseInt(c.numRecentReplies, 10) > 0); + let keys = []; + if (plugins.hasListeners('filter:categories.getRecentTopicReplies')) { + const result = await plugins.fireHook('filter:categories.getRecentTopicReplies', { + categories: categoriesToLoad, + uid: uid, + query: query, + keys: [], + }); + keys = result.keys; + } else { + keys = categoriesToLoad.map(c => 'cid:' + c.cid + ':recent_tids'); + } + const results = await db.getSortedSetsMembers(keys); let tids = _.uniq(_.flatten(results).filter(Boolean)); diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 22f4964a74..98dae31fa5 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -19,7 +19,7 @@ categoriesController.list = async function (req, res) { const categoryData = await categories.getCategoriesByPrivilege('categories:cid', req.uid, 'find'); const tree = categories.getTree(categoryData, 0); - await categories.getRecentTopicReplies(categoryData, req.uid); + await categories.getRecentTopicReplies(categoryData, req.uid, req.query); const data = { title: meta.config.homePageTitle || '[[pages:home]]', diff --git a/src/controllers/category.js b/src/controllers/category.js index 4edc7324d6..befc2621cb 100644 --- a/src/controllers/category.js +++ b/src/controllers/category.js @@ -88,7 +88,7 @@ categoryController.get = async function (req, res, next) { if (categoryData.children.length) { const allCategories = []; categories.flattenCategories(allCategories, categoryData.children); - await categories.getRecentTopicReplies(allCategories, req.uid); + await categories.getRecentTopicReplies(allCategories, req.uid, req.query); } categoryData.title = translator.escape(categoryData.name); diff --git a/test/categories.js b/test/categories.js index a0d3f778d2..82cf43acf1 100644 --- a/test/categories.js +++ b/test/categories.js @@ -109,7 +109,7 @@ describe('Categories', function () { uid: 0, }, function (err, categoryData) { assert.ifError(err); - Categories.getRecentTopicReplies(categoryData, 0, function (err) { + Categories.getRecentTopicReplies(categoryData, 0, {}, function (err) { assert.ifError(err); done(); });