-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathget-unread-messages-in-chat.js
47 lines (42 loc) · 1.06 KB
/
get-unread-messages-in-chat.js
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
export async function getUnreadMessagesInChat(
id,
includeMe,
includeNotifications,
done
) {
// get chat and its messages
let chat = await WAPI.getChat(id);
let messages = chat.msgs._models;
// initialize result list
let output = [];
// look for unread messages, newest is at the end of array
for (let i = messages.length - 1; i >= 0; i--) {
// system message: skip it
if (i === 'remove') {
continue;
}
// get message
let messageObj = messages[i];
// found a read message: stop looking for others
if (
typeof messageObj.isNewMsg !== 'boolean' ||
messageObj.isNewMsg === false
) {
continue;
} else {
messageObj.isNewMsg = false;
// process it
let message = await WAPI.processMessageObj(
messageObj,
includeMe,
includeNotifications
);
// save processed message on result list
if (message) output.push(message);
}
}
// callback was passed: run it
if (done !== undefined) done(output);
// return result list
return output;
}