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 () {
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;

View File

@@ -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) {

View File

@@ -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 () {

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.getPrivileges = async (caller, { cid }) => {

View File

@@ -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);

View File

@@ -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);

View File

@@ -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) {

View File

@@ -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'));
};