feat: add filter:categories.getRecentTopicReplies

pass req.query to getRecentTopicReplies
v1.18.x
Barış Soner Uşaklı 5 years ago
parent 7e42988489
commit aad0880f7c

@ -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) { if (!Array.isArray(categoryData) || !categoryData.length) {
return; return;
} }
const categoriesToLoad = categoryData.filter(category => category && category.numRecentReplies && parseInt(category.numRecentReplies, 10) > 0); const categoriesToLoad = categoryData.filter(c => c && c.numRecentReplies && parseInt(c.numRecentReplies, 10) > 0);
const keys = categoriesToLoad.map(category => 'cid:' + category.cid + ':recent_tids'); 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); const results = await db.getSortedSetsMembers(keys);
let tids = _.uniq(_.flatten(results).filter(Boolean)); let tids = _.uniq(_.flatten(results).filter(Boolean));

@ -19,7 +19,7 @@ categoriesController.list = async function (req, res) {
const categoryData = await categories.getCategoriesByPrivilege('categories:cid', req.uid, 'find'); const categoryData = await categories.getCategoriesByPrivilege('categories:cid', req.uid, 'find');
const tree = categories.getTree(categoryData, 0); const tree = categories.getTree(categoryData, 0);
await categories.getRecentTopicReplies(categoryData, req.uid); await categories.getRecentTopicReplies(categoryData, req.uid, req.query);
const data = { const data = {
title: meta.config.homePageTitle || '[[pages:home]]', title: meta.config.homePageTitle || '[[pages:home]]',

@ -88,7 +88,7 @@ categoryController.get = async function (req, res, next) {
if (categoryData.children.length) { if (categoryData.children.length) {
const allCategories = []; const allCategories = [];
categories.flattenCategories(allCategories, categoryData.children); 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); categoryData.title = translator.escape(categoryData.name);

@ -109,7 +109,7 @@ describe('Categories', function () {
uid: 0, uid: 0,
}, function (err, categoryData) { }, function (err, categoryData) {
assert.ifError(err); assert.ifError(err);
Categories.getRecentTopicReplies(categoryData, 0, function (err) { Categories.getRecentTopicReplies(categoryData, 0, {}, function (err) {
assert.ifError(err); assert.ifError(err);
done(); done();
}); });

Loading…
Cancel
Save