@queenanya/baileys 7.4.7 → 7.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/README.md +26 -26
  2. package/WAProto/index.js +97194 -67957
  3. package/lib/Defaults/baileys-version.json +1 -1
  4. package/lib/Defaults/index.d.ts +231 -1
  5. package/lib/Defaults/index.js +23 -11
  6. package/lib/Defaults/phonenumber-mcc.json +223 -0
  7. package/lib/Socket/Client/index.d.ts +3 -2
  8. package/lib/Socket/Client/index.js +3 -2
  9. package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
  10. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  11. package/lib/Socket/Client/{websocket.d.ts → web-socket-client.d.ts} +1 -1
  12. package/lib/Socket/Client/{websocket.js → web-socket-client.js} +2 -2
  13. package/lib/Socket/business.d.ts +22 -29
  14. package/lib/Socket/chats.d.ts +3 -14
  15. package/lib/Socket/chats.js +48 -76
  16. package/lib/Socket/groups.d.ts +4 -21
  17. package/lib/Socket/groups.js +1 -12
  18. package/lib/Socket/index.d.ts +32 -37
  19. package/lib/Socket/index.js +2 -2
  20. package/lib/Socket/messages-recv.d.ts +22 -28
  21. package/lib/Socket/messages-recv.js +180 -291
  22. package/lib/Socket/messages-send.d.ts +13 -23
  23. package/lib/Socket/messages-send.js +76 -110
  24. package/lib/Socket/newsletter.d.ts +7 -17
  25. package/lib/Socket/newsletter.js +54 -67
  26. package/lib/Socket/registration.d.ts +267 -0
  27. package/lib/Socket/registration.js +166 -0
  28. package/lib/Socket/socket.d.ts +1 -3
  29. package/lib/Socket/socket.js +17 -15
  30. package/lib/Store/make-cache-manager-store.d.ts +1 -2
  31. package/lib/Store/make-in-memory-store.js +11 -13
  32. package/lib/Store/make-ordered-dictionary.js +2 -2
  33. package/lib/Types/Auth.d.ts +6 -1
  34. package/lib/Types/Call.d.ts +1 -1
  35. package/lib/Types/Chat.d.ts +7 -12
  36. package/lib/Types/Contact.d.ts +1 -1
  37. package/lib/Types/Events.d.ts +2 -44
  38. package/lib/Types/GroupMetadata.d.ts +1 -3
  39. package/lib/Types/Label.d.ts +0 -11
  40. package/lib/Types/Message.d.ts +30 -37
  41. package/lib/Types/Newsletter.d.ts +13 -0
  42. package/lib/Types/Newsletter.js +15 -1
  43. package/lib/Types/Socket.d.ts +3 -10
  44. package/lib/Types/index.d.ts +0 -8
  45. package/lib/Utils/auth-utils.js +7 -1
  46. package/lib/Utils/chat-utils.d.ts +4 -5
  47. package/lib/Utils/chat-utils.js +20 -41
  48. package/lib/Utils/crypto.d.ts +1 -2
  49. package/lib/Utils/crypto.js +2 -4
  50. package/lib/Utils/decode-wa-message.d.ts +0 -1
  51. package/lib/Utils/decode-wa-message.js +14 -34
  52. package/lib/Utils/event-buffer.js +8 -14
  53. package/lib/Utils/generics.d.ts +13 -37
  54. package/lib/Utils/generics.js +18 -102
  55. package/lib/Utils/history.d.ts +2 -6
  56. package/lib/Utils/history.js +0 -3
  57. package/lib/Utils/index.d.ts +0 -1
  58. package/lib/Utils/index.js +0 -1
  59. package/lib/Utils/link-preview.js +1 -24
  60. package/lib/Utils/logger.d.ts +3 -1
  61. package/lib/Utils/make-mutex.js +0 -1
  62. package/lib/Utils/messages-media.d.ts +2 -3
  63. package/lib/Utils/messages-media.js +32 -17
  64. package/lib/Utils/messages.d.ts +0 -1
  65. package/lib/Utils/messages.js +72 -67
  66. package/lib/Utils/noise-handler.d.ts +3 -3
  67. package/lib/Utils/noise-handler.js +12 -7
  68. package/lib/Utils/process-message.d.ts +2 -3
  69. package/lib/Utils/process-message.js +21 -55
  70. package/lib/Utils/signal.js +16 -23
  71. package/lib/Utils/use-multi-file-auth-state.js +3 -17
  72. package/lib/Utils/validate-connection.d.ts +1 -0
  73. package/lib/Utils/validate-connection.js +44 -10
  74. package/lib/WABinary/constants.js +5 -5
  75. package/lib/WABinary/decode.d.ts +2 -3
  76. package/lib/WABinary/decode.js +4 -6
  77. package/lib/WABinary/encode.d.ts +2 -1
  78. package/lib/WABinary/encode.js +6 -8
  79. package/lib/WABinary/generic-utils.d.ts +0 -1
  80. package/lib/WABinary/jid-utils.d.ts +3 -3
  81. package/lib/WABinary/jid-utils.js +5 -5
  82. package/lib/WAM/BinaryInfo.d.ts +2 -3
  83. package/lib/WAM/constants.d.ts +2 -3
  84. package/lib/WAM/encode.d.ts +0 -1
  85. package/lib/WAM/encode.js +2 -2
  86. package/package.json +37 -39
  87. /package/lib/Socket/Client/{types.d.ts → abstract-socket-client.d.ts} +0 -0
  88. /package/lib/Socket/Client/{types.js → abstract-socket-client.js} +0 -0
@@ -17,9 +17,9 @@ const WABinary_1 = require("../WABinary");
17
17
  const groups_1 = require("./groups");
18
18
  const messages_send_1 = require("./messages-send");
19
19
  const makeMessagesRecvSocket = (config) => {
20
- const { logger, retryRequestDelayMs, maxMsgRetryCount, ignoreMsgLoading, getMessage, shouldIgnoreJid } = config;
20
+ const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config;
21
21
  const sock = (0, messages_send_1.makeMessagesSocket)(config);
22
- const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, sendPeerDataOperationMessage, } = sock;
22
+ const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, getUSyncDevices, createParticipantNodes } = sock;
23
23
  /** this mutex ensures that each retryRequest will wait for the previous one to finish */
24
24
  const retryMutex = (0, make_mutex_1.makeMutex)();
25
25
  const msgRetryCache = config.msgRetryCounterCache || new node_cache_1.default({
@@ -30,10 +30,6 @@ const makeMessagesRecvSocket = (config) => {
30
30
  stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.CALL_OFFER,
31
31
  useClones: false
32
32
  });
33
- const placeholderResendCache = config.placeholderResendCache || new node_cache_1.default({
34
- stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
35
- useClones: false
36
- });
37
33
  let sendActiveReceipts = false;
38
34
  const sendMessageAck = async ({ tag, attrs, content }) => {
39
35
  const stanza = {
@@ -59,6 +55,58 @@ const makeMessagesRecvSocket = (config) => {
59
55
  logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
60
56
  await sendNode(stanza);
61
57
  };
58
+ const offerCall = async (toJid, isVideo = false) => {
59
+ const callId = (0, crypto_1.randomBytes)(16).toString('hex').toUpperCase().substring(0, 64);
60
+ const offerContent = [];
61
+ offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '16000' }, content: undefined });
62
+ offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '8000' }, content: undefined });
63
+ if (isVideo) {
64
+ offerContent.push({
65
+ tag: 'video',
66
+ attrs: { enc: 'vp8', dec: 'vp8', orientation: '0', 'screen_width': '1920', 'screen_height': '1080', 'device_orientation': '0' },
67
+ content: undefined
68
+ });
69
+ }
70
+ offerContent.push({ tag: 'net', attrs: { medium: '3' }, content: undefined });
71
+ offerContent.push({ tag: 'capability', attrs: { ver: '1' }, content: new Uint8Array([1, 4, 255, 131, 207, 4]) });
72
+ offerContent.push({ tag: 'encopt', attrs: { keygen: '2' }, content: undefined });
73
+ const encKey = (0, crypto_1.randomBytes)(32);
74
+ const devices = (await getUSyncDevices([toJid], true, false)).map(({ user, device }) => (0, WABinary_1.jidEncode)(user, 's.whatsapp.net', device));
75
+ await assertSessions(devices, true);
76
+ const { nodes: destinations, shouldIncludeDeviceIdentity } = await createParticipantNodes(devices, {
77
+ call: {
78
+ callKey: new Uint8Array(encKey)
79
+ }
80
+ }, { count: '0' });
81
+ offerContent.push({ tag: 'destination', attrs: {}, content: destinations });
82
+ if (shouldIncludeDeviceIdentity) {
83
+ offerContent.push({
84
+ tag: 'device-identity',
85
+ attrs: {},
86
+ content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
87
+ });
88
+ }
89
+ const stanza = ({
90
+ tag: 'call',
91
+ attrs: {
92
+ id: (0, Utils_1.generateMessageIDV2)(),
93
+ to: toJid,
94
+ },
95
+ content: [{
96
+ tag: 'offer',
97
+ attrs: {
98
+ 'call-id': callId,
99
+ 'call-creator': authState.creds.me.id,
100
+ },
101
+ content: offerContent,
102
+ }],
103
+ });
104
+ await query(stanza);
105
+ return {
106
+ id: callId,
107
+ to: toJid
108
+ };
109
+ };
62
110
  const rejectCall = async (callId, callFrom) => {
63
111
  const stanza = ({
64
112
  tag: 'call',
@@ -79,24 +127,16 @@ const makeMessagesRecvSocket = (config) => {
79
127
  await query(stanza);
80
128
  };
81
129
  const sendRetryRequest = async (node, forceIncludeKeys = false) => {
82
- const { fullMessage } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '');
83
- const { key: msgKey } = fullMessage;
84
- const msgId = msgKey.id;
85
- const key = `${msgId}:${msgKey === null || msgKey === void 0 ? void 0 : msgKey.participant}`;
86
- let retryCount = msgRetryCache.get(key) || 0;
130
+ const msgId = node.attrs.id;
131
+ let retryCount = msgRetryCache.get(msgId) || 0;
87
132
  if (retryCount >= maxMsgRetryCount) {
88
133
  logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');
89
- msgRetryCache.del(key);
134
+ msgRetryCache.del(msgId);
90
135
  return;
91
136
  }
92
137
  retryCount += 1;
93
- msgRetryCache.set(key, retryCount);
138
+ msgRetryCache.set(msgId, retryCount);
94
139
  const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;
95
- if (retryCount === 1) {
96
- //request a resend via phone
97
- const msgId = await requestPlaceholderResend(msgKey);
98
- logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`);
99
- }
100
140
  const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);
101
141
  await authState.keys.transaction(async () => {
102
142
  const receipt = {
@@ -175,8 +215,6 @@ const makeMessagesRecvSocket = (config) => {
175
215
  }
176
216
  };
177
217
  const handleGroupNotification = (participant, child, msg) => {
178
- var _a, _b, _c, _d;
179
- const participantJid = ((_b = (_a = (0, WABinary_1.getBinaryNodeChild)(child, 'participant')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.jid) || participant;
180
218
  switch (child === null || child === void 0 ? void 0 : child.tag) {
181
219
  case 'create':
182
220
  const metadata = (0, groups_1.extractGroupMetadata)(child);
@@ -202,11 +240,6 @@ const makeMessagesRecvSocket = (config) => {
202
240
  }
203
241
  };
204
242
  break;
205
- case 'modify':
206
- const oldNumber = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
207
- msg.messageStubParameters = oldNumber || [];
208
- msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
209
- break;
210
243
  case 'promote':
211
244
  case 'demote':
212
245
  case 'remove':
@@ -228,11 +261,6 @@ const makeMessagesRecvSocket = (config) => {
228
261
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;
229
262
  msg.messageStubParameters = [child.attrs.subject];
230
263
  break;
231
- case 'description':
232
- const description = (_d = (_c = (0, WABinary_1.getBinaryNodeChild)(child, 'body')) === null || _c === void 0 ? void 0 : _c.content) === null || _d === void 0 ? void 0 : _d.toString();
233
- msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION;
234
- msg.messageStubParameters = description ? [description] : undefined;
235
- break;
236
264
  case 'announcement':
237
265
  case 'not_announcement':
238
266
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;
@@ -261,59 +289,6 @@ const makeMessagesRecvSocket = (config) => {
261
289
  msg.messageStubParameters = [approvalMode.attrs.state];
262
290
  }
263
291
  break;
264
- case 'created_membership_requests':
265
- msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
266
- msg.messageStubParameters = [participantJid, 'created', child.attrs.request_method];
267
- break;
268
- case 'revoked_membership_requests':
269
- const isDenied = (0, WABinary_1.areJidsSameUser)(participantJid, participant);
270
- msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
271
- msg.messageStubParameters = [participantJid, isDenied ? 'revoked' : 'rejected'];
272
- break;
273
- }
274
- };
275
- const handleNewsletterNotification = (id, node) => {
276
- const messages = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
277
- const message = (0, WABinary_1.getBinaryNodeChild)(messages, 'message');
278
- const server_id = message.attrs.server_id;
279
- const reactionsList = (0, WABinary_1.getBinaryNodeChild)(message, 'reactions');
280
- const viewsList = (0, WABinary_1.getBinaryNodeChildren)(message, 'views_count');
281
- if (reactionsList) {
282
- const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionsList, 'reaction');
283
- if (reactions.length === 0) {
284
- ev.emit('newsletter.reaction', { id, server_id, reaction: { removed: true } });
285
- }
286
- reactions.forEach(item => {
287
- var _a, _b;
288
- ev.emit('newsletter.reaction', { id, server_id, reaction: { code: (_a = item.attrs) === null || _a === void 0 ? void 0 : _a.code, count: +((_b = item.attrs) === null || _b === void 0 ? void 0 : _b.count) } });
289
- });
290
- }
291
- if (viewsList.length) {
292
- viewsList.forEach(item => {
293
- ev.emit('newsletter.view', { id, server_id, count: +item.attrs.count });
294
- });
295
- }
296
- };
297
- const handleMexNewsletterNotification = (id, node) => {
298
- var _a;
299
- const operation = node === null || node === void 0 ? void 0 : node.attrs.op_name;
300
- const content = JSON.parse((_a = node === null || node === void 0 ? void 0 : node.content) === null || _a === void 0 ? void 0 : _a.toString());
301
- let contentPath;
302
- if (operation === Types_1.MexOperations.PROMOTE || operation === Types_1.MexOperations.DEMOTE) {
303
- let action;
304
- if (operation === Types_1.MexOperations.PROMOTE) {
305
- action = 'promote';
306
- contentPath = content.data[Types_1.XWAPaths.PROMOTE];
307
- }
308
- if (operation === Types_1.MexOperations.DEMOTE) {
309
- action = 'demote';
310
- contentPath = content.data[Types_1.XWAPaths.DEMOTE];
311
- }
312
- ev.emit('newsletter-participants.update', { id, author: contentPath.actor.pn, user: contentPath.user.pn, new_role: contentPath.user_new_role, action });
313
- }
314
- if (operation === Types_1.MexOperations.UPDATE) {
315
- contentPath = content.data[Types_1.XWAPaths.METADATA_UPDATE];
316
- ev.emit('newsletter-settings.update', { id, update: contentPath.thread_metadata.settings });
317
292
  }
318
293
  };
319
294
  const processNotification = async (node) => {
@@ -336,12 +311,6 @@ const makeMessagesRecvSocket = (config) => {
336
311
  logger.debug({ jid }, 'got privacy token update');
337
312
  }
338
313
  break;
339
- case 'newsletter':
340
- handleNewsletterNotification(node.attrs.from, child);
341
- break;
342
- case 'mex':
343
- handleMexNewsletterNotification(node.attrs.from, child);
344
- break;
345
314
  case 'w:gp2':
346
315
  handleGroupNotification(node.attrs.participant, child, result);
347
316
  break;
@@ -373,7 +342,7 @@ const makeMessagesRecvSocket = (config) => {
373
342
  const setPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'set');
374
343
  const delPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'delete');
375
344
  ev.emit('contacts.update', [{
376
- id: (0, WABinary_1.jidNormalizedUser)((_a = node === null || node === void 0 ? void 0 : node.attrs) === null || _a === void 0 ? void 0 : _a.from) || ((_c = (_b = (setPicture || delPicture)) === null || _b === void 0 ? void 0 : _b.attrs) === null || _c === void 0 ? void 0 : _c.hash) || '',
345
+ id: (0, WABinary_1.jidNormalizedUser)((_a = node === null || node === void 0 ? void 0 : node.attrs) === null || _a === void 0 ? void 0 : _a.jid) || ((_c = (_b = (setPicture || delPicture)) === null || _b === void 0 ? void 0 : _b.attrs) === null || _c === void 0 ? void 0 : _c.hash) || '',
377
346
  imgUrl: setPicture ? 'changed' : 'removed'
378
347
  }]);
379
348
  if ((0, WABinary_1.isJidGroup)(from)) {
@@ -418,7 +387,7 @@ const makeMessagesRecvSocket = (config) => {
418
387
  const ref = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_ref'));
419
388
  const primaryIdentityPublicKey = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'primary_identity_pub'));
420
389
  const primaryEphemeralPublicKeyWrapped = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'));
421
- const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped);
390
+ const codePairingPublicKey = decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped);
422
391
  const companionSharedKey = Utils_1.Curve.sharedKey(authState.creds.pairingEphemeralKeyPair.private, codePairingPublicKey);
423
392
  const random = (0, crypto_1.randomBytes)(32);
424
393
  const linkCodeSalt = (0, crypto_1.randomBytes)(32);
@@ -475,10 +444,10 @@ const makeMessagesRecvSocket = (config) => {
475
444
  return result;
476
445
  }
477
446
  };
478
- async function decipherLinkPublicKey(data) {
447
+ function decipherLinkPublicKey(data) {
479
448
  const buffer = toRequiredBuffer(data);
480
449
  const salt = buffer.slice(0, 32);
481
- const secretKey = await (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
450
+ const secretKey = (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
482
451
  const iv = buffer.slice(32, 48);
483
452
  const payload = buffer.slice(48, 80);
484
453
  return (0, Utils_1.aesDecryptCTR)(payload, secretKey, iv);
@@ -513,7 +482,8 @@ const makeMessagesRecvSocket = (config) => {
513
482
  await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } });
514
483
  }
515
484
  logger.debug({ participant, sendToAll }, 'forced new session for retry recp');
516
- for (const [i, msg] of msgs.entries()) {
485
+ for (let i = 0; i < msgs.length; i++) {
486
+ const msg = msgs[i];
517
487
  if (msg) {
518
488
  updateSendMessageAgainCount(ids[i], participant);
519
489
  const msgRelayOpts = { messageId: ids[i] };
@@ -556,63 +526,59 @@ const makeMessagesRecvSocket = (config) => {
556
526
  const items = (0, WABinary_1.getBinaryNodeChildren)(content[0], 'item');
557
527
  ids.push(...items.map(i => i.attrs.id));
558
528
  }
559
- try {
560
- await Promise.all([
561
- processingMutex.mutex(async () => {
562
- const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);
563
- if (typeof status !== 'undefined' &&
564
- (
565
- // basically, we only want to know when a message from us has been delivered to/read by the other person
566
- // or another device of ours has read some messages
567
- status > WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ||
568
- !isNodeFromMe)) {
569
- if ((0, WABinary_1.isJidGroup)(remoteJid) || (0, WABinary_1.isJidStatusBroadcast)(remoteJid)) {
570
- if (attrs.participant) {
571
- const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
572
- ev.emit('message-receipt.update', ids.map(id => ({
573
- key: { ...key, id },
574
- receipt: {
575
- userJid: (0, WABinary_1.jidNormalizedUser)(attrs.participant),
576
- [updateKey]: +attrs.t
577
- }
578
- })));
579
- }
580
- }
581
- else {
582
- ev.emit('messages.update', ids.map(id => ({
529
+ await Promise.all([
530
+ processingMutex.mutex(async () => {
531
+ const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);
532
+ if (typeof status !== 'undefined' &&
533
+ (
534
+ // basically, we only want to know when a message from us has been delivered to/read by the other person
535
+ // or another device of ours has read some messages
536
+ status > WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ||
537
+ !isNodeFromMe)) {
538
+ if ((0, WABinary_1.isJidGroup)(remoteJid)) {
539
+ if (attrs.participant) {
540
+ const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
541
+ ev.emit('message-receipt.update', ids.map(id => ({
583
542
  key: { ...key, id },
584
- update: { status }
543
+ receipt: {
544
+ userJid: (0, WABinary_1.jidNormalizedUser)(attrs.participant),
545
+ [updateKey]: +attrs.t
546
+ }
585
547
  })));
586
548
  }
587
549
  }
588
- if (attrs.type === 'retry') {
589
- // correctly set who is asking for the retry
590
- key.participant = key.participant || attrs.from;
591
- const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
592
- if (willSendMessageAgain(ids[0], key.participant)) {
593
- if (key.fromMe) {
594
- try {
595
- logger.debug({ attrs, key }, 'recv retry request');
596
- await sendMessagesAgain(key, ids, retryNode);
597
- }
598
- catch (error) {
599
- logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
600
- }
550
+ else {
551
+ ev.emit('messages.update', ids.map(id => ({
552
+ key: { ...key, id },
553
+ update: { status }
554
+ })));
555
+ }
556
+ }
557
+ if (attrs.type === 'retry') {
558
+ // correctly set who is asking for the retry
559
+ key.participant = key.participant || attrs.from;
560
+ const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
561
+ if (willSendMessageAgain(ids[0], key.participant)) {
562
+ if (key.fromMe) {
563
+ try {
564
+ logger.debug({ attrs, key }, 'recv retry request');
565
+ await sendMessagesAgain(key, ids, retryNode);
601
566
  }
602
- else {
603
- logger.info({ attrs, key }, 'recv retry for not fromMe message');
567
+ catch (error) {
568
+ logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
604
569
  }
605
570
  }
606
571
  else {
607
- logger.info({ attrs, key }, 'will not send message again, as sent too many times');
572
+ logger.info({ attrs, key }, 'recv retry for not fromMe message');
608
573
  }
609
574
  }
610
- })
611
- ]);
612
- }
613
- finally {
614
- await sendMessageAck(node);
615
- }
575
+ else {
576
+ logger.info({ attrs, key }, 'will not send message again, as sent too many times');
577
+ }
578
+ }
579
+ }),
580
+ sendMessageAck(node)
581
+ ]);
616
582
  };
617
583
  const handleNotification = async (node) => {
618
584
  const remoteJid = node.attrs.from;
@@ -621,164 +587,89 @@ const makeMessagesRecvSocket = (config) => {
621
587
  await sendMessageAck(node);
622
588
  return;
623
589
  }
624
- try {
625
- await Promise.all([
626
- processingMutex.mutex(async () => {
627
- var _a;
628
- const msg = await processNotification(node);
629
- if (msg) {
630
- const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
631
- msg.key = {
632
- remoteJid,
633
- fromMe,
634
- participant: node.attrs.participant,
635
- id: node.attrs.id,
636
- ...(msg.key || {})
637
- };
638
- (_a = msg.participant) !== null && _a !== void 0 ? _a : (msg.participant = node.attrs.participant);
639
- msg.messageTimestamp = +node.attrs.t;
640
- const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
641
- await upsertMessage(fullMsg, 'append');
642
- }
643
- })
644
- ]);
645
- }
646
- finally {
647
- await sendMessageAck(node);
648
- }
590
+ await Promise.all([
591
+ processingMutex.mutex(async () => {
592
+ var _a;
593
+ const msg = await processNotification(node);
594
+ if (msg) {
595
+ const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
596
+ msg.key = {
597
+ remoteJid,
598
+ fromMe,
599
+ participant: node.attrs.participant,
600
+ id: node.attrs.id,
601
+ ...(msg.key || {})
602
+ };
603
+ (_a = msg.participant) !== null && _a !== void 0 ? _a : (msg.participant = node.attrs.participant);
604
+ msg.messageTimestamp = +node.attrs.t;
605
+ const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
606
+ await upsertMessage(fullMsg, 'append');
607
+ }
608
+ }),
609
+ sendMessageAck(node)
610
+ ]);
649
611
  };
650
612
  const handleMessage = async (node) => {
651
- var _a, _b, _c;
613
+ var _a, _b;
652
614
  if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
653
615
  logger.debug({ key: node.attrs.key }, 'ignored message');
654
616
  await sendMessageAck(node);
655
617
  return;
656
618
  }
657
- let response;
658
- if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable') && !(0, WABinary_1.getBinaryNodeChild)(node, 'enc')) {
659
- await sendMessageAck(node);
660
- const { key } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '').fullMessage;
661
- response = await requestPlaceholderResend(key);
662
- if (response === 'RESOLVED') {
663
- return;
664
- }
665
- logger.debug('received unavailable message, acked and requested resend from phone');
666
- }
667
- else {
668
- if (placeholderResendCache.get(node.attrs.id)) {
669
- placeholderResendCache.del(node.attrs.id);
670
- }
671
- }
672
619
  const { fullMessage: msg, category, author, decrypt } = (0, Utils_1.decryptMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, logger);
673
- if (response && ((_a = msg === null || msg === void 0 ? void 0 : msg.messageStubParameters) === null || _a === void 0 ? void 0 : _a[0]) === Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT) {
674
- msg.messageStubParameters = [Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT, response];
675
- }
676
- if (((_c = (_b = msg.message) === null || _b === void 0 ? void 0 : _b.protocolMessage) === null || _c === void 0 ? void 0 : _c.type) === WAProto_1.proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER && node.attrs.sender_pn) {
677
- ev.emit('chats.phoneNumberShare', { lid: node.attrs.from, jid: node.attrs.sender_pn });
620
+ if (((_b = (_a = msg.message) === null || _a === void 0 ? void 0 : _a.protocolMessage) === null || _b === void 0 ? void 0 : _b.type) === WAProto_1.proto.Message.ProtocolMessage.Type.SHARE_PHONE_NUMBER) {
621
+ if (node.attrs.sender_pn) {
622
+ ev.emit('chats.phoneNumberShare', { lid: node.attrs.from, jid: node.attrs.sender_pn });
623
+ }
678
624
  }
679
- try {
680
- await Promise.all([
681
- processingMutex.mutex(async () => {
682
- await decrypt();
683
- // message failed to decrypt
684
- if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
685
- retryMutex.mutex(async () => {
686
- if (ws.isOpen) {
687
- if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
688
- return;
689
- }
690
- const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
691
- await sendRetryRequest(node, !encNode);
692
- if (retryRequestDelayMs) {
693
- await (0, Utils_1.delay)(retryRequestDelayMs);
694
- }
695
- }
696
- else {
697
- logger.debug({ node }, 'connection closed, ignoring retry req');
625
+ await Promise.all([
626
+ processingMutex.mutex(async () => {
627
+ await decrypt();
628
+ // message failed to decrypt
629
+ if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
630
+ retryMutex.mutex(async () => {
631
+ if (ws.isOpen) {
632
+ const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
633
+ await sendRetryRequest(node, !encNode);
634
+ if (retryRequestDelayMs) {
635
+ await (0, Utils_1.delay)(retryRequestDelayMs);
698
636
  }
699
- });
700
- }
701
- else {
702
- // no type in the receipt => message delivered
703
- let type = undefined;
704
- let participant = msg.key.participant;
705
- if (category === 'peer') { // special peer message
706
- type = 'peer_msg';
707
637
  }
708
- else if (msg.key.fromMe) { // message was sent by us from a different device
709
- type = 'sender';
710
- // need to specially handle this case
711
- if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {
712
- participant = author;
713
- }
714
- }
715
- else if (!sendActiveReceipts) {
716
- type = 'inactive';
638
+ else {
639
+ logger.debug({ node }, 'connection closed, ignoring retry req');
717
640
  }
718
- await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
719
- // send ack for history message
720
- const isAnyHistoryMsg = (0, Utils_1.getHistoryMsg)(msg.message);
721
- if (isAnyHistoryMsg) {
722
- const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);
723
- await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
641
+ });
642
+ }
643
+ else {
644
+ // no type in the receipt => message delivered
645
+ let type = undefined;
646
+ let participant = msg.key.participant;
647
+ if (category === 'peer') { // special peer message
648
+ type = 'peer_msg';
649
+ }
650
+ else if (msg.key.fromMe) { // message was sent by us from a different device
651
+ type = 'sender';
652
+ // need to specially handle this case
653
+ if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {
654
+ participant = author;
724
655
  }
725
656
  }
726
- (0, Utils_2.cleanMessage)(msg, authState.creds.me.id);
727
- await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
728
- })
729
- ]);
730
- }
731
- finally {
732
- await sendMessageAck(node);
733
- }
734
- };
735
- const fetchMessageHistory = async (count, oldestMsgKey, oldestMsgTimestamp) => {
736
- var _a;
737
- if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
738
- throw new boom_1.Boom('Not authenticated');
739
- }
740
- const pdoMessage = {
741
- historySyncOnDemandRequest: {
742
- chatJid: oldestMsgKey.remoteJid,
743
- oldestMsgFromMe: oldestMsgKey.fromMe,
744
- oldestMsgId: oldestMsgKey.id,
745
- oldestMsgTimestampMs: oldestMsgTimestamp,
746
- onDemandMsgCount: count
747
- },
748
- peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND
749
- };
750
- return sendPeerDataOperationMessage(pdoMessage);
751
- };
752
- const requestPlaceholderResend = async (messageKey) => {
753
- var _a;
754
- if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
755
- throw new boom_1.Boom('Not authenticated');
756
- }
757
- if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
758
- logger.debug('already requested resend', { messageKey });
759
- return;
760
- }
761
- else {
762
- placeholderResendCache.set(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id, true);
763
- }
764
- await (0, Utils_1.delay)(5000);
765
- if (!placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
766
- logger.debug('message received while resend requested', { messageKey });
767
- return 'RESOLVED';
768
- }
769
- const pdoMessage = {
770
- placeholderMessageResendRequest: [{
771
- messageKey
772
- }],
773
- peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.PLACEHOLDER_MESSAGE_RESEND
774
- };
775
- setTimeout(() => {
776
- if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
777
- logger.debug('PDO message without response after 15 seconds. Phone possibly offline', { messageKey });
778
- placeholderResendCache.del(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id);
779
- }
780
- }, 15000);
781
- return sendPeerDataOperationMessage(pdoMessage);
657
+ else if (!sendActiveReceipts) {
658
+ type = 'inactive';
659
+ }
660
+ await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
661
+ // send ack for history message
662
+ const isAnyHistoryMsg = (0, Utils_1.getHistoryMsg)(msg.message);
663
+ if (isAnyHistoryMsg) {
664
+ const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);
665
+ await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
666
+ }
667
+ }
668
+ (0, Utils_2.cleanMessage)(msg, authState.creds.me.id);
669
+ await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
670
+ }),
671
+ sendMessageAck(node)
672
+ ]);
782
673
  };
783
674
  const handleCall = async (node) => {
784
675
  const { attrs } = node;
@@ -807,15 +698,14 @@ const makeMessagesRecvSocket = (config) => {
807
698
  call.isGroup = existingCall.isGroup;
808
699
  }
809
700
  // delete data once call has ended
810
- if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
701
+ if (status === 'reject' || status === 'accept' || status === 'timeout') {
811
702
  callOfferCache.del(call.id);
812
703
  }
813
704
  ev.emit('call', [call]);
814
705
  await sendMessageAck(node);
815
706
  };
816
707
  const handleBadAck = async ({ attrs }) => {
817
- // const key: WAMessageKey = { remoteJid: attrs.from, fromMe: true, id: attrs.id }
818
- const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id, server_id: attrs === null || attrs === void 0 ? void 0 : attrs.server_id };
708
+ const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id };
819
709
  // current hypothesis is that if pash is sent in the ack
820
710
  // it means -- the message hasn't reached all devices yet
821
711
  // we'll retry sending the message here
@@ -910,9 +800,8 @@ const makeMessagesRecvSocket = (config) => {
910
800
  ...sock,
911
801
  sendMessageAck,
912
802
  sendRetryRequest,
913
- rejectCall,
914
- fetchMessageHistory,
915
- requestPlaceholderResend,
803
+ offerCall,
804
+ rejectCall
916
805
  };
917
806
  };
918
807
  exports.makeMessagesRecvSocket = makeMessagesRecvSocket;