forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-message-by-id.js
51 lines (44 loc) · 1.08 KB
/
get-message-by-id.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
export async function getMessageById(key, done, serialize = true) {
// Check message is loaded in store
let msg = window.Store.Msg.get(key);
let erro = { erro: true };
if (!msg) {
// Get chat of message
const chat = window.Store.Chat.get(key.remote);
if (!chat) {
return erro;
}
//If not message not found, load latest messages of chat
await chat.onEmptyMRM();
await WAPI.sleep(100);
msg = window.Store.Msg.get(key);
if (!msg) {
// If not found, load messages around the message ID
const context = chat.getSearchContext(key);
if (
context &&
context.collection &&
context.collection.loadAroundPromise
) {
await context.collection.loadAroundPromise;
}
msg = window.Store.Msg.get(key);
}
}
if (!msg) {
return erro;
}
let result = erro;
if (serialize) {
try {
result = await WAPI.processMessageObj(msg, true, true);
} catch (err) {}
} else {
result = msg;
}
if (typeof done === 'function') {
done(result);
} else {
return result;
}
}