Skip to content

Node.js based forum software built for the modern web

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

Julian Lam75c8cda18c32
diff --git a/public/openapi/write.yaml b/public/openapi/write.yaml
index 819caf9e29..aad2bf2d16 100644
--- a/public/openapi/write.yaml
+++ b/public/openapi/write.yaml
@@ -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}:
diff --git a/public/openapi/write/chats/roomId/watch.yaml b/public/openapi/write/chats/roomId/watch.yaml
new file mode 100644
index 0000000000..cdc01ce82b
--- /dev/null
+++ b/public/openapi/write/chats/roomId/watch.yaml
@@ -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: {}
\ No newline at end of file
diff --git a/public/src/client/chats.js b/public/src/client/chats.js
index 222960945c..d831170e78 100644
--- a/public/src/client/chats.js
+++ b/public/src/client/chats.js
@@ -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'),
});
});
diff --git a/src/api/chats.js b/src/api/chats.js
index e0d1429803..6336aa9e2b 100644
--- a/src/api/chats.js
+++ b/src/api/chats.js
@@ -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;
diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.js
index 04f87b3efd..f7602ebf7b 100644
--- a/src/controllers/write/chats.js
+++ b/src/controllers/write/chats.js
@@ -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;
diff --git a/src/routes/write/chats.js b/src/routes/write/chats.js
index 4e73ee0f37..9f5c4c088e 100644
--- a/src/routes/write/chats.js
+++ b/src/routes/write/chats.js
@@ -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);
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index 699a6850fc..e3376720ec 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -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) => {