2016-07-12 03:42:54 +08:00
|
|
|
import { Tracker } from 'meteor/tracker';
|
|
|
|
|
2016-07-07 20:50:32 +08:00
|
|
|
import Storage from '/imports/ui/services/storage/session';
|
2016-07-08 00:51:55 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2018-08-01 01:13:36 +08:00
|
|
|
import GroupChat from '/imports/api/group-chat';
|
2019-08-03 02:18:33 +08:00
|
|
|
import { GroupChatMsg } from '/imports/api/group-chat-msg';
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2016-08-17 23:48:03 +08:00
|
|
|
const CHAT_CONFIG = Meteor.settings.public.chat;
|
|
|
|
const STORAGE_KEY = CHAT_CONFIG.storage_key;
|
2018-07-27 21:44:21 +08:00
|
|
|
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2016-07-12 00:28:55 +08:00
|
|
|
class UnreadMessagesTracker {
|
|
|
|
constructor() {
|
2017-06-03 03:25:02 +08:00
|
|
|
this._tracker = new Tracker.Dependency();
|
2019-08-22 20:05:06 +08:00
|
|
|
this._unreadChats = {
|
|
|
|
...Storage.getItem('UNREAD_CHATS'),
|
|
|
|
[PUBLIC_GROUP_CHAT_ID]: (new Date()).getTime(),
|
|
|
|
};
|
2018-05-30 00:43:11 +08:00
|
|
|
this.get = this.get.bind(this);
|
2016-07-12 00:28:55 +08:00
|
|
|
}
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2016-07-12 00:28:55 +08:00
|
|
|
get(chatID) {
|
2016-07-12 03:42:54 +08:00
|
|
|
this._tracker.depend();
|
2016-07-12 00:28:55 +08:00
|
|
|
return this._unreadChats[chatID] || 0;
|
2016-07-05 02:53:47 +08:00
|
|
|
}
|
|
|
|
|
2016-07-12 00:28:55 +08:00
|
|
|
update(chatID, timestamp = 0) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const currentValue = this.get(chatID);
|
2016-07-12 00:28:55 +08:00
|
|
|
if (currentValue < timestamp) {
|
|
|
|
this._unreadChats[chatID] = timestamp;
|
2016-07-12 03:42:54 +08:00
|
|
|
this._tracker.changed();
|
2016-07-12 00:28:55 +08:00
|
|
|
Storage.setItem(STORAGE_KEY, this._unreadChats);
|
|
|
|
}
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2016-07-12 00:28:55 +08:00
|
|
|
return this._unreadChats[chatID];
|
2016-07-11 20:34:58 +08:00
|
|
|
}
|
|
|
|
|
2018-05-30 00:43:11 +08:00
|
|
|
getUnreadMessages(chatID) {
|
2017-06-03 03:25:02 +08:00
|
|
|
const filter = {
|
2018-07-27 21:44:21 +08:00
|
|
|
timestamp: {
|
2016-07-12 00:28:55 +08:00
|
|
|
$gt: this.get(chatID),
|
|
|
|
},
|
2018-07-27 21:44:21 +08:00
|
|
|
sender: { $ne: Auth.userID },
|
2016-07-12 00:28:55 +08:00
|
|
|
};
|
2018-07-27 21:44:21 +08:00
|
|
|
if (chatID === PUBLIC_GROUP_CHAT_ID) {
|
2018-12-10 19:28:29 +08:00
|
|
|
filter.chatId = { $eq: chatID };
|
2016-07-12 00:28:55 +08:00
|
|
|
} else {
|
2019-08-22 20:05:06 +08:00
|
|
|
const privateChat = GroupChat.findOne({ users: { $all: [chatID, Auth.userID] } },
|
|
|
|
{ fields: { chatId: 1 } });
|
2018-08-01 01:13:36 +08:00
|
|
|
|
2018-12-10 19:28:29 +08:00
|
|
|
filter.chatId = { $ne: PUBLIC_GROUP_CHAT_ID };
|
|
|
|
|
2018-08-01 01:13:36 +08:00
|
|
|
if (privateChat) {
|
|
|
|
filter.chatId = privateChat.chatId;
|
|
|
|
}
|
2016-07-12 00:28:55 +08:00
|
|
|
}
|
2018-07-27 21:44:21 +08:00
|
|
|
const messages = GroupChatMsg.find(filter).fetch();
|
2018-05-30 00:43:11 +08:00
|
|
|
return messages;
|
|
|
|
}
|
2016-07-12 00:28:55 +08:00
|
|
|
|
2018-05-30 00:43:11 +08:00
|
|
|
count(chatID) {
|
|
|
|
const messages = this.getUnreadMessages(chatID);
|
2018-06-09 02:40:35 +08:00
|
|
|
return messages.length;
|
2016-07-12 00:28:55 +08:00
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2016-07-05 02:53:47 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const UnreadTrackerSingleton = new UnreadMessagesTracker();
|
2016-07-12 00:28:55 +08:00
|
|
|
export default UnreadTrackerSingleton;
|