Files
NodeBB/src/messaging/unread.js

38 lines
1.0 KiB
JavaScript
Raw Normal View History

2015-12-15 14:10:32 +02:00
'use strict';
var db = require('../database');
var sockets = require('../socket.io');
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
}
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);
sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount);
2015-12-15 14:10:32 +02:00
};
Messaging.markRead = async (uid, roomId) => db.sortedSetRemove('uid:' + uid + ':chat:rooms:unread', roomId);
Messaging.markAllRead = async uid => 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) {
throw new Error('[[error:chat-room-does-not-exist]]');
}
var keys = uids.map(function (uid) {
return 'uid:' + uid + ':chat:rooms:unread';
});
2015-12-16 15:09:14 +02: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
};