/
readMessages.ts
168 lines (139 loc) · 4.16 KB
/
readMessages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { Emitter } from '@rocket.chat/emitter';
import type { IRoom } from '@rocket.chat/core-typings';
import $ from 'jquery';
import { RoomHistoryManager } from './RoomHistoryManager';
import { RoomManager } from './RoomManager';
import { ChatSubscription, ChatMessage } from '../../../models/client';
import { APIClient } from '../../../utils/client';
export class ReadMessage extends Emitter {
protected enabled: boolean;
protected debug = false;
constructor() {
super();
this.enable();
}
protected log(...args: any[]) {
return this.debug && console.log(...args);
}
public enable() {
this.enabled = document.hasFocus();
}
public disable() {
this.enabled = false;
}
public isEnable() {
return this.enabled === true;
}
public read(rid: IRoom['_id'] = Session.get('openedRoom')) {
if (!this.enabled) {
this.log('readMessage -> readNow canceled by enabled: false');
return;
}
const subscription = ChatSubscription.findOne({ rid });
if (subscription == null) {
this.log('readMessage -> readNow canceled, no subscription found for rid:', rid);
return;
}
if (subscription.alert === false && subscription.unread === 0) {
this.log('readMessage -> readNow canceled, alert', subscription.alert, 'and unread', subscription.unread);
return;
}
const room = RoomManager.getOpenedRoomByRid(rid);
if (room == null) {
this.log('readMessage -> readNow canceled, no room found for typeName:', subscription.t + subscription.name);
return;
}
// Only read messages if user saw the first unread message
const unreadMark = $('.message.first-unread, .rcx-message-divider--unread');
if (unreadMark.length > 0) {
const position = unreadMark.position();
const visible = (position ? position.top : 0) >= 0;
if (!visible) {
this.log('readMessage -> readNow canceled, unread mark visible:', visible);
return;
}
// if unread mark is not visible and there is more more not loaded unread messages
} else if (RoomHistoryManager.getRoom(rid).unreadNotLoaded.get() > 0) {
return;
}
return this.readNow(rid);
}
public readNow(rid: IRoom['_id'] = Session.get('openedRoom')) {
if (rid == null) {
this.log('readMessage -> readNow canceled, no rid informed');
return;
}
const subscription = ChatSubscription.findOne({ rid });
if (subscription == null) {
this.log('readMessage -> readNow canceled, no subscription found for rid:', rid);
return;
}
return APIClient.post('/v1/subscriptions.read', { rid }).then(() => {
RoomHistoryManager.getRoom(rid).unreadNotLoaded.set(0);
return this.emit(rid);
});
}
public refreshUnreadMark(rid: IRoom['_id']) {
if (!rid) {
return;
}
const subscription = ChatSubscription.findOne({ rid }, { reactive: false });
if (!subscription) {
return;
}
const room = RoomManager.openedRooms[subscription.t + subscription.name];
if (!room) {
return;
}
if (!subscription.alert && subscription.unread === 0) {
document.querySelector('.message.first-unread')?.classList.remove('first-unread');
room.unreadSince.set(undefined);
return;
}
let lastReadRecord = ChatMessage.findOne(
{
rid: subscription.rid,
ts: {
$lt: subscription.ls,
},
},
{
sort: {
ts: -1,
},
},
) as { ts: Date } | undefined;
const { unreadNotLoaded } = RoomHistoryManager.getRoom(rid);
if (!lastReadRecord && unreadNotLoaded.get() === 0) {
lastReadRecord = { ts: new Date(0) };
}
room.unreadSince.set((lastReadRecord || unreadNotLoaded.get() > 0) && subscription.ls);
if (!lastReadRecord) {
return;
}
const firstUnreadRecord = ChatMessage.findOne(
{
'rid': subscription.rid,
'ts': {
$gt: lastReadRecord.ts,
},
'u._id': {
$ne: Meteor.userId(),
},
},
{
sort: {
ts: 1,
},
},
);
if (firstUnreadRecord) {
room.unreadFirstId = firstUnreadRecord._id;
document.querySelector('.message.first-unread')?.classList.remove('first-unread');
document.querySelector(`.message[data-id="${firstUnreadRecord._id}"]`)?.classList.add('first-unread');
}
}
}
export const readMessage = new ReadMessage();