Node.js based forum software built for the modern web
refactor(socket.io): deprecate categories.getTopicCount in favour of api.categories.getTopicCount
Julian Lamc442b6e662a8
diff --git a/public/src/admin/manage/category.js b/public/src/admin/manage/category.jsindex 9bbc9c47df..b6255fa251 100644--- a/public/src/admin/manage/category.js+++ b/public/src/admin/manage/category.js@@ -109,11 +109,14 @@ define('admin/manage/category', [callback: function () {modal.find('.modal-footer button').prop('disabled', true);- const intervalId = setInterval(function () {- socket.emit('categories.getTopicCount', ajaxify.data.category.cid, function (err, count) {- if (err) {- return alerts.error(err);- }+ const intervalId = setInterval(async () => {+ if (!ajaxify.data.category) {+ // Already navigated away+ return;+ }++ try {+ const { count } = await api.get(`/categories/${ajaxify.data.category.cid}/count`);let percent = 0;if (ajaxify.data.category.topic_count > 0) {@@ -121,16 +124,21 @@ define('admin/manage/category', [}modal.find('.progress-bar').css({ width: percent + '%' });- });+ } catch (err) {+ clearInterval(intervalId);+ alerts.error(err);+ }}, 1000);api.del('/categories/' + ajaxify.data.category.cid).then(() => {- if (intervalId) {- clearInterval(intervalId);- }- modal.modal('hide');- alerts.success('[[admin/manage/categories:alert.purge-success]]');- ajaxify.go('admin/manage/categories');+ setTimeout(() => {+ if (intervalId) {+ clearInterval(intervalId);+ }+ modal.modal('hide');+ alerts.success('[[admin/manage/categories:alert.purge-success]]');+ ajaxify.go('admin/manage/categories');+ }, 5000);}).catch(alerts.error);return false;diff --git a/public/src/client/category.js b/public/src/client/category.jsindex a69ef64616..a89620cb34 100644--- a/public/src/client/category.js+++ b/public/src/client/category.js@@ -9,7 +9,8 @@ define('forum/category', ['categorySelector','hooks','alerts',-], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts) {+ 'api',+], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts, api) {const Category = {};$(window).on('action:ajaxify.start', function (ev, data) {@@ -118,14 +119,9 @@ define('forum/category', [navigator.scrollTop(0);};- Category.toBottom = function () {- socket.emit('categories.getTopicCount', ajaxify.data.cid, function (err, count) {- if (err) {- return alerts.error(err);- }-- navigator.scrollBottom(count - 1);- });+ Category.toBottom = async () => {+ const { count } = await api.get(`/categories/${ajaxify.data.category.cid}/count`);+ navigator.scrollBottom(count - 1);};function loadTopicsAfter(after, direction, callback) {diff --git a/public/src/sockets.js b/public/src/sockets.jsindex 1a87e57646..e4ef8273e1 100644--- a/public/src/sockets.js+++ b/public/src/sockets.js@@ -111,8 +111,8 @@ app = window.app || {};alerts.alert(params);});});- socket.on('event:deprecated_call', function (data) {- console.warn('[socket.io] ', data.eventName, 'is now deprecated in favour of', data.replacement);+ socket.on('event:deprecated_call', (data) => {+ console.warn('[socket.io]', data.eventName, 'is now deprecated', data.replacement ? `in favour of ${data.replacement}` : 'with no alternative planned.');});socket.on('event:livereload', function () {diff --git a/src/api/categories.js b/src/api/categories.jsindex 47cb0ca8f6..28b2539a3a 100644--- a/src/api/categories.js+++ b/src/api/categories.js@@ -77,6 +77,11 @@ categoriesAPI.delete = async function (caller, { cid }) {});};+categoriesAPI.getTopicCount = async (caller, { cid }) => {+ const count = await categories.getCategoryField(cid, 'topic_count');+ return { count };+};+categoriesAPI.getPosts = async (caller, { cid }) => await categories.getRecentReplies(cid, caller.uid, 0, 4);categoriesAPI.getPrivileges = async (caller, { cid }) => {diff --git a/src/controllers/write/categories.js b/src/controllers/write/categories.jsindex 52408a5400..8d4c0aebcf 100644--- a/src/controllers/write/categories.js+++ b/src/controllers/write/categories.js@@ -35,6 +35,10 @@ Categories.delete = async (req, res) => {helpers.formatApiResponse(200, res);};+Categories.getTopicCount = async (req, res) => {+ helpers.formatApiResponse(200, res, await api.categories.getTopicCount(req, { ...req.params }));+};+Categories.getPosts = async (req, res) => {const posts = await api.categories.getPosts(req, { ...req.params });helpers.formatApiResponse(200, res, posts);diff --git a/src/routes/write/categories.js b/src/routes/write/categories.jsindex 35957b440b..44a3020863 100644--- a/src/routes/write/categories.js+++ b/src/routes/write/categories.js@@ -16,6 +16,7 @@ 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/privileges', [...middlewares], controllers.write.categories.getPrivileges);diff --git a/src/socket.io/categories.js b/src/socket.io/categories.jsindex 8168176656..126bf5e7d6 100644--- a/src/socket.io/categories.js+++ b/src/socket.io/categories.js@@ -24,6 +24,8 @@ SocketCategories.get = async function (socket) {};SocketCategories.getWatchedCategories = async function (socket) {+ sockets.warnDeprecated(socket);+const [categoriesData, ignoredCids] = await Promise.all([categories.getCategoriesByPrivilege('cid:0:children', socket.uid, 'find'),user.getIgnoredCategories(socket.uid),@@ -81,7 +83,9 @@ SocketCategories.loadMore = async function (socket, data) {};SocketCategories.getTopicCount = async function (socket, cid) {- return await categories.getCategoryField(cid, 'topic_count');+ sockets.warnDeprecated(socket, 'GET /api/v3/categories/:cid');+ const { count } = await api.categories.getTopicCount(socket, { cid });+ return count;};SocketCategories.getCategoriesByPrivilege = async function (socket, privilege) {diff --git a/src/socket.io/index.js b/src/socket.io/index.jsindex 8f03eb2a9d..c10f271585 100644--- a/src/socket.io/index.js+++ b/src/socket.io/index.js@@ -333,5 +333,9 @@ Sockets.warnDeprecated = (socket, replacement) => {replacement: replacement,});}- winston.warn(`[deprecated]\n ${new Error('-').stack.split('\n').slice(2, 5).join('\n')}\n use ${replacement}`);+ winston.warn([+ '[deprecated]',+ `${new Error('-').stack.split('\n').slice(2, 5).join('\n')}`,+ ` ${replacement ? `use ${replacement}` : 'there is no replacement for this call.'}`,+ ].join('\n'));};