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,28 +89,22 @@ define('forum/category', [
} }
function handleLoadMoreSubcategories() { function handleLoadMoreSubcategories() {
$('[component="category/load-more-subcategories"]').on('click', function () { $('[component="category/load-more-subcategories"]').on('click', async function () {
const btn = $(this); const btn = $(this);
socket.emit('categories.loadMoreSubCategories', { const { categories: data } = await api.get(`/categories/${ajaxify.data.cid}/children?start=${ajaxify.data.nextSubCategoryStart}`);
cid: ajaxify.data.cid, btn.toggleClass('hidden', !data.length || data.length < ajaxify.data.subCategoriesPerPage);
start: ajaxify.data.nextSubCategoryStart, if (!data.length) {
}, function (err, data) { return;
if (err) { }
return alerts.error(err); app.parseAndTranslate('category', 'children', { children: data }, function (html) {
} html.find('.timeago').timeago();
btn.toggleClass('hidden', !data.length || data.length < ajaxify.data.subCategoriesPerPage); $('[component="category/subcategory/container"]').append(html);
if (!data.length) { ajaxify.data.nextSubCategoryStart += ajaxify.data.subCategoriesPerPage;
return; ajaxify.data.subCategoriesLeft -= data.length;
} btn.toggleClass('hidden', ajaxify.data.subCategoriesLeft <= 0)
app.parseAndTranslate('category', 'children', { children: data }, function (html) { .translateText('[[category:x-more-categories, ' + ajaxify.data.subCategoriesLeft + ']]');
html.find('.timeago').timeago();
$('[component="category/subcategory/container"]').append(html);
ajaxify.data.nextSubCategoryStart += ajaxify.data.subCategoriesPerPage;
ajaxify.data.subCategoriesLeft -= data.length;
btn.toggleClass('hidden', ajaxify.data.subCategoriesLeft <= 0)
.translateText('[[category:x-more-categories, ' + ajaxify.data.subCategoriesLeft + ']]');
});
}); });
return false; 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.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 }) => { categoriesAPI.setWatchState = async (caller, { cid, state, uid }) => {
let targetUid = caller.uid; let targetUid = caller.uid;
const cids = Array.isArray(cid) ? cid.map(cid => parseInt(cid, 10)) : [parseInt(cid, 10)]; 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); 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) => { Categories.setWatchState = async (req, res) => {
const { cid } = req.params; const { cid } = req.params;
let { uid, state } = req.body; 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, 'put', '/:cid', [...middlewares], controllers.write.categories.update);
setupApiRoute(router, 'delete', '/:cid', [...middlewares], controllers.write.categories.delete); setupApiRoute(router, 'delete', '/:cid', [...middlewares], controllers.write.categories.delete);
setupApiRoute(router, 'get', '/:cid/count', [...middlewares], controllers.write.categories.getTopicCount); setupApiRoute(router, 'get', '/:cid/count', [...middlewares, middleware.assert.category], controllers.write.categories.getTopicCount);
setupApiRoute(router, 'get', '/:cid/posts', [...middlewares], controllers.write.categories.getPosts); 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, 'put', '/:cid/watch', [...middlewares, middleware.assert.category], controllers.write.categories.setWatchState);
setupApiRoute(router, 'delete', '/: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) { SocketCategories.loadMoreSubCategories = async function (socket, data) {
sockets.warnDeprecated(socket, `GET /api/v3/categories/:cid/children`);
if (!data || !data.cid || !(parseInt(data.start, 10) >= 0)) { if (!data || !data.cid || !(parseInt(data.start, 10) >= 0)) {
throw new Error('[[error:invalid-data]]'); throw new Error('[[error:invalid-data]]');
} }
const allowed = await privileges.categories.can('read', data.cid, socket.uid);
if (!allowed) { const { categories: children } = await api.categories.getChildren(socket, data);
throw new Error('[[error:no-privileges]]'); return children;
}
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);
}; };
require('../promisify')(SocketCategories); require('../promisify')(SocketCategories);