Skip to content

Node.js based forum software built for the modern web

fix(chats): mark notifications with matching roomId read when opening a room (#14450)

palmoni53a7497e5771a
diff --git a/src/api/chats.js b/src/api/chats.js
index bd2c1c6bd6..8c14f902f7 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 privileges = require('../privileges');
const plugins = require('../plugins');
const utils = require('../utils');
@@ -210,9 +209,7 @@ chatsAPI.mark = async (caller, data) => {
} else {
await messaging.markRead(caller.uid, roomId);
socketHelpers.emitToUids('event:chats.markedAsRead', { roomId: roomId }, [caller.uid]);
- const nids = await user.notifications.getUnreadByField(caller.uid, 'roomId', [roomId]);
- await notifications.markReadMultiple(nids, caller.uid);
- user.notifications.pushCount(caller.uid);
+ await messaging.markRoomNotificationsRead(caller.uid, roomId);
}
socketHelpers.emitToUids('event:chats.mark', { roomId, state }, [caller.uid]);
diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js
index e101157ea9..632c627d7e 100644
--- a/src/messaging/notifications.js
+++ b/src/messaging/notifications.js
@@ -1,6 +1,7 @@
'use strict';
const winston = require('winston');
+const _ = require('lodash');
const batch = require('../batch');
const db = require('../database');
@@ -30,10 +31,16 @@ module.exports = function (Messaging) {
};
Messaging.markRoomNotificationsRead = async (uid, roomId) => {
- const chatNids = await db.getSortedSetScan({
- key: `uid:${uid}:notifications:unread`,
- match: `chat_${roomId}_*`,
- });
+ const [scannedNids, fieldNids] = await Promise.all([
+ db.getSortedSetScan({
+ key: `uid:${uid}:notifications:unread`,
+ match: `chat_${roomId}_*`,
+ }),
+ // covers notifications that reference the room but use a different
+ // nid format (e.g. those created by plugins)
+ user.notifications.getUnreadByField(uid, 'roomId', [roomId]),
+ ]);
+ const chatNids = _.uniq(scannedNids.concat(fieldNids));
if (chatNids.length) {
await notifications.markReadMultiple(chatNids, uid);
await user.notifications.pushCount(uid);