refactor(socket.io): deprecate categories.loadMore in favour of api.categories.getTopics

This commit is contained in:
Julian Lam
2023-10-26 16:51:46 -04:00
parent 5399e86af1
commit 1ce4ca54da
8 changed files with 135 additions and 39 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
const meta = require('../meta');
const categories = require('../categories');
const topics = require('../topics');
const events = require('../events');
@@ -106,6 +107,47 @@ categoriesAPI.getChildren = async (caller, { cid, start }) => {
return { categories: payload };
};
categoriesAPI.getTopics = async (caller, data) => {
data.query = data.query || {};
const [userPrivileges, settings, targetUid] = await Promise.all([
privileges.categories.get(data.cid, caller.uid),
user.getSettings(caller.uid),
user.getUidByUserslug(data.query.author),
]);
if (!userPrivileges.read) {
throw new Error('[[error:no-privileges]]');
}
const infScrollTopicsPerPage = 20;
const sort = data.sort || data.categoryTopicSort || meta.config.categoryTopicSort || 'newest_to_oldest';
let start = Math.max(0, parseInt(data.after || 0, 10));
if (data.direction === -1) {
start -= infScrollTopicsPerPage;
}
let stop = start + infScrollTopicsPerPage - 1;
start = Math.max(0, start);
stop = Math.max(0, stop);
const result = await categories.getCategoryTopics({
uid: caller.uid,
cid: data.cid,
start,
stop,
sort,
settings,
query: data.query,
tag: data.query.tag,
targetUid,
});
categories.modifyTopicsByPrivilege(result.topics, userPrivileges);
return { ...result, privileges: userPrivileges };
};
categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
let targetUid = caller.uid;
const cids = Array.isArray(cid) ? cid.map(cid => parseInt(cid, 10)) : [parseInt(cid, 10)];