mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-08-31 04:16:13 +02:00
refactor(socket.io): deprecate categories.loadMoreSubCategories in favour of api.categories.getChildren
This commit is contained in:
@@ -85,6 +85,27 @@ categoriesAPI.getTopicCount = async (caller, { cid }) => {
|
||||
|
||||
categoriesAPI.getPosts = async (caller, { cid }) => await categories.getRecentReplies(cid, caller.uid, 0, 4);
|
||||
|
||||
categoriesAPI.getChildren = async (caller, { cid, start }) => {
|
||||
if (!start || start < 0) {
|
||||
start = 0;
|
||||
}
|
||||
start = parseInt(start, 10);
|
||||
|
||||
const allowed = await privileges.categories.can('read', cid, caller.uid);
|
||||
if (!allowed) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
const category = await categories.getCategoryData(cid);
|
||||
await categories.getChildrenTree(category, caller.uid);
|
||||
const allCategories = [];
|
||||
categories.flattenCategories(allCategories, category.children);
|
||||
await categories.getRecentTopicReplies(allCategories, caller.uid);
|
||||
|
||||
const payload = category.children.slice(start, start + category.subCategoriesPerPage);
|
||||
return { categories: payload };
|
||||
};
|
||||
|
||||
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)];
|
||||
|
||||
@@ -45,6 +45,12 @@ Categories.getPosts = async (req, res) => {
|
||||
helpers.formatApiResponse(200, res, posts);
|
||||
};
|
||||
|
||||
Categories.getChildren = async (req, res) => {
|
||||
const { cid } = req.params;
|
||||
const { start } = req.query;
|
||||
helpers.formatApiResponse(200, res, await api.categories.getChildren(req, { cid, start }));
|
||||
};
|
||||
|
||||
Categories.setWatchState = async (req, res) => {
|
||||
const { cid } = req.params;
|
||||
let { uid, state } = req.body;
|
||||
|
||||
@@ -16,8 +16,9 @@ module.exports = function () {
|
||||
setupApiRoute(router, 'put', '/:cid', [...middlewares], controllers.write.categories.update);
|
||||
setupApiRoute(router, 'delete', '/:cid', [...middlewares], controllers.write.categories.delete);
|
||||
|
||||
setupApiRoute(router, 'get', '/:cid/count', [...middlewares], controllers.write.categories.getTopicCount);
|
||||
setupApiRoute(router, 'get', '/:cid/posts', [...middlewares], controllers.write.categories.getPosts);
|
||||
setupApiRoute(router, 'get', '/:cid/count', [...middlewares, middleware.assert.category], controllers.write.categories.getTopicCount);
|
||||
setupApiRoute(router, 'get', '/:cid/posts', [...middlewares, middleware.assert.category], controllers.write.categories.getPosts);
|
||||
setupApiRoute(router, 'get', '/:cid/children', [...middlewares, middleware.assert.category], controllers.write.categories.getChildren);
|
||||
|
||||
setupApiRoute(router, 'put', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
||||
setupApiRoute(router, 'delete', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
|
||||
|
||||
@@ -167,20 +167,14 @@ SocketCategories.isModerator = async function (socket, cid) {
|
||||
};
|
||||
|
||||
SocketCategories.loadMoreSubCategories = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, `GET /api/v3/categories/:cid/children`);
|
||||
|
||||
if (!data || !data.cid || !(parseInt(data.start, 10) >= 0)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const allowed = await privileges.categories.can('read', data.cid, socket.uid);
|
||||
if (!allowed) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const category = await categories.getCategoryData(data.cid);
|
||||
await categories.getChildrenTree(category, socket.uid);
|
||||
const allCategories = [];
|
||||
categories.flattenCategories(allCategories, category.children);
|
||||
await categories.getRecentTopicReplies(allCategories, socket.uid);
|
||||
const start = parseInt(data.start, 10);
|
||||
return category.children.slice(start, start + category.subCategoriesPerPage);
|
||||
|
||||
const { categories: children } = await api.categories.getChildren(socket, data);
|
||||
return children;
|
||||
};
|
||||
|
||||
require('../promisify')(SocketCategories);
|
||||
|
||||
Reference in New Issue
Block a user