Skip to content

Commit

Permalink
feat: change chat notification delivery
Browse files Browse the repository at this point in the history
instead of checking if user is online in the past x minutes, check if the user has read the room, if they have already marked the chat as read don't send notification
  • Loading branch information
barisusakli committed Mar 27, 2023
1 parent 2a9b3ee commit 2c5a5bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/messaging/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ module.exports = function (Messaging) {
};

async function sendNotifications(fromuid, uids, roomId, messageObj) {
const isOnline = await user.isOnline(uids);
uids = uids.filter((uid, index) => !isOnline[index] && parseInt(fromuid, 10) !== parseInt(uid, 10));
const hasRead = await Messaging.hasRead(uids, roomId);
uids = uids.filter((uid, index) => !hasRead[index] && parseInt(fromuid, 10) !== parseInt(uid, 10));
console.log('checking notif', uids, hasRead);
if (!uids.length) {
delete Messaging.notifyQueue[`${fromuid}:${roomId}`];
return;
Expand Down
9 changes: 9 additions & 0 deletions src/messaging/unread.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ module.exports = function (Messaging) {
};

Messaging.markRead = async (uid, roomId) => {
console.log('mark read :)', uid, roomId);
await db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId);
};

Messaging.hasRead = async (uids, roomId) => {
const isMembers = await db.isMemberOfSortedSets(
uids.map(uid => `uid:${uid}:chat:rooms:unread`),
roomId
);
return uids.map((uid, index) => !isMembers[index]);
};

Messaging.markAllRead = async (uid) => {
await db.delete(`uid:${uid}:chat:rooms:unread`);
};
Expand Down

0 comments on commit 2c5a5bf

Please sign in to comment.