refactor(socket.io): deprecate categories.loadMoreSubCategories in favour of api.categories.getChildren

This commit is contained in:
Julian Lam
2023-10-23 15:08:52 -04:00
parent d7c6b3d60e
commit 010727f5cb
5 changed files with 49 additions and 33 deletions

View File

@@ -89,15 +89,9 @@ define('forum/category', [
}
function handleLoadMoreSubcategories() {
$('[component="category/load-more-subcategories"]').on('click', function () {
$('[component="category/load-more-subcategories"]').on('click', async function () {
const btn = $(this);
socket.emit('categories.loadMoreSubCategories', {
cid: ajaxify.data.cid,
start: ajaxify.data.nextSubCategoryStart,
}, function (err, data) {
if (err) {
return alerts.error(err);
}
const { categories: data } = await api.get(`/categories/${ajaxify.data.cid}/children?start=${ajaxify.data.nextSubCategoryStart}`);
btn.toggleClass('hidden', !data.length || data.length < ajaxify.data.subCategoriesPerPage);
if (!data.length) {
return;
@@ -110,7 +104,7 @@ define('forum/category', [
btn.toggleClass('hidden', ajaxify.data.subCategoriesLeft <= 0)
.translateText('[[category:x-more-categories, ' + ajaxify.data.subCategoriesLeft + ']]');
});
});
return false;
});
}

View File

@@ -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)];

View File

@@ -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;

View File

@@ -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);

View File

@@ -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);