refactor(socket.io): deprecate SocketModules.chats.typing in favour of api.chats.toggleTyping

This commit is contained in:
Julian Lam
2023-11-17 14:10:29 -05:00
parent 401e8636bd
commit c1e6be7705
9 changed files with 86 additions and 18 deletions

View File

@@ -10,7 +10,9 @@ const messaging = require('../messaging');
const notifications = require('../notifications');
const privileges = require('../privileges');
const plugins = require('../plugins');
const utils = require('../utils');
const websockets = require('../socket.io');
const socketHelpers = require('../socket.io/helpers');
const chatsAPI = module.exports;
@@ -207,6 +209,27 @@ chatsAPI.watch = async (caller, { roomId, state }) => {
await messaging.setUserNotificationSetting(caller.uid, roomId, state);
};
chatsAPI.toggleTyping = async (caller, { roomId, typing }) => {
if (!utils.isNumber(roomId) || typeof typing !== 'boolean') {
throw new Error('[[error:invalid-data]]');
}
const [isInRoom, username] = await Promise.all([
messaging.isUserInRoom(caller.uid, roomId),
user.getUserField(caller.uid, 'username'),
]);
if (!isInRoom) {
throw new Error('[[error:no-privileges]]');
}
websockets.in(`chat_room_${roomId}`).emit('event:chats.typing', {
uid: caller.uid,
roomId,
typing,
username,
});
};
chatsAPI.users = async (caller, data) => {
const start = data.hasOwnProperty('start') ? data.start : 0;
const stop = start + 39;