Skip to content

Commit 3a7497e

Browse files
authored
fix(chats): mark notifications with matching roomId read when opening a room (#14450)
Messaging.markRoomNotificationsRead only matched notifications whose nid starts with chat_<roomId>_, so chat-related notifications created by plugins (e.g. message reactions) stayed unread even after the user opened the room. Topics already handle this by matching unread notifications on the tid field, and chatsAPI.mark did the same with the roomId field. Match unread notifications by their roomId field as well, and reuse markRoomNotificationsRead in chatsAPI.mark instead of duplicating the logic there.
1 parent 597cdb4 commit 3a7497e

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

src/api/chats.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const db = require('../database');
66
const user = require('../user');
77
const meta = require('../meta');
88
const messaging = require('../messaging');
9-
const notifications = require('../notifications');
109
const privileges = require('../privileges');
1110
const plugins = require('../plugins');
1211
const utils = require('../utils');
@@ -210,9 +209,7 @@ chatsAPI.mark = async (caller, data) => {
210209
} else {
211210
await messaging.markRead(caller.uid, roomId);
212211
socketHelpers.emitToUids('event:chats.markedAsRead', { roomId: roomId }, [caller.uid]);
213-
const nids = await user.notifications.getUnreadByField(caller.uid, 'roomId', [roomId]);
214-
await notifications.markReadMultiple(nids, caller.uid);
215-
user.notifications.pushCount(caller.uid);
212+
await messaging.markRoomNotificationsRead(caller.uid, roomId);
216213
}
217214

218215
socketHelpers.emitToUids('event:chats.mark', { roomId, state }, [caller.uid]);

src/messaging/notifications.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const winston = require('winston');
4+
const _ = require('lodash');
45

56
const batch = require('../batch');
67
const db = require('../database');
@@ -30,10 +31,16 @@ module.exports = function (Messaging) {
3031
};
3132

3233
Messaging.markRoomNotificationsRead = async (uid, roomId) => {
33-
const chatNids = await db.getSortedSetScan({
34-
key: `uid:${uid}:notifications:unread`,
35-
match: `chat_${roomId}_*`,
36-
});
34+
const [scannedNids, fieldNids] = await Promise.all([
35+
db.getSortedSetScan({
36+
key: `uid:${uid}:notifications:unread`,
37+
match: `chat_${roomId}_*`,
38+
}),
39+
// covers notifications that reference the room but use a different
40+
// nid format (e.g. those created by plugins)
41+
user.notifications.getUnreadByField(uid, 'roomId', [roomId]),
42+
]);
43+
const chatNids = _.uniq(scannedNids.concat(fieldNids));
3744
if (chatNids.length) {
3845
await notifications.markReadMultiple(chatNids, uid);
3946
await user.notifications.pushCount(uid);

0 commit comments

Comments
 (0)