-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsend-link-preview.js
80 lines (79 loc) · 2.24 KB
/
send-link-preview.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 sendLinkPreview(chatId, url, text, body, thumbnail) {
text = text || '';
const _Path = {
Protocol: '^(https?:\\/\\/)?',
Domain: '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|',
IP: '((\\d{1,3}\\.){3}\\d{1,3}))',
Port: '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*',
Query: '(\\?[;&a-z\\d%_.~+=-]*)?',
End: '(\\#[-a-z\\d_]*)?$',
Reg: () => {
return new RegExp(
_Path.Protocol +
_Path.Domain +
_Path.IP +
_Path.Port +
_Path.Query +
_Path.End,
'i'
);
}
};
if (!_Path.Reg().test(url)) {
var text =
'Use a valid HTTP protocol. Example: https://www.youtube.com/watch?v=V1bFr2SWP1';
return WAPI.scope(chatId, true, null, text);
}
var chat = await WAPI.sendExist(chatId);
if (!chat.erro) {
const newMsgId = await window.WAPI.getNewMessageId(chat.id._serialized);
const fromwWid = await Store.MaybeMeUser.getMaybeMeUser();
let inChat = await WAPI.getchatId(chat.id).catch(() => {});
if (inChat) {
chat.lastReceivedKey._serialized = inChat._serialized;
chat.lastReceivedKey.id = inChat.id;
}
const link = await window.Store.Validators.findLink(url);
const message = {
id: newMsgId,
links: link,
ack: 0,
body: body.includes(url) ? body : url + '\n' + body,
from: fromwWid,
to: chat.id,
local: !0,
self: 'out',
t: parseInt(new Date().getTime() / 1000),
isNewMsg: !0,
type: 'chat',
subtype: 'url',
preview: true,
disappearingModeInitiator: 'chat',
thumbnail: thumbnail,
content: url,
canonicalUrl: url,
description: url,
matchedText: url,
title: text
};
const result = (
await Promise.all(window.Store.addAndSendMsgToChat(chat, message))
)[1];
let m = { type: 'LinkPreview', url: url, text: text };
if (
result === 'success' ||
result === 'OK' ||
result.messageSendResult === 'OK'
) {
let obj = WAPI.scope(newMsgId, false, result, null);
Object.assign(obj, m);
return obj;
} else {
let obj = WAPI.scope(newMsgId, true, result, null);
Object.assign(obj, m);
return obj;
}
} else {
return chat;
}
}