Skip to content

Node.js based forum software built for the modern web

mark chat room notifs read on load

Barış Soner Uşaklı4f3f9d9891b4
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 <strong>foo</strong>');
- 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}`);
});