feat: add hook to modify sorting options/fields

This commit is contained in:
Barış Soner Uşaklı
2024-03-26 09:57:34 -04:00
parent 5924a6eb43
commit 168419cf24

View File

@@ -159,18 +159,24 @@ module.exports = function (Topics) {
return tids; return tids;
} }
const topicData = await Topics.getTopicsFields(tids, [ const { sortMap, fields } = await plugins.hooks.fire('filter:topics.sortOptions', {
params,
fields: [
'tid', 'timestamp', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned', 'tid', 'timestamp', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned',
]); ],
const sortMap = { sortMap: {
recent: sortRecent, recent: sortRecent,
old: sortOld, old: sortOld,
create: sortCreate, create: sortCreate,
posts: sortPopular, posts: sortPopular,
votes: sortVotes, votes: sortVotes,
views: sortViews, views: sortViews,
}; },
const sortFn = sortMap[params.sort] || sortRecent; });
console.log(params.sort);
const topicData = await Topics.getTopicsFields(tids, fields);
const sortFn = sortMap.hasOwnProperty(params.sort) && sortMap[params.sort] ?
sortMap[params.sort] : sortRecent;
if (params.floatPinned) { if (params.floatPinned) {
floatPinned(topicData, sortFn); floatPinned(topicData, sortFn);