refactor(socket.io): deprecate SocketModules.chat.getUnreadCount in favour of api.chats.getUnread

This commit is contained in:
Julian Lam
2023-11-14 12:19:23 -05:00
parent 214989a8c1
commit 5eaffb422c
7 changed files with 43 additions and 4 deletions

View File

@@ -192,6 +192,8 @@ paths:
$ref: 'write/posts/pid/replies.yaml' $ref: 'write/posts/pid/replies.yaml'
/chats/: /chats/:
$ref: 'write/chats.yaml' $ref: 'write/chats.yaml'
/chats/unread:
$ref: 'write/chats/unread.yaml'
/chats/{roomId}: /chats/{roomId}:
$ref: 'write/chats/roomId.yaml' $ref: 'write/chats/roomId.yaml'
/chats/{roomId}/state: /chats/{roomId}/state:

View File

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

View File

@@ -1,8 +1,8 @@
'use strict'; 'use strict';
define('forum/header/chat', [ define('forum/header/chat', [
'components', 'hooks', 'components', 'hooks', 'api',
], function (components, hooks) { ], function (components, hooks, api) {
const chat = {}; const chat = {};
chat.prepareDOM = function () { chat.prepareDOM = function () {
@@ -44,7 +44,7 @@ define('forum/header/chat', [
chatPage.markChatPageElUnread(data); chatPage.markChatPageElUnread(data);
} }
let count = await socket.emit('modules.chats.getUnreadCount', {}); let { count } = await api.get('/chats/unread');
const chatIcon = components.get('chat/icon'); const chatIcon = components.get('chat/icon');
count = Math.max(0, count); count = Math.max(0, count);
chatIcon.toggleClass('fa-comment', count > 0) chatIcon.toggleClass('fa-comment', count > 0)

View File

@@ -79,6 +79,11 @@ chatsAPI.create = async function (caller, data) {
return await messaging.getRoomData(roomId); 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.get = async (caller, { uid, roomId }) => await messaging.loadRoom(caller.uid, { uid, roomId });
chatsAPI.post = async (caller, data) => { chatsAPI.post = async (caller, data) => {

View File

@@ -29,6 +29,9 @@ Chats.create = async (req, res) => {
helpers.formatApiResponse(200, res, roomObj); 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) => { Chats.exists = async (req, res) => {
// yes, this is fine. Room existence is checked via middleware :) // yes, this is fine. Room existence is checked via middleware :)
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);

View File

@@ -13,6 +13,8 @@ module.exports = function () {
setupApiRoute(router, 'get', '/', [...middlewares], controllers.write.chats.list); setupApiRoute(router, 'get', '/', [...middlewares], controllers.write.chats.list);
setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.create); 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, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);
setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get); 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); setupApiRoute(router, 'post', '/:roomId', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['message'])], controllers.write.chats.post);

View File

@@ -90,7 +90,10 @@ SocketModules.chats.getIP = async function (socket, mid) {
}; };
SocketModules.chats.getUnreadCount = async function (socket) { 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) { SocketModules.chats.enter = async function (socket, roomIds) {