diff --git a/public/src/admin/manage/categories.js b/public/src/admin/manage/categories.js index ad1d8465398c..4e436410dfc8 100644 --- a/public/src/admin/manage/categories.js +++ b/public/src/admin/manage/categories.js @@ -162,7 +162,7 @@ define('admin/manage/categories', [ }; Categories.create = function (payload) { - socket.emit('admin.categories.create', payload, function (err, data) { + api.post('/categories', payload, function (err, data) { if (err) { return app.alertError(err.message); } diff --git a/src/admin/search.js b/src/admin/search.js index 301e14f0e5db..d4cbe287b5e5 100644 --- a/src/admin/search.js +++ b/src/admin/search.js @@ -131,7 +131,6 @@ async function buildNamespace(language, namespace) { const cache = {}; async function getDictionary(language) { - console.log('get dictionary', language); if (cache[language]) { return cache[language]; } diff --git a/src/api/categories.js b/src/api/categories.js new file mode 100644 index 000000000000..673155b43606 --- /dev/null +++ b/src/api/categories.js @@ -0,0 +1,28 @@ +'use strict'; + +const categories = require('../categories'); +const events = require('../events'); + +const categoriesAPI = module.exports; + +categoriesAPI.create = async function (caller, data) { + const response = await categories.create(data); + const categoryObjs = await categories.getCategories([response.cid], caller.uid); + return categoryObjs[0]; +}; + +categoriesAPI.update = async function (caller, data) { + await categories.update(data); +}; + +categoriesAPI.delete = async function (caller, data) { + const name = await categories.getCategoryField(data.cid, 'name'); + await categories.purge(data.cid, caller.uid); + await events.log({ + type: 'category-purge', + uid: caller.uid, + ip: caller.ip, + cid: data.cid, + name: name, + }); +}; diff --git a/src/api/index.js b/src/api/index.js index 56961e1c9bf2..592092d0482c 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -4,4 +4,5 @@ module.exports = { users: require('./users'), groups: require('./groups'), topics: require('./topics'), + categories: require('./categories'), }; diff --git a/src/controllers/write/categories.js b/src/controllers/write/categories.js index dcd0f862ec60..05ba627ebca1 100644 --- a/src/controllers/write/categories.js +++ b/src/controllers/write/categories.js @@ -1,37 +1,26 @@ 'use strict'; const categories = require('../../categories'); -const events = require('../../events'); +const api = require('../../api'); const helpers = require('../helpers'); const Categories = module.exports; Categories.create = async (req, res) => { - const response = await categories.create(req.body); - const categoryObjs = await categories.getCategories([response.cid]); - helpers.formatApiResponse(200, res, categoryObjs[0]); + const response = await api.categories.create(req, req.body); + helpers.formatApiResponse(200, res, response); }; Categories.update = async (req, res) => { const payload = {}; payload[req.params.cid] = req.body; - - await categories.update(payload); + await api.categories.update(req, payload); const categoryObjs = await categories.getCategories([req.params.cid]); helpers.formatApiResponse(200, res, categoryObjs[0]); }; Categories.delete = async (req, res) => { - const name = await categories.getCategoryField(req.params.cid, 'name'); - await categories.purge(req.params.cid, req.user.uid); - await events.log({ - type: 'category-purge', - uid: req.user.uid, - ip: req.ip, - cid: req.params.cid, - name: name, - }); - + await api.categories.delete(req, { cid: req.params.cid }); helpers.formatApiResponse(200, res); }; diff --git a/src/socket.io/admin/categories.js b/src/socket.io/admin/categories.js index b12882c849ba..fa5093976845 100644 --- a/src/socket.io/admin/categories.js +++ b/src/socket.io/admin/categories.js @@ -8,6 +8,7 @@ const categories = require('../../categories'); const privileges = require('../../privileges'); const plugins = require('../../plugins'); const events = require('../../events'); +const api = require('../../api'); const sockets = require('..'); const Categories = module.exports; @@ -18,8 +19,7 @@ Categories.create = async function (socket, data) { if (!data) { throw new Error('[[error:invalid-data]]'); } - - return await categories.create(data); + return await api.categories.create(socket, data); }; // DEPRECATED: @1.14.3, remove in version >=1.16 @@ -42,15 +42,7 @@ Categories.getNames = async function () { Categories.purge = async function (socket, cid) { sockets.warnDeprecated(socket, 'DELETE /api/v3/categories/:cid'); - const name = await categories.getCategoryField(cid, 'name'); - await categories.purge(cid, socket.uid); - await events.log({ - type: 'category-purge', - uid: socket.uid, - ip: socket.ip, - cid: cid, - name: name, - }); + await api.categories.delete(socket, { cid: cid }); }; Categories.update = async function (socket, data) { @@ -59,8 +51,7 @@ Categories.update = async function (socket, data) { if (!data) { throw new Error('[[error:invalid-data]]'); } - - return await categories.update(data); + return await api.categories.update(socket, data); }; Categories.setPrivilege = async function (socket, data) {