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

This commit is contained in:
Julian Lam
2023-10-19 11:26:00 -04:00
parent 96046373da
commit c442b6e662
8 changed files with 47 additions and 25 deletions

View File

@@ -109,11 +109,14 @@ define('admin/manage/category', [
callback: function () { callback: function () {
modal.find('.modal-footer button').prop('disabled', true); modal.find('.modal-footer button').prop('disabled', true);
const intervalId = setInterval(function () { const intervalId = setInterval(async () => {
socket.emit('categories.getTopicCount', ajaxify.data.category.cid, function (err, count) { if (!ajaxify.data.category) {
if (err) { // Already navigated away
return alerts.error(err); return;
} }
try {
const { count } = await api.get(`/categories/${ajaxify.data.category.cid}/count`);
let percent = 0; let percent = 0;
if (ajaxify.data.category.topic_count > 0) { if (ajaxify.data.category.topic_count > 0) {
@@ -121,16 +124,21 @@ define('admin/manage/category', [
} }
modal.find('.progress-bar').css({ width: percent + '%' }); modal.find('.progress-bar').css({ width: percent + '%' });
}); } catch (err) {
clearInterval(intervalId);
alerts.error(err);
}
}, 1000); }, 1000);
api.del('/categories/' + ajaxify.data.category.cid).then(() => { api.del('/categories/' + ajaxify.data.category.cid).then(() => {
if (intervalId) { setTimeout(() => {
clearInterval(intervalId); if (intervalId) {
} clearInterval(intervalId);
modal.modal('hide'); }
alerts.success('[[admin/manage/categories:alert.purge-success]]'); modal.modal('hide');
ajaxify.go('admin/manage/categories'); alerts.success('[[admin/manage/categories:alert.purge-success]]');
ajaxify.go('admin/manage/categories');
}, 5000);
}).catch(alerts.error); }).catch(alerts.error);
return false; return false;

View File

@@ -9,7 +9,8 @@ define('forum/category', [
'categorySelector', 'categorySelector',
'hooks', 'hooks',
'alerts', 'alerts',
], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts) { 'api',
], function (infinitescroll, share, navigator, topicList, sort, categorySelector, hooks, alerts, api) {
const Category = {}; const Category = {};
$(window).on('action:ajaxify.start', function (ev, data) { $(window).on('action:ajaxify.start', function (ev, data) {
@@ -118,14 +119,9 @@ define('forum/category', [
navigator.scrollTop(0); navigator.scrollTop(0);
}; };
Category.toBottom = function () { Category.toBottom = async () => {
socket.emit('categories.getTopicCount', ajaxify.data.cid, function (err, count) { const { count } = await api.get(`/categories/${ajaxify.data.category.cid}/count`);
if (err) { navigator.scrollBottom(count - 1);
return alerts.error(err);
}
navigator.scrollBottom(count - 1);
});
}; };
function loadTopicsAfter(after, direction, callback) { function loadTopicsAfter(after, direction, callback) {

View File

@@ -111,8 +111,8 @@ app = window.app || {};
alerts.alert(params); alerts.alert(params);
}); });
}); });
socket.on('event:deprecated_call', function (data) { socket.on('event:deprecated_call', (data) => {
console.warn('[socket.io] ', data.eventName, 'is now deprecated in favour of', data.replacement); 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 () { socket.on('event:livereload', function () {

View File

@@ -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.getPosts = async (caller, { cid }) => await categories.getRecentReplies(cid, caller.uid, 0, 4);
categoriesAPI.getPrivileges = async (caller, { cid }) => { categoriesAPI.getPrivileges = async (caller, { cid }) => {

View File

@@ -35,6 +35,10 @@ Categories.delete = async (req, res) => {
helpers.formatApiResponse(200, 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) => { Categories.getPosts = async (req, res) => {
const posts = await api.categories.getPosts(req, { ...req.params }); const posts = await api.categories.getPosts(req, { ...req.params });
helpers.formatApiResponse(200, res, posts); helpers.formatApiResponse(200, res, posts);

View File

@@ -16,6 +16,7 @@ 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/posts', [...middlewares], controllers.write.categories.getPosts); setupApiRoute(router, 'get', '/:cid/posts', [...middlewares], controllers.write.categories.getPosts);
setupApiRoute(router, 'get', '/:cid/privileges', [...middlewares], controllers.write.categories.getPrivileges); setupApiRoute(router, 'get', '/:cid/privileges', [...middlewares], controllers.write.categories.getPrivileges);

View File

@@ -24,6 +24,8 @@ SocketCategories.get = async function (socket) {
}; };
SocketCategories.getWatchedCategories = async function (socket) { SocketCategories.getWatchedCategories = async function (socket) {
sockets.warnDeprecated(socket);
const [categoriesData, ignoredCids] = await Promise.all([ const [categoriesData, ignoredCids] = await Promise.all([
categories.getCategoriesByPrivilege('cid:0:children', socket.uid, 'find'), categories.getCategoriesByPrivilege('cid:0:children', socket.uid, 'find'),
user.getIgnoredCategories(socket.uid), user.getIgnoredCategories(socket.uid),
@@ -81,7 +83,9 @@ SocketCategories.loadMore = async function (socket, data) {
}; };
SocketCategories.getTopicCount = async function (socket, cid) { 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) { SocketCategories.getCategoriesByPrivilege = async function (socket, privilege) {

View File

@@ -333,5 +333,9 @@ Sockets.warnDeprecated = (socket, replacement) => {
replacement: 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'));
}; };