forked from orkestral/venom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-contact-vcard.js
80 lines (71 loc) · 2.09 KB
/
send-contact-vcard.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export async function sendContactVcard(chatId, contact, name) {
if (typeof chatId != 'string' || chatId.length === 0) {
return WAPI.scope(chatId, true, 404, 'It is necessary to pass the number!');
}
if (typeof contact != 'string' || contact.length === 0) {
return WAPI.scope(
contact,
true,
404,
'It is necessary to pass the number!'
);
}
const chat = await WAPI.sendExist(chatId);
const cont = await WAPI.sendExist(contact);
if (
chat &&
chat.status != 404 &&
chat.id &&
cont &&
cont.status != 404 &&
cont.id
) {
const newMsgId = await window.WAPI.getNewMessageId(chat.id._serialized);
let inChat = await WAPI.getchatId(chat.id).catch(() => {
return WAPI.scope(chat.id, true, 404, 'Error to number ' + chatId);
});
if (inChat) {
chat.lastReceivedKey && chat.lastReceivedKey._serialized
? (chat.lastReceivedKey._serialized = inChat._serialized)
: '';
chat.lastReceivedKey && chat.lastReceivedKey.id
? (chat.lastReceivedKey.id = inChat.id)
: '';
}
if (!newMsgId) {
return WAPI.scope(chatId, true, 404, 'Error to newId');
}
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();
const body = await window.Store.Vcard.vcardFromContactModel(
cont.__x_contact
);
name = !name ? cont.__x_formattedTitle : name;
const message = {
id: newMsgId,
ack: 0,
body: body.vcard,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'vcard'
};
const result = (
await Promise.all(window.Store.addAndSendMsgToChat(chat, message))
)[1];
var m = { from: contact, type: 'vcard' };
if (result === 'success' || result || result.messageSendResult === 'OK') {
var obj = WAPI.scope(newMsgId, false, result, null);
Object.assign(obj, m);
return obj;
} else {
var obj = WAPI.scope(newMsgId, true, result, null);
Object.assign(obj, m);
return obj;
}
} else {
return chat;
}
}