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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Messaging) {
|
2019-08-13 14:36:15 -04:00
|
|
|
Messaging.getUnreadCount = async (uid) => {
|
2018-11-12 00:20:44 -05:00
|
|
|
if (parseInt(uid, 10) <= 0) {
|
2019-07-22 11:18:13 -04:00
|
|
|
return 0;
|
2016-03-11 13:38:52 +02:00
|
|
|
}
|
2019-07-22 11:18:13 -04: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
|
|
|
};
|
|
|
|
|
|
2019-08-13 14:36:15 -04:00
|
|
|
Messaging.pushUnreadCount = async (uid) => {
|
2018-11-12 00:20:44 -05:00
|
|
|
if (parseInt(uid, 10) <= 0) {
|
2017-02-17 21:55:19 -07:00
|
|
|
return;
|
2016-03-11 13:38:52 +02:00
|
|
|
}
|
2019-07-22 11:18:13 -04: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
|
|
|
};
|
|
|
|
|
|
2020-06-10 20:49:41 -04:00
|
|
|
Messaging.markRead = async (uid, roomId) => {
|
2021-02-03 23:59:08 -07:00
|
|
|
await db.sortedSetRemove(`uid:${uid}:chat:rooms:unread`, roomId);
|
2020-06-10 20:49:41 -04:00
|
|
|
};
|
|
|
|
|
|
2023-03-26 21:13:07 -04:00
|
|
|
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]);
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-10 20:49:41 -04:00
|
|
|
Messaging.markAllRead = async (uid) => {
|
2021-02-03 23:59:08 -07:00
|
|
|
await db.delete(`uid:${uid}:chat:rooms:unread`);
|
2020-06-10 20:49:41 -04:00
|
|
|
};
|
2016-03-11 13:38:52 +02:00
|
|
|
|
2019-07-22 11:18:13 -04:00
|
|
|
Messaging.markUnread = async (uids, roomId) => {
|
|
|
|
|
const exists = await Messaging.roomExists(roomId);
|
|
|
|
|
if (!exists) {
|
2021-04-23 14:46:54 -04:00
|
|
|
return;
|
2019-07-22 11:18:13 -04:00
|
|
|
}
|
2021-02-03 23:59:08 -07:00
|
|
|
const keys = uids.map(uid => `uid:${uid}:chat:rooms:unread`);
|
2019-07-22 11:18:13 -04:00
|
|
|
return await db.sortedSetsAdd(keys, Date.now(), roomId);
|
2015-12-15 14:10:32 +02:00
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|