forked from danielcardeenas/sulla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-message-to-id.js
57 lines (56 loc) Β· 1.41 KB
/
send-message-to-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
52
53
54
55
56
57
/**
* Sends message to given chat id
* @param {string} id
* @param {string} message
* @param {Function} done
*/
export function sendMessageToID(id, message, done) {
try {
window.getContact = (id) => {
return Store.WapQuery.queryExist(id);
};
window.getContact(id).then((contact) => {
if (contact.status === 404) {
done(true);
} else {
Store.Chat.find(contact.jid)
.then((chat) => {
chat.sendMessage(message);
return true;
})
.catch((reject) => {
if (WAPI.sendMessage(id, message)) {
done(true);
return true;
} else {
done(false);
return false;
}
});
}
});
} catch (e) {
if (window.Store.Chat.length === 0) return false;
firstChat = Store.Chat.models[0];
var originalID = firstChat.id;
firstChat.id =
typeof originalID === 'string'
? id
: new window.Store.UserConstructor(id, {
intentionallyUsePrivateConstructor: true,
});
if (done !== undefined) {
firstChat.sendMessage(message).then(function () {
firstChat.id = originalID;
done(true);
});
return true;
} else {
firstChat.sendMessage(message);
firstChat.id = originalID;
return true;
}
}
if (done !== undefined) done(false);
return false;
}