forked from danielcardeenas/sulla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-unread-messages-in-chat.js
54 lines (49 loc) Β· 1.3 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
48
49
50
51
52
53
54
/**
* Retrieves unread messages from chat and mark them as read as a regular UX
* @param {string} id Chat id
* @param {boolean} includeMe Include user client messages
* @param {boolean} includeNotifications Include notifications
* @param {Function} done
*/
export function getUnreadMessagesInChat(
id,
includeMe,
includeNotifications,
done
) {
// get chat and its messages
let chat = 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 = 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;
}