From 4f3f9d9891b441d3bec72e2763edaa2c56965cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 21 Jul 2023 16:19:26 -0400 Subject: [PATCH] mark chat room notifs read on load --- src/api/chats.js | 18 ------------------ src/controllers/write/chats.js | 4 ++-- src/messaging/notifications.js | 14 +++++++++++++- src/messaging/rooms.js | 13 ++++++------- test/messaging.js | 2 +- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/api/chats.js b/src/api/chats.js index 976c380679..16a54de74d 100644 --- a/src/api/chats.js +++ b/src/api/chats.js @@ -6,7 +6,6 @@ const db = require('../database'); const user = require('../user'); const meta = require('../meta'); const messaging = require('../messaging'); -const notifications = require('../notifications'); const plugins = require('../plugins'); const privileges = require('../privileges'); @@ -152,27 +151,10 @@ chatsAPI.mark = async (caller, data) => { } else { await messaging.markRead(caller.uid, roomId); socketHelpers.emitToUids('event:chats.markedAsRead', { roomId: roomId }, [caller.uid]); - - const isUserInRoom = await messaging.isUserInRoom(caller.uid, roomId); - if (!isUserInRoom) { - return; - } - let chatNids = await db.getSortedSetScan({ - key: `uid:${caller.uid}:notifications:unread`, - match: `chat_*`, - }); - chatNids = chatNids.filter( - nid => nid && !nid.startsWith(`chat_${caller.uid}_`) && nid.endsWith(`_${roomId}`) - ); - - await notifications.markReadMultiple(chatNids, caller.uid); - await user.notifications.pushCount(caller.uid); } socketHelpers.emitToUids('event:chats.mark', { roomId, state }, [caller.uid]); messaging.pushUnreadCount(caller.uid); - - return messaging.loadRoom(caller.uid, { roomId }); }; chatsAPI.users = async (caller, data) => { diff --git a/src/controllers/write/chats.js b/src/controllers/write/chats.js index e595ef1a44..2266795eec 100644 --- a/src/controllers/write/chats.js +++ b/src/controllers/write/chats.js @@ -58,12 +58,12 @@ Chats.rename = async (req, res) => { Chats.mark = async (req, res) => { const state = req.method === 'PUT' ? 1 : 0; - const roomObj = await api.chats.mark(req, { + await api.chats.mark(req, { roomId: req.params.roomId, state, }); - helpers.formatApiResponse(200, res, roomObj); + helpers.formatApiResponse(200, res); }; Chats.users = async (req, res) => { diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index 11a1ebd16f..2295dcd58a 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -5,6 +5,7 @@ const winston = require('winston'); const batch = require('../batch'); const db = require('../database'); const notifications = require('../notifications'); +const user = require('../user'); const io = require('../socket.io'); const plugins = require('../plugins'); const meta = require('../meta'); @@ -29,6 +30,17 @@ module.exports = function (Messaging) { return uids.map(uid => parseInt(settings[uid] || roomData.notificationSetting, 10)); }; + Messaging.markRoomNotificationsRead = async (uid, roomId) => { + const chatNids = await db.getSortedSetScan({ + key: `uid:${uid}:notifications:unread`, + match: `chat_${roomId}_*`, + }); + if (chatNids.length) { + await notifications.markReadMultiple(chatNids, uid); + await user.notifications.pushCount(uid); + } + }; + Messaging.notifyUsersInRoom = async (fromUid, roomId, messageObj) => { const isPublic = parseInt(await db.getObjectField(`chat:room:${roomId}`, 'public'), 10) === 1; @@ -114,7 +126,7 @@ module.exports = function (Messaging) { subject: `[[email:notif.chat.subject, ${displayname}]]`, bodyShort: `[[notifications:new_message_from, ${displayname}]]`, bodyLong: messageObj.content, - nid: `chat_${fromUid}_${roomId}`, + nid: `chat_${roomId}_${fromUid}`, from: fromUid, path: `/chats/${messageObj.roomId}`, }); diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index c5698185ac..51c76c6ce9 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -26,16 +26,14 @@ const intFields = [ module.exports = function (Messaging) { Messaging.getRoomData = async (roomId, fields = []) => { - const data = await db.getObject(`chat:room:${roomId}`, fields); - if (!data) { - throw new Error('[[error:no-chat-room]]'); - } - - modifyRoomData([data], fields); - return data; + const roomData = await Messaging.getRoomsData([roomId], fields); + return roomData[0]; }; Messaging.getRoomsData = async (roomIds, fields = []) => { + if (fields.includes('notificationSetting') && !fields.includes('public')) { + fields.push('public'); + } const roomData = await db.getObjects( roomIds.map(roomId => `chat:room:${roomId}`), fields @@ -504,6 +502,7 @@ module.exports = function (Messaging) { Messaging.isRoomOwner(uid, roomId), io.getUidsInRoom(`chat_room_${roomId}`), getNotificationOptions(), + Messaging.markRoomNotificationsRead(uid, roomId), ]); users.forEach((user) => { diff --git a/test/messaging.js b/test/messaging.js index 9b07ff3670..cb9e9f1555 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -404,7 +404,7 @@ describe('Messaging Library', () => { assert(data.unread[0]); const notification = data.unread[0]; assert.strictEqual(notification.bodyShort, 'New message from foo'); - assert.strictEqual(notification.nid, `chat_${mocks.users.foo.uid}_${roomId}`); + assert.strictEqual(notification.nid, `chat_${roomId}_${mocks.users.foo.uid}`); assert.strictEqual(notification.path, `${nconf.get('relative_path')}/chats/${roomId}`); });