Node.js based forum software built for the modern web
refactor(socket.io): deprecate categories.get in favour of api.categories.list
Julian Lam96046373da82
diff --git a/src/api/categories.js b/src/api/categories.jsindex 394db8f95d..47cb0ca8f6 100644--- a/src/api/categories.js+++ b/src/api/categories.js@@ -15,6 +15,22 @@ const hasAdminPrivilege = async (uid, privilege = 'categories') => {}};+categoriesAPI.list = async (caller) => {+ async function getCategories() {+ const cids = await categories.getCidsByPrivilege('categories:cid', caller.uid, 'find');+ return await categories.getCategoriesData(cids);+ }++ const [isAdmin, categoriesData] = await Promise.all([+ user.isAdministrator(caller.uid),+ getCategories(),+ ]);++ return {+ categories: categoriesData.filter(category => category && (!category.disabled || isAdmin)),+ };+};+categoriesAPI.get = async function (caller, data) {const [userPrivileges, category] = await Promise.all([privileges.categories.get(data.cid, caller.uid),diff --git a/src/controllers/write/categories.js b/src/controllers/write/categories.jsindex 714816ef9f..52408a5400 100644--- a/src/controllers/write/categories.js+++ b/src/controllers/write/categories.js@@ -7,6 +7,10 @@ const helpers = require('../helpers');const Categories = module.exports;+Categories.list = async (req, res) => {+ helpers.formatApiResponse(200, res, await api.categories.list(req));+};+Categories.get = async (req, res) => {helpers.formatApiResponse(200, res, await api.categories.get(req, req.params));};diff --git a/src/routes/write/categories.js b/src/routes/write/categories.jsindex 3b96556d20..35957b440b 100644--- a/src/routes/write/categories.js+++ b/src/routes/write/categories.js@@ -10,6 +10,7 @@ const { setupApiRoute } = routeHelpers;module.exports = function () {const middlewares = [middleware.ensureLoggedIn];+ setupApiRoute(router, 'get', '/', [...middlewares], controllers.write.categories.list);setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['name'])], controllers.write.categories.create);setupApiRoute(router, 'get', '/:cid', [], controllers.write.categories.get);setupApiRoute(router, 'put', '/:cid', [...middlewares], controllers.write.categories.update);diff --git a/src/socket.io/categories.js b/src/socket.io/categories.jsindex 1db5358354..8168176656 100644--- a/src/socket.io/categories.js+++ b/src/socket.io/categories.js@@ -18,15 +18,9 @@ SocketCategories.getRecentReplies = async function (socket, cid) {};SocketCategories.get = async function (socket) {- async function getCategories() {- const cids = await categories.getCidsByPrivilege('categories:cid', socket.uid, 'find');- return await categories.getCategoriesData(cids);- }- const [isAdmin, categoriesData] = await Promise.all([- user.isAdministrator(socket.uid),- getCategories(),- ]);- return categoriesData.filter(category => category && (!category.disabled || isAdmin));+ sockets.warnDeprecated(socket, 'GET /api/v3/categories');+ const { categories } = await api.categories.list(socket);+ return categories;};SocketCategories.getWatchedCategories = async function (socket) {