Node.js based forum software built for the modern web
refactor(socket.io): deprecate SocketModules.chat.getUnreadCount in favour of api.chats.getUnread
Julian Lam5eaffb422ca1
diff --git a/public/openapi/write.yaml b/public/openapi/write.yamlindex af700430fa..5054499dcd 100644--- a/public/openapi/write.yaml+++ b/public/openapi/write.yaml@@ -192,6 +192,8 @@ paths:$ref: 'write/posts/pid/replies.yaml'/chats/:$ref: 'write/chats.yaml'+ /chats/unread:+ $ref: 'write/chats/unread.yaml'/chats/{roomId}:$ref: 'write/chats/roomId.yaml'/chats/{roomId}/state:diff --git a/public/openapi/write/chats/unread.yaml b/public/openapi/write/chats/unread.yamlnew file mode 100644index 0000000000..ee440665bf--- /dev/null+++ b/public/openapi/write/chats/unread.yaml@@ -0,0 +1,24 @@+get:+ tags:+ - chats+ summary: get unread count+ description: >+ This operation retrieves the calling user's count of unread chat rooms.++ Note that this API call is open-ended.+ It currently only returns the unread count, but can be expanded upon in the future.+ responses:+ '200':+ description: Count of unread chat rooms successfully retrieved.+ content:+ application/json:+ schema:+ type: object+ properties:+ status:+ $ref: ../../components/schemas/Status.yaml#/Status+ response:+ type: object+ properties:+ count:+ type: number\ No newline at end of filediff --git a/public/src/client/header/chat.js b/public/src/client/header/chat.jsindex 44f9cf4a53..a087a45939 100644--- a/public/src/client/header/chat.js+++ b/public/src/client/header/chat.js@@ -1,8 +1,8 @@'use strict';define('forum/header/chat', [- 'components', 'hooks',-], function (components, hooks) {+ 'components', 'hooks', 'api',+], function (components, hooks, api) {const chat = {};chat.prepareDOM = function () {@@ -44,7 +44,7 @@ define('forum/header/chat', [chatPage.markChatPageElUnread(data);}- let count = await socket.emit('modules.chats.getUnreadCount', {});+ let { count } = await api.get('/chats/unread');const chatIcon = components.get('chat/icon');count = Math.max(0, count);chatIcon.toggleClass('fa-comment', count > 0)diff --git a/src/api/chats.js b/src/api/chats.jsindex da47892872..91e3dbd2b4 100644--- a/src/api/chats.js+++ b/src/api/chats.js@@ -79,6 +79,11 @@ chatsAPI.create = async function (caller, data) {return await messaging.getRoomData(roomId);};+chatsAPI.getUnread = async (caller) => {+ const count = await messaging.getUnreadCount(caller.uid);+ return { count };+};+chatsAPI.get = async (caller, { uid, roomId }) => await messaging.loadRoom(caller.uid, { uid, roomId });chatsAPI.post = async (caller, data) => {diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.jsindex d962e7c1a1..87b1de3023 100644--- a/src/controllers/write/chats.js+++ b/src/controllers/write/chats.js@@ -29,6 +29,9 @@ Chats.create = async (req, res) => {helpers.formatApiResponse(200, res, roomObj);};+// currently only returns unread count, but open-ended for future additions if warranted.+Chats.getUnread = async (req, res) => helpers.formatApiResponse(200, res, await api.chats.getUnread(req));+Chats.exists = async (req, res) => {// yes, this is fine. Room existence is checked via middleware :)helpers.formatApiResponse(200, res);diff --git a/src/routes/write/chats.js b/src/routes/write/chats.jsindex bc4326a945..5a42bcdc51 100644--- a/src/routes/write/chats.js+++ b/src/routes/write/chats.js@@ -13,6 +13,8 @@ module.exports = function () {setupApiRoute(router, 'get', '/', [...middlewares], controllers.write.chats.list);setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.create);+ setupApiRoute(router, 'get', '/unread', [...middlewares], controllers.write.chats.getUnread);+setupApiRoute(router, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get);setupApiRoute(router, 'post', '/:roomId', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['message'])], controllers.write.chats.post);diff --git a/src/socket.io/modules.js b/src/socket.io/modules.jsindex a18f2d8586..e9ad965cb8 100644--- a/src/socket.io/modules.js+++ b/src/socket.io/modules.js@@ -90,7 +90,10 @@ SocketModules.chats.getIP = async function (socket, mid) {};SocketModules.chats.getUnreadCount = async function (socket) {- return await Messaging.getUnreadCount(socket.uid);+ sockets.warnDeprecated(socket, 'GET /api/v3/chats/unread');++ const { count } = await api.chats.getUnread(socket);+ return count;};SocketModules.chats.enter = async function (socket, roomIds) {