Files
NodeBB/src/messaging/unread.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-12-15 14:10:32 +02:00
'use strict';
2021-02-04 00:06:15 -07:00
const db = require('../database');
const sockets = require('../socket.io');
2015-12-15 14:10:32 +02:00
module.exports = function (Messaging) {
Messaging.getUnreadCount = async (uid) => {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
return 0;
2016-03-11 13:38:52 +02:00
}
2021-02-03 23:59:08 -07:00
return await db.sortedSetCard(`uid:${uid}:chat:rooms:unread`);
2015-12-15 14:10:32 +02:00
};
Messaging.pushUnreadCount = async (uid) => {
2018-11-12 00:20:44 -05:00
if (parseInt(uid, 10) <= 0) {
return;
2016-03-11 13:38:52 +02:00
}
const unreadCount = await Messaging.getUnreadCount(uid);
2021-02-03 23:59:08 -07:00
sockets.in(`uid_${uid}`).emit('event:unread.updateChatCount', unreadCount);
2015-12-15 14:10:32 +02:00
};
Messaging.markRead = async (uid, roomId) => {
2021-02-03 23:59:08 -07:00
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) => {
2021-02-03 23:59:08 -07:00
await db.delete(`uid:${uid}:chat:rooms:unread`);
};
2016-03-11 13:38:52 +02:00
Messaging.markUnread = async (uids, roomId) => {
const exists = await Messaging.roomExists(roomId);
if (!exists) {
return;
}
2021-02-03 23:59:08 -07:00
const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`);
return await db.sortedSetsAdd(keys, Date.now(), roomId);
2015-12-15 14:10:32 +02:00
};
2017-02-18 02:30:48 -07:00
};