refactor(socket.io): deprecate SocketModules.chats.setNotificationSetting in favour of api.chats.watch

This commit is contained in:
Julian Lam
2023-11-16 11:23:39 -05:00
parent b13c6ee431
commit 75c8cda18c
7 changed files with 94 additions and 8 deletions

View File

@@ -200,6 +200,8 @@ paths:
$ref: 'write/chats/roomId.yaml'
/chats/{roomId}/state:
$ref: 'write/chats/roomId/state.yaml'
/chats/{roomId}/watch:
$ref: 'write/chats/roomId/watch.yaml'
/chats/{roomId}/users:
$ref: 'write/chats/roomId/users.yaml'
/chats/{roomId}/users/{uid}:

View File

@@ -0,0 +1,69 @@
put:
tags:
- chats
summary: set chat room notification setting
description: >
This operation updates the chat room notification setting for the calling user.
N.B. The calling user must be in the chat room for this call to succeed.
parameters:
- in: path
name: roomId
schema:
type: number
required: true
description: a valid room id
example: 1
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
value:
type: number
example: 11
required:
- value
responses:
'200':
description: Chat room notification setting updated.
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response: {}
delete:
tags:
- chats
summary: restore default chat room notification setting
description: >
This operation restores the default value for notifications for the calling user.
You can also call the `PUT` variant of this route, and pass in `-1` for `value`.
That would accomplish the same thing.
N.B. The calling user must be in the chat room for this call to succeed.
parameters:
- in: path
name: roomId
schema:
type: number
required: true
description: a valid room id
example: 1
responses:
'200':
description: Chat room notification setting updated.
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response: {}

View File

@@ -184,8 +184,7 @@ define('forum/chats', [
const $this = $(this);
$this.find('i.fa-check').removeClass('hidden');
notifSettingEl.find('[component="chat/notification/setting/icon"]').attr('class', `fa ${$this.attr('data-icon')}`);
await socket.emit('modules.chats.setNotificationSetting', {
roomId: roomId,
await api.put(`/chats/${roomId}/watch`, {
value: $this.attr('data-value'),
});
});

View File

@@ -198,6 +198,15 @@ chatsAPI.mark = async (caller, data) => {
messaging.pushUnreadCount(caller.uid);
};
chatsAPI.watch = async (caller, { roomId, state }) => {
const inRoom = await messaging.isUserInRoom(caller.uid, roomId);
if (!inRoom) {
throw new Error('[[error:no-privileges]]');
}
await messaging.setUserNotificationSetting(caller.uid, roomId, state);
};
chatsAPI.users = async (caller, data) => {
const start = data.hasOwnProperty('start') ? data.start : 0;
const stop = start + 39;

View File

@@ -88,6 +88,13 @@ Chats.mark = async (req, res) => {
helpers.formatApiResponse(200, res);
};
Chats.watch = async (req, res) => {
const state = req.method === 'DELETE' ? -1 : parseInt(req.body.value, 10) || -1;
await api.chats.watch(req, { state, ...req.params });
helpers.formatApiResponse(200, res);
};
Chats.users = async (req, res) => {
const { roomId } = req.params;
const start = parseInt(req.query.start, 10) || 0;

View File

@@ -24,6 +24,9 @@ module.exports = function () {
setupApiRoute(router, 'put', '/:roomId/state', [...middlewares, middleware.assert.room], controllers.write.chats.mark);
setupApiRoute(router, 'delete', '/:roomId/state', [...middlewares, middleware.assert.room], controllers.write.chats.mark);
setupApiRoute(router, 'put', '/:roomId/watch', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['value'])], controllers.write.chats.watch);
setupApiRoute(router, 'delete', '/:roomId/watch', [...middlewares, middleware.assert.room], controllers.write.chats.watch);
setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users);
setupApiRoute(router, 'post', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.invite);
setupApiRoute(router, 'delete', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.kick);

View File

@@ -180,16 +180,13 @@ SocketModules.chats.toggleOwner = async (socket, data) => {
};
SocketModules.chats.setNotificationSetting = async (socket, data) => {
sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/chats/:roomId/watch');
if (!data || !utils.isNumber(data.value) || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
const inRoom = await Messaging.isUserInRoom(socket.uid, data.roomId);
if (!inRoom) {
throw new Error('[[error:no-privileges]]');
}
await Messaging.setUserNotificationSetting(socket.uid, data.roomId, data.value);
await api.chats.watch(socket, data);
};
SocketModules.chats.searchMessages = async (socket, data) => {