feat: sorting an filtering in /world

This commit is contained in:
Opliko
2024-03-28 17:07:34 +01:00
parent a3dce46371
commit d1fa6a596b
3 changed files with 55 additions and 11 deletions

View File

@@ -1,13 +1,46 @@
'use strict';
define('forum/world', ['topicList'], function (topicList) {
define('forum/world', ['topicList', 'sort', 'hooks', 'alerts', 'api'], function (topicList, sort, hooks, alerts, api) {
const World = {};
World.init = function () {
app.enterRoom('world');
topicList.init('world');
sort.handleSort('categoryTopicSort', 'world');
handleIgnoreWatch(-1);
hooks.fire('action:topics.loaded', { topics: ajaxify.data.topics });
hooks.fire('action:category.loaded', { cid: ajaxify.data.cid });
};
function handleIgnoreWatch(cid) {
$('[component="category/watching"], [component="category/tracking"], [component="category/ignoring"], [component="category/notwatching"]').on('click', function () {
const $this = $(this);
const state = $this.attr('data-state');
api.put(`/categories/${cid}/watch`, { state }, (err) => {
if (err) {
return alerts.error(err);
}
$('[component="category/watching/menu"]').toggleClass('hidden', state !== 'watching');
$('[component="category/watching/check"]').toggleClass('fa-check', state === 'watching');
$('[component="category/tracking/menu"]').toggleClass('hidden', state !== 'tracking');
$('[component="category/tracking/check"]').toggleClass('fa-check', state === 'tracking');
$('[component="category/notwatching/menu"]').toggleClass('hidden', state !== 'notwatching');
$('[component="category/notwatching/check"]').toggleClass('fa-check', state === 'notwatching');
$('[component="category/ignoring/menu"]').toggleClass('hidden', state !== 'ignoring');
$('[component="category/ignoring/check"]').toggleClass('fa-check', state === 'ignoring');
alerts.success('[[category:' + state + '.message]]');
});
});
}
return World;
});

View File

@@ -144,7 +144,7 @@ define('tagFilter', ['hooks', 'alerts', 'bootstrap'], function (hooks, alerts, b
function loadList(query, callback) {
let cids = null;
if (ajaxify.data.template.category) {
if (ajaxify.data.template.category || ajaxify.data.template.world) {
cids = [ajaxify.data.cid];
// selectedCids is avaiable on /recent, /unread, /popular etc.
} else if (Array.isArray(ajaxify.data.selectedCids) && ajaxify.data.selectedCids.length) {

View File

@@ -26,7 +26,23 @@ controller.list = async function (req, res) {
const start = Math.max(0, (page - 1) * topicsPerPage);
const stop = start + topicsPerPage - 1;
const sets = ['cid:-1:tids', `uid:${req.uid}:inbox`];
const sortToSet = {
recently_replied: `cid:-1:tids`,
recently_created: `cid:-1:tids:create`,
most_posts: `cid:-1:tids:posts`,
most_votes: `cid:-1:tids:votes`,
most_views: `cid:-1:tids:views`,
};
const [userPrivileges, tagData, userSettings, rssToken] = await Promise.all([
privileges.categories.get('-1', req.uid),
helpers.getSelectedTag(req.query.tag),
user.getSettings(req.uid),
user.auth.getFeedToken(req.uid),
]);
const sort = validSorts.includes(req.query.sort) ? req.query.sort : userSettings.categoryTopicSort;
const sets = [sortToSet[sort], `uid:${req.uid}:inbox`];
if (req.params.filter === 'all' || !req.uid) {
sets.pop();
} else if (req.params.filter) {
@@ -40,14 +56,7 @@ controller.list = async function (req, res) {
weights: sets.map((s, index) => (index ? 0 : 1)),
});
const [userPrivileges, tagData, userSettings, rssToken] = await Promise.all([
privileges.categories.get('-1', req.uid),
helpers.getSelectedTag(req.query.tag),
user.getSettings(req.uid),
user.auth.getFeedToken(req.uid),
]);
const targetUid = await user.getUidByUserslug(req.query.author);
const sort = validSorts.includes(req.query.sort) ? req.query.sort : userSettings.categoryTopicSort;
const data = await categories.getCategoryById({
uid: req.uid,
@@ -68,6 +77,8 @@ controller.list = async function (req, res) {
data.title = translator.escape(data.name);
data.privileges = userPrivileges;
data.selectedTag = tagData.selectedTag;
data.selectedTags = tagData.selectedTags;
data.breadcrumbs = helpers.buildBreadcrumbs([{ text: `[[pages:world]]` }]);
data['feeds:disableRSS'] = meta.config['feeds:disableRSS'] || 0;