Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/messages-send.js
1129 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
exports.makeMessagesSocket = void 0;
7
const boom_1 = require("@hapi/boom");
8
const node_cache_1 = __importDefault(require("node-cache"));
9
const WAProto_1 = require("../../WAProto");
10
const Defaults_1 = require("../Defaults");
11
const Utils_1 = require("../Utils");
12
const link_preview_1 = require("../Utils/link-preview");
13
const WABinary_1 = require("../WABinary");
14
const groups_1 = require("./groups");
15
const makeMessagesSocket = (config) => {
16
const { logger, linkPreviewImageThumbnailWidth } = config;
17
const sock = (0, groups_1.makeGroupsSocket)(config);
18
const { ev, authState, upsertMessage, query, fetchPrivacySettings, generateMessageTag, sendNode, groupMetadata, groupToggleEphemeral } = sock;
19
const userDevicesCache = config.userDevicesCache || new node_cache_1.default({
20
stdTTL: 300,
21
useClones: false
22
});
23
let mediaConn;
24
const refreshMediaConn = async (forceGet = false) => {
25
const media = await mediaConn;
26
if (!media || forceGet || (new Date().getTime() - media.fetchDate.getTime()) > media.ttl * 1000) {
27
mediaConn = (async () => {
28
const result = await query({
29
tag: 'iq',
30
attrs: {
31
type: 'set',
32
xmlns: 'w:m',
33
to: WABinary_1.S_WHATSAPP_NET,
34
},
35
content: [{ tag: 'media_conn', attrs: {} }]
36
});
37
const mediaConnNode = (0, WABinary_1.getBinaryNodeChild)(result, 'media_conn');
38
const node = {
39
hosts: (0, WABinary_1.getBinaryNodeChildren)(mediaConnNode, 'host').map(item => item.attrs),
40
auth: mediaConnNode.attrs.auth,
41
ttl: +mediaConnNode.attrs.ttl,
42
fetchDate: new Date()
43
};
44
logger.debug('fetched media conn');
45
return node;
46
})();
47
}
48
return mediaConn;
49
};
50
/**
51
* generic send receipt function
52
* used for receipts of phone call, read, delivery etc.
53
* */
54
const sendReceipt = async (jid, participant, messageIds, type) => {
55
const node = {
56
tag: 'receipt',
57
attrs: {
58
id: messageIds[0],
59
},
60
};
61
const isReadReceipt = type === 'read' || type === 'read-self';
62
if (isReadReceipt) {
63
node.attrs.t = (0, Utils_1.unixTimestampSeconds)().toString();
64
}
65
if (type === 'sender' && (0, WABinary_1.isJidUser)(jid)) {
66
node.attrs.recipient = jid;
67
node.attrs.to = participant;
68
}
69
else {
70
node.attrs.to = jid;
71
if (participant) {
72
node.attrs.participant = participant;
73
}
74
}
75
if (type) {
76
node.attrs.type = type;
77
}
78
const remainingMessageIds = messageIds.slice(1);
79
if (remainingMessageIds.length) {
80
node.content = [
81
{
82
tag: 'list',
83
attrs: {},
84
content: remainingMessageIds.map(id => ({
85
tag: 'item',
86
attrs: { id }
87
}))
88
}
89
];
90
}
91
logger.debug({ attrs: node.attrs, messageIds }, 'sending receipt for messages');
92
await sendNode(node);
93
};
94
/** Correctly bulk send receipts to multiple chats, participants */
95
const sendReceipts = async (keys, type) => {
96
const recps = (0, Utils_1.aggregateMessageKeysNotFromMe)(keys);
97
for (const { jid, participant, messageIds } of recps) {
98
await sendReceipt(jid, participant, messageIds, type);
99
}
100
};
101
/** Bulk read messages. Keys can be from different chats & participants */
102
const readMessages = async (keys) => {
103
const privacySettings = await fetchPrivacySettings();
104
// based on privacy settings, we have to change the read type
105
const readType = privacySettings.readreceipts === 'all' ? 'read' : 'read-self';
106
await sendReceipts(keys, readType);
107
};
108
/** Fetch all the devices we've to send a message to */
109
const getUSyncDevices = async (jids, useCache, ignoreZeroDevices) => {
110
var _a;
111
const deviceResults = [];
112
if (!useCache) {
113
logger.debug('not using cache for devices');
114
}
115
const users = [];
116
jids = Array.from(new Set(jids));
117
for (let jid of jids) {
118
const user = (_a = (0, WABinary_1.jidDecode)(jid)) === null || _a === void 0 ? void 0 : _a.user;
119
jid = (0, WABinary_1.jidNormalizedUser)(jid);
120
if (userDevicesCache.has(user) && useCache) {
121
const devices = userDevicesCache.get(user);
122
deviceResults.push(...devices);
123
logger.trace({ user }, 'using cache for devices');
124
}
125
else {
126
users.push({ tag: 'user', attrs: { jid } });
127
}
128
}
129
const iq = {
130
tag: 'iq',
131
attrs: {
132
to: WABinary_1.S_WHATSAPP_NET,
133
type: 'get',
134
xmlns: 'usync',
135
},
136
content: [
137
{
138
tag: 'usync',
139
attrs: {
140
sid: generateMessageTag(),
141
mode: 'query',
142
last: 'true',
143
index: '0',
144
context: 'message',
145
},
146
content: [
147
{
148
tag: 'query',
149
attrs: {},
150
content: [
151
{
152
tag: 'devices',
153
attrs: { version: '2' }
154
}
155
]
156
},
157
{ tag: 'list', attrs: {}, content: users }
158
]
159
},
160
],
161
};
162
const result = await query(iq);
163
const extracted = (0, Utils_1.extractDeviceJids)(result, authState.creds.me.id, ignoreZeroDevices);
164
const deviceMap = {};
165
for (const item of extracted) {
166
deviceMap[item.user] = deviceMap[item.user] || [];
167
deviceMap[item.user].push(item);
168
deviceResults.push(item);
169
}
170
for (const key in deviceMap) {
171
userDevicesCache.set(key, deviceMap[key]);
172
}
173
return deviceResults;
174
};
175
const assertSessions = async (jids, force) => {
176
let didFetchNewSession = false;
177
let jidsRequiringFetch = [];
178
if (force) {
179
jidsRequiringFetch = jids;
180
}
181
else {
182
const addrs = jids.map(jid => (0, Utils_1.jidToSignalProtocolAddress)(jid).toString());
183
const sessions = await authState.keys.get('session', addrs);
184
for (const jid of jids) {
185
const signalId = (0, Utils_1.jidToSignalProtocolAddress)(jid).toString();
186
if (!sessions[signalId]) {
187
jidsRequiringFetch.push(jid);
188
}
189
}
190
}
191
if (jidsRequiringFetch.length) {
192
logger.debug({ jidsRequiringFetch }, 'fetching sessions');
193
const result = await query({
194
tag: 'iq',
195
attrs: {
196
xmlns: 'encrypt',
197
type: 'get',
198
to: WABinary_1.S_WHATSAPP_NET,
199
},
200
content: [
201
{
202
tag: 'key',
203
attrs: {},
204
content: jidsRequiringFetch.map(jid => ({
205
tag: 'user',
206
attrs: { jid },
207
}))
208
}
209
]
210
});
211
await (0, Utils_1.parseAndInjectE2ESessions)(result, authState);
212
didFetchNewSession = true;
213
}
214
return didFetchNewSession;
215
};
216
const createParticipantNodes = async (jids, bytes, extraAttrs) => {
217
let shouldIncludeDeviceIdentity = false;
218
const nodes = await Promise.all(jids.map(async (jid) => {
219
const { type, ciphertext } = await (0, Utils_1.encryptSignalProto)(jid, bytes, authState);
220
if (type === 'pkmsg') {
221
shouldIncludeDeviceIdentity = true;
222
}
223
const node = {
224
tag: 'to',
225
attrs: { jid },
226
content: [{
227
tag: 'enc',
228
attrs: {
229
v: '2',
230
type,
231
...extraAttrs || {}
232
},
233
content: ciphertext
234
}]
235
};
236
return node;
237
}));
238
return { nodes, shouldIncludeDeviceIdentity };
239
};
240
const relayMessage = async (jid, message, { messageId: msgId, participant, additionalAttributes, useUserDevicesCache, cachedGroupMetadata }) => {
241
const meId = authState.creds.me.id;
242
let shouldIncludeDeviceIdentity = false;
243
const { user, server } = (0, WABinary_1.jidDecode)(jid);
244
const isGroup = server === 'g.us';
245
msgId = msgId || (0, Utils_1.generateMessageID)();
246
useUserDevicesCache = useUserDevicesCache !== false;
247
const encodedMsg = (0, Utils_1.encodeWAMessage)(message);
248
const participants = [];
249
const destinationJid = (0, WABinary_1.jidEncode)(user, isGroup ? 'g.us' : 's.whatsapp.net');
250
const binaryNodeContent = [];
251
const devices = [];
252
if (participant) {
253
// when the retry request is not for a group
254
// only send to the specific device that asked for a retry
255
// otherwise the message is sent out to every device that should be a recipient
256
if (!isGroup) {
257
additionalAttributes = { ...additionalAttributes, device_fanout: 'false' };
258
}
259
const { user, device } = (0, WABinary_1.jidDecode)(participant.jid);
260
devices.push({ user, device });
261
}
262
await authState.keys.transaction(async () => {
263
if (isGroup) {
264
const { ciphertext, senderKeyDistributionMessageKey } = await (0, Utils_1.encryptSenderKeyMsgSignalProto)(destinationJid, encodedMsg, meId, authState);
265
const [groupData, senderKeyMap] = await Promise.all([
266
(async () => {
267
let groupData = cachedGroupMetadata ? await cachedGroupMetadata(jid) : undefined;
268
if (groupData) {
269
logger.trace({ jid, participants: groupData.participants.length }, 'using cached group metadata');
270
}
271
if (!groupData) {
272
groupData = await groupMetadata(jid);
273
}
274
return groupData;
275
})(),
276
(async () => {
277
if (!participant) {
278
const result = await authState.keys.get('sender-key-memory', [jid]);
279
return result[jid] || {};
280
}
281
return {};
282
})()
283
]);
284
if (!participant) {
285
const participantsList = groupData.participants.map(p => p.id);
286
const additionalDevices = await getUSyncDevices(participantsList, !!useUserDevicesCache, false);
287
devices.push(...additionalDevices);
288
}
289
const senderKeyJids = [];
290
// ensure a connection is established with every device
291
for (const { user, device } of devices) {
292
const jid = (0, WABinary_1.jidEncode)(user, 's.whatsapp.net', device);
293
if (!senderKeyMap[jid] || !!participant) {
294
senderKeyJids.push(jid);
295
// store that this person has had the sender keys sent to them
296
senderKeyMap[jid] = true;
297
}
298
}
299
// if there are some participants with whom the session has not been established
300
// if there are, we re-send the senderkey
301
if (senderKeyJids.length) {
302
logger.debug({ senderKeyJids }, 'sending new sender key');
303
const encSenderKeyMsg = (0, Utils_1.encodeWAMessage)({
304
senderKeyDistributionMessage: {
305
axolotlSenderKeyDistributionMessage: senderKeyDistributionMessageKey,
306
groupId: destinationJid
307
}
308
});
309
await assertSessions(senderKeyJids, false);
310
const result = await createParticipantNodes(senderKeyJids, encSenderKeyMsg);
311
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || result.shouldIncludeDeviceIdentity;
312
participants.push(...result.nodes);
313
}
314
binaryNodeContent.push({
315
tag: 'enc',
316
attrs: { v: '2', type: 'skmsg' },
317
content: ciphertext
318
});
319
await authState.keys.set({ 'sender-key-memory': { [jid]: senderKeyMap } });
320
}
321
else {
322
const { user: meUser } = (0, WABinary_1.jidDecode)(meId);
323
const encodedMeMsg = (0, Utils_1.encodeWAMessage)({
324
deviceSentMessage: {
325
destinationJid,
326
message
327
}
328
});
329
if (!participant) {
330
devices.push({ user });
331
devices.push({ user: meUser });
332
const additionalDevices = await getUSyncDevices([meId, jid], !!useUserDevicesCache, true);
333
devices.push(...additionalDevices);
334
}
335
const allJids = [];
336
const meJids = [];
337
const otherJids = [];
338
for (const { user, device } of devices) {
339
const jid = (0, WABinary_1.jidEncode)(user, 's.whatsapp.net', device);
340
const isMe = user === meUser;
341
if (isMe) {
342
meJids.push(jid);
343
}
344
else {
345
otherJids.push(jid);
346
}
347
allJids.push(jid);
348
}
349
await assertSessions(allJids, false);
350
const [{ nodes: meNodes, shouldIncludeDeviceIdentity: s1 }, { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 }] = await Promise.all([
351
createParticipantNodes(meJids, encodedMeMsg),
352
createParticipantNodes(otherJids, encodedMsg)
353
]);
354
participants.push(...meNodes);
355
participants.push(...otherNodes);
356
shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2;
357
}
358
if (participants.length) {
359
binaryNodeContent.push({
360
tag: 'participants',
361
attrs: {},
362
content: participants
363
});
364
}
365
const stanza = {
366
tag: 'message',
367
attrs: {
368
id: msgId,
369
type: 'text',
370
...(additionalAttributes || {})
371
},
372
content: binaryNodeContent
373
};
374
// if the participant to send to is explicitly specified (generally retry recp)
375
// ensure the message is only sent to that person
376
// if a retry receipt is sent to everyone -- it'll fail decryption for everyone else who received the msg
377
if (participant) {
378
if ((0, WABinary_1.isJidGroup)(destinationJid)) {
379
stanza.attrs.to = destinationJid;
380
stanza.attrs.participant = participant.jid;
381
}
382
else if ((0, WABinary_1.areJidsSameUser)(participant.jid, meId)) {
383
stanza.attrs.to = participant.jid;
384
stanza.attrs.recipient = destinationJid;
385
}
386
else {
387
stanza.attrs.to = participant.jid;
388
}
389
}
390
else {
391
stanza.attrs.to = destinationJid;
392
}
393
if (shouldIncludeDeviceIdentity) {
394
stanza.content.push({
395
tag: 'device-identity',
396
attrs: {},
397
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
398
});
399
logger.debug({ jid }, 'adding device identity');
400
}
401
logger.debug({ msgId }, `sending message to ${participants.length} devices`);
402
await sendNode(stanza);
403
});
404
return msgId;
405
};
406
const getPrivacyTokens = async (jids) => {
407
const t = (0, Utils_1.unixTimestampSeconds)().toString();
408
const result = await query({
409
tag: 'iq',
410
attrs: {
411
to: WABinary_1.S_WHATSAPP_NET,
412
type: 'set',
413
xmlns: 'privacy'
414
},
415
content: [
416
{
417
tag: 'tokens',
418
attrs: {},
419
content: jids.map(jid => ({
420
tag: 'token',
421
attrs: {
422
jid: (0, WABinary_1.jidNormalizedUser)(jid),
423
t,
424
type: 'trusted_contact'
425
}
426
}))
427
}
428
]
429
});
430
return result;
431
};
432
const waUploadToServer = (0, Utils_1.getWAUploadToServer)(config, refreshMediaConn);
433
const waitForMsgMediaUpdate = (0, Utils_1.bindWaitForEvent)(ev, 'messages.media-update');
434
return {
435
...sock,
436
getPrivacyTokens,
437
assertSessions,
438
relayMessage,
439
sendReceipt,
440
sendReceipts,
441
readMessages,
442
refreshMediaConn,
443
waUploadToServer,
444
fetchPrivacySettings,
445
updateMediaMessage: async (message) => {
446
const content = (0, Utils_1.assertMediaContent)(message.message);
447
const mediaKey = content.mediaKey;
448
const meId = authState.creds.me.id;
449
const node = (0, Utils_1.encryptMediaRetryRequest)(message.key, mediaKey, meId);
450
let error = undefined;
451
await Promise.all([
452
sendNode(node),
453
waitForMsgMediaUpdate(update => {
454
const result = update.find(c => c.key.id === message.key.id);
455
if (result) {
456
if (result.error) {
457
error = result.error;
458
}
459
else {
460
try {
461
const media = (0, Utils_1.decryptMediaRetryData)(result.media, mediaKey, result.key.id);
462
if (media.result !== WAProto_1.proto.MediaRetryNotification.ResultType.SUCCESS) {
463
const resultStr = WAProto_1.proto.MediaRetryNotification.ResultType[media.result];
464
throw new boom_1.Boom(`Media re-upload failed by device (${resultStr})`, { data: media, statusCode: (0, Utils_1.getStatusCodeForMediaRetry)(media.result) || 404 });
465
}
466
content.directPath = media.directPath;
467
content.url = (0, Utils_1.getUrlFromDirectPath)(content.directPath);
468
logger.debug({ directPath: media.directPath, key: result.key }, 'media update successful');
469
}
470
catch (err) {
471
error = err;
472
}
473
}
474
return true;
475
}
476
})
477
]);
478
if (error) {
479
throw error;
480
}
481
ev.emit('messages.update', [
482
{ key: message.key, update: { message: message.message } }
483
]);
484
return message;
485
},
486
sendMessage: async (jid, content, options = {}) => {
487
var _a, _b;
488
const userJid = authState.creds.me.id;
489
if (typeof content === 'object' &&
490
'disappearingMessagesInChat' in content &&
491
typeof content['disappearingMessagesInChat'] !== 'undefined' &&
492
(0, WABinary_1.isJidGroup)(jid)) {
493
const { disappearingMessagesInChat } = content;
494
const value = typeof disappearingMessagesInChat === 'boolean' ?
495
(disappearingMessagesInChat ? Defaults_1.WA_DEFAULT_EPHEMERAL : 0) :
496
disappearingMessagesInChat;
497
await groupToggleEphemeral(jid, value);
498
}
499
else {
500
const fullMsg = await (0, Utils_1.generateWAMessage)(jid, content, {
501
logger,
502
userJid,
503
getUrlInfo: text => (0, link_preview_1.getUrlInfo)(text, { thumbnailWidth: linkPreviewImageThumbnailWidth, timeoutMs: 3000 }),
504
upload: waUploadToServer,
505
mediaCache: config.mediaCache,
506
...options,
507
});
508
const isDeleteMsg = 'delete' in content && !!content.delete;
509
const additionalAttributes = {};
510
// required for delete
511
if (isDeleteMsg) {
512
// if the chat is a group, and I am not the author, then delete the message as an admin
513
if ((0, WABinary_1.isJidGroup)((_a = content.delete) === null || _a === void 0 ? void 0 : _a.remoteJid) && !((_b = content.delete) === null || _b === void 0 ? void 0 : _b.fromMe)) {
514
additionalAttributes.edit = '8';
515
}
516
else {
517
additionalAttributes.edit = '7';
518
}
519
}
520
fullMsg.message = (0, Utils_1.patchMessageForMdIfRequired)(fullMsg.message);
521
await relayMessage(jid, fullMsg.message, { messageId: fullMsg.key.id, cachedGroupMetadata: options.cachedGroupMetadata, additionalAttributes });
522
if (config.emitOwnEvents) {
523
process.nextTick(() => {
524
upsertMessage(fullMsg, 'append');
525
});
526
}
527
return fullMsg;
528
}
529
}
530
};
531
};
532
exports.makeMessagesSocket = makeMessagesSocket;
533
534