mark chat room notifs read on load

isekai-main
Barış Soner Uşaklı 2 years ago
parent 2792d77104
commit 4f3f9d9891

@ -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) => {

@ -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) => {

@ -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}`,
});

@ -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) => {

@ -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}`);
});

Loading…
Cancel
Save