bigbluebutton-Github/bigbluebutton-html5/imports/ui/services/unread-messages/index.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

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-05 02:53:47 +08:00
const CHAT_CONFIG = Meteor.settings.public.chat;
const STORAGE_KEY = CHAT_CONFIG.storage_key;
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();
this._unreadChats = {
...Storage.getItem('UNREAD_CHATS'),
[PUBLIC_GROUP_CHAT_ID]: (new Date()).getTime(),
};
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
}
2021-02-04 04:49:58 +08:00
2021-04-10 04:35:05 +08:00
getUnreadMessages(chatID, messages) {
const isPublicChat = chatID === PUBLIC_GROUP_CHAT_ID;
let unreadMessages = [];
if (messages[chatID]) {
const contextChat = messages[chatID];
const unreadTimewindows = contextChat.unreadTimeWindows;
for (const unreadTimeWindowId of unreadTimewindows) {
unreadMessages.push(isPublicChat
? contextChat?.preJoinMessages[unreadTimeWindowId] || contextChat?.posJoinMessages[unreadTimeWindowId]
: contextChat?.messageGroups[unreadTimeWindowId]);
}
2016-07-12 00:28:55 +08:00
}
2021-04-10 04:35:05 +08:00
return unreadMessages;
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;