@realvare/based 2.7.62 → 2.7.71
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.
- package/README.MD +1062 -282
- package/WAProto/WAProto.proto +1073 -244
- package/WAProto/index.d.ts +16282 -8183
- package/WAProto/index.js +76605 -50628
- package/engine-requirements.js +10 -10
- package/lib/Defaults/baileys-version.json +1 -1
- package/lib/Defaults/index.d.ts +4 -2
- package/lib/Defaults/index.js +8 -6
- package/lib/Signal/Group/ciphertext-message.d.ts +1 -1
- package/lib/Signal/Group/ciphertext-message.js +1 -1
- package/lib/Signal/Group/sender-message-key.d.ts +1 -1
- package/lib/Signal/Group/sender-message-key.js +1 -1
- package/lib/Signal/libsignal.d.ts +1 -1
- package/lib/Socket/business.d.ts +1 -1
- package/lib/Socket/business.js +1 -1
- package/lib/Socket/chats.d.ts +4 -1
- package/lib/Socket/chats.js +213 -36
- package/lib/Socket/groups.js +87 -15
- package/lib/Socket/index.js +9 -0
- package/lib/Socket/messages-interactive.js +259 -0
- package/lib/Socket/messages-recv.js +1473 -1228
- package/lib/Socket/messages-send.js +437 -469
- package/lib/Socket/socket.js +143 -26
- package/lib/Socket/usync.js +57 -4
- package/lib/Store/make-in-memory-store.js +28 -15
- package/lib/Types/Auth.d.ts +4 -0
- package/lib/Types/Message.d.ts +316 -6
- package/lib/Types/Message.js +1 -1
- package/lib/Types/Socket.d.ts +2 -0
- package/lib/Utils/cache-manager.d.ts +16 -0
- package/lib/Utils/cache-manager.js +22 -5
- package/lib/Utils/chat-utils.js +17 -13
- package/lib/Utils/decode-wa-message.js +1 -11
- package/lib/Utils/event-buffer.js +103 -2
- package/lib/Utils/generics.js +5 -6
- package/lib/Utils/index.d.ts +5 -0
- package/lib/Utils/index.js +3 -0
- package/lib/Utils/jid-validation.d.ts +2 -0
- package/lib/Utils/jid-validation.js +43 -10
- package/lib/Utils/link-preview.js +38 -28
- package/lib/Utils/messages-media.d.ts +1 -1
- package/lib/Utils/messages-media.js +22 -53
- package/lib/Utils/messages.js +653 -65
- package/lib/Utils/performance-config.d.ts +2 -0
- package/lib/Utils/performance-config.js +16 -7
- package/lib/Utils/process-message.js +124 -12
- package/lib/Utils/rate-limiter.js +15 -20
- package/lib/WABinary/generic-utils.js +5 -1
- package/lib/WABinary/jid-utils.d.ts +1 -0
- package/lib/WABinary/jid-utils.js +265 -5
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +75 -5
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -6
- package/lib/WAUSync/USyncQuery.js +64 -6
- package/lib/index.d.ts +1 -0
- package/lib/index.js +5 -4
- package/package.json +10 -15
- package/WAProto/index.ts.ts +0 -53473
|
@@ -1,1229 +1,1474 @@
|
|
|
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.makeMessagesRecvSocket = void 0;
|
|
7
|
-
const boom_1 = require("@hapi/boom");
|
|
8
|
-
const crypto_1 = require("crypto");
|
|
9
|
-
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
10
|
-
const WAProto_1 = require("../../WAProto");
|
|
11
|
-
const Defaults_1 = require("../Defaults");
|
|
12
|
-
const Types_1 = require("../Types");
|
|
13
|
-
const Utils_1 = require("../Utils");
|
|
14
|
-
const performance_config_1 = require("../Utils/performance-config");
|
|
15
|
-
const make_mutex_1 = require("../Utils/make-mutex");
|
|
16
|
-
const WABinary_1 = require("../WABinary");
|
|
17
|
-
const groups_1 = require("./groups");
|
|
18
|
-
const messages_send_1 = require("./messages-send");
|
|
19
|
-
const makeMessagesRecvSocket = (config) => {
|
|
20
|
-
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config;
|
|
21
|
-
const sock = (0, messages_send_1.makeMessagesSocket)(config);
|
|
22
|
-
const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, groupMetadata, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, createParticipantNodes, getUSyncDevices, sendPeerDataOperationMessage, } = sock;
|
|
23
|
-
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
|
24
|
-
const retryMutex = (0, make_mutex_1.makeMutex)();
|
|
25
|
-
const msgRetryCache = config.msgRetryCounterCache || new node_cache_1.default({
|
|
26
|
-
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
27
|
-
useClones: false
|
|
28
|
-
});
|
|
29
|
-
const callOfferCache = config.callOfferCache || new node_cache_1.default({
|
|
30
|
-
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.CALL_OFFER,
|
|
31
|
-
useClones: false
|
|
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
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (
|
|
240
|
-
await
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (
|
|
398
|
-
|
|
399
|
-
const
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
msg.
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
})
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
const
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const
|
|
655
|
-
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
if (
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
};
|
|
1008
|
-
const
|
|
1009
|
-
const
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
{
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
[
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
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.makeMessagesRecvSocket = void 0;
|
|
7
|
+
const boom_1 = require("@hapi/boom");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
10
|
+
const WAProto_1 = require("../../WAProto");
|
|
11
|
+
const Defaults_1 = require("../Defaults");
|
|
12
|
+
const Types_1 = require("../Types");
|
|
13
|
+
const Utils_1 = require("../Utils");
|
|
14
|
+
const performance_config_1 = require("../Utils/performance-config");
|
|
15
|
+
const make_mutex_1 = require("../Utils/make-mutex");
|
|
16
|
+
const WABinary_1 = require("../WABinary");
|
|
17
|
+
const groups_1 = require("./groups");
|
|
18
|
+
const messages_send_1 = require("./messages-send");
|
|
19
|
+
const makeMessagesRecvSocket = (config) => {
|
|
20
|
+
const { logger, retryRequestDelayMs, maxMsgRetryCount, getMessage, shouldIgnoreJid } = config;
|
|
21
|
+
const sock = (0, messages_send_1.makeMessagesSocket)(config);
|
|
22
|
+
const { ev, authState, ws, processingMutex, signalRepository, query, upsertMessage, resyncAppState, groupMetadata, onUnexpectedError, assertSessions, sendNode, relayMessage, sendReceipt, uploadPreKeys, createParticipantNodes, getUSyncDevices, sendPeerDataOperationMessage, } = sock;
|
|
23
|
+
/** this mutex ensures that each retryRequest will wait for the previous one to finish */
|
|
24
|
+
const retryMutex = (0, make_mutex_1.makeMutex)();
|
|
25
|
+
const msgRetryCache = config.msgRetryCounterCache || new node_cache_1.default({
|
|
26
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
|
|
27
|
+
useClones: false
|
|
28
|
+
});
|
|
29
|
+
const callOfferCache = config.callOfferCache || new node_cache_1.default({
|
|
30
|
+
stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.CALL_OFFER,
|
|
31
|
+
useClones: false
|
|
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
|
+
const notificationDedupCache = new node_cache_1.default({
|
|
38
|
+
stdTTL: 30,
|
|
39
|
+
useClones: false
|
|
40
|
+
});
|
|
41
|
+
const notificationStubDedupCache = new node_cache_1.default({
|
|
42
|
+
stdTTL: 10,
|
|
43
|
+
useClones: false
|
|
44
|
+
});
|
|
45
|
+
const sentMessageCache = new Map();
|
|
46
|
+
const sentMessageCacheMaxSize = 256;
|
|
47
|
+
const getSentMessageCacheKey = (remoteJid, id) => `${remoteJid}:${id}`;
|
|
48
|
+
const getCachedSentMessage = (remoteJid, id) => {
|
|
49
|
+
const cacheKey = getSentMessageCacheKey(remoteJid, id);
|
|
50
|
+
const existing = sentMessageCache.get(cacheKey);
|
|
51
|
+
if (!existing) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
sentMessageCache.delete(cacheKey);
|
|
55
|
+
sentMessageCache.set(cacheKey, existing);
|
|
56
|
+
return existing;
|
|
57
|
+
};
|
|
58
|
+
const cacheSentMessage = (remoteJid, id, message) => {
|
|
59
|
+
const cacheKey = getSentMessageCacheKey(remoteJid, id);
|
|
60
|
+
if (sentMessageCache.has(cacheKey)) {
|
|
61
|
+
sentMessageCache.delete(cacheKey);
|
|
62
|
+
}
|
|
63
|
+
sentMessageCache.set(cacheKey, message);
|
|
64
|
+
while (sentMessageCache.size > sentMessageCacheMaxSize) {
|
|
65
|
+
const oldestKey = sentMessageCache.keys().next().value;
|
|
66
|
+
if (!oldestKey) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
sentMessageCache.delete(oldestKey);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
let sendActiveReceipts = false;
|
|
73
|
+
const resolveJid = WABinary_1.resolveJid;
|
|
74
|
+
const sendMessageAck = async ({ tag, attrs, content }, errorCode) => {
|
|
75
|
+
const stanza = {
|
|
76
|
+
tag: 'ack',
|
|
77
|
+
attrs: {
|
|
78
|
+
id: attrs.id,
|
|
79
|
+
to: attrs.from,
|
|
80
|
+
class: tag
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
if (!!errorCode) {
|
|
84
|
+
stanza.attrs.error = errorCode.toString();
|
|
85
|
+
}
|
|
86
|
+
if (!!attrs.participant) {
|
|
87
|
+
stanza.attrs.participant = attrs.participant;
|
|
88
|
+
}
|
|
89
|
+
if (!!attrs.recipient) {
|
|
90
|
+
stanza.attrs.recipient = attrs.recipient;
|
|
91
|
+
}
|
|
92
|
+
if (!!attrs.type && (tag !== 'message' || (0, WABinary_1.getBinaryNodeChild)({ tag, attrs, content }, 'unavailable') || errorCode !== 0)) {
|
|
93
|
+
stanza.attrs.type = attrs.type;
|
|
94
|
+
}
|
|
95
|
+
if (tag === 'message' && (0, WABinary_1.getBinaryNodeChild)({ tag, attrs, content }, 'unavailable')) {
|
|
96
|
+
stanza.attrs.from = authState.creds.me.id;
|
|
97
|
+
}
|
|
98
|
+
logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
|
|
99
|
+
await sendNode(stanza);
|
|
100
|
+
};
|
|
101
|
+
// Add withAck wrapper for guaranteed acknowledgments so less ban tag ig
|
|
102
|
+
const withAck = (processFn) => async (node) => {
|
|
103
|
+
try {
|
|
104
|
+
await processFn(node);
|
|
105
|
+
} finally {
|
|
106
|
+
// Always send ack even on failure to allow potential retry (not sure bout this tho)
|
|
107
|
+
await sendMessageAck(node);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const offerCall = async (toJid, isVideo = false) => {
|
|
111
|
+
toJid = resolveJid(toJid);
|
|
112
|
+
const callId = (0, crypto_1.randomBytes)(16).toString('hex').toUpperCase().substring(0, 64);
|
|
113
|
+
const offerContent = [];
|
|
114
|
+
offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '16000' }, content: undefined });
|
|
115
|
+
offerContent.push({ tag: 'audio', attrs: { enc: 'opus', rate: '8000' }, content: undefined });
|
|
116
|
+
if (isVideo) {
|
|
117
|
+
offerContent.push({
|
|
118
|
+
tag: 'video',
|
|
119
|
+
attrs: {
|
|
120
|
+
orientation: '0',
|
|
121
|
+
'screen_width': '1920',
|
|
122
|
+
'screen_height': '1080',
|
|
123
|
+
'device_orientation': '0',
|
|
124
|
+
enc: 'vp8',
|
|
125
|
+
dec: 'vp8',
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
offerContent.push({ tag: 'net', attrs: { medium: '3' }, content: undefined });
|
|
130
|
+
offerContent.push({ tag: 'capability', attrs: { ver: '1' }, content: new Uint8Array([1, 4, 255, 131, 207, 4]) });
|
|
131
|
+
offerContent.push({ tag: 'encopt', attrs: { keygen: '2' }, content: undefined });
|
|
132
|
+
const encKey = (0, crypto_1.randomBytes)(32);
|
|
133
|
+
const devices = (await getUSyncDevices([toJid], true, false)).map(({ user, device }) => (0, WABinary_1.jidEncode)(user, 's.whatsapp.net', device));
|
|
134
|
+
await assertSessions(devices, true);
|
|
135
|
+
const { nodes: destinations, shouldIncludeDeviceIdentity } = await createParticipantNodes(devices, {
|
|
136
|
+
call: {
|
|
137
|
+
callKey: encKey
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
offerContent.push({ tag: 'destination', attrs: {}, content: destinations });
|
|
141
|
+
if (shouldIncludeDeviceIdentity) {
|
|
142
|
+
offerContent.push({
|
|
143
|
+
tag: 'device-identity',
|
|
144
|
+
attrs: {},
|
|
145
|
+
content: (0, Utils_1.encodeSignedDeviceIdentity)(authState.creds.account, true)
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const stanza = ({
|
|
149
|
+
tag: 'call',
|
|
150
|
+
attrs: {
|
|
151
|
+
to: toJid,
|
|
152
|
+
},
|
|
153
|
+
content: [{
|
|
154
|
+
tag: 'offer',
|
|
155
|
+
attrs: {
|
|
156
|
+
'call-id': callId,
|
|
157
|
+
'call-creator': authState.creds.me.id,
|
|
158
|
+
},
|
|
159
|
+
content: offerContent,
|
|
160
|
+
}],
|
|
161
|
+
});
|
|
162
|
+
await query(stanza);
|
|
163
|
+
return {
|
|
164
|
+
callId,
|
|
165
|
+
toJid,
|
|
166
|
+
isVideo,
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
const rejectCall = async (callId, callFrom) => {
|
|
170
|
+
callFrom = resolveJid(callFrom);
|
|
171
|
+
const stanza = ({
|
|
172
|
+
tag: 'call',
|
|
173
|
+
attrs: {
|
|
174
|
+
from: authState.creds.me.id,
|
|
175
|
+
to: callFrom,
|
|
176
|
+
},
|
|
177
|
+
content: [{
|
|
178
|
+
tag: 'reject',
|
|
179
|
+
attrs: {
|
|
180
|
+
'call-id': callId,
|
|
181
|
+
'call-creator': callFrom,
|
|
182
|
+
count: '0',
|
|
183
|
+
},
|
|
184
|
+
content: undefined,
|
|
185
|
+
}],
|
|
186
|
+
});
|
|
187
|
+
await query(stanza);
|
|
188
|
+
};
|
|
189
|
+
const sendRetryRequest = async (node, forceIncludeKeys = false) => {
|
|
190
|
+
const { fullMessage } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '');
|
|
191
|
+
const { key: msgKey } = fullMessage;
|
|
192
|
+
const msgId = msgKey.id;
|
|
193
|
+
const key = `${msgId}:${msgKey === null || msgKey === void 0 ? void 0 : msgKey.participant}`;
|
|
194
|
+
let retryCount = msgRetryCache.get(key) || 0;
|
|
195
|
+
if (retryCount >= maxMsgRetryCount) {
|
|
196
|
+
logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');
|
|
197
|
+
msgRetryCache.del(key);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
retryCount += 1;
|
|
201
|
+
msgRetryCache.set(key, retryCount);
|
|
202
|
+
const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;
|
|
203
|
+
if (retryCount === 1) {
|
|
204
|
+
const msgId = await requestPlaceholderResend(msgKey);
|
|
205
|
+
logger.debug(`sendRetryRequest: requested placeholder resend for message ${msgId}`);
|
|
206
|
+
}
|
|
207
|
+
const deviceIdentity = (0, Utils_1.encodeSignedDeviceIdentity)(account, true);
|
|
208
|
+
await authState.keys.transaction(async () => {
|
|
209
|
+
const receipt = {
|
|
210
|
+
tag: 'receipt',
|
|
211
|
+
attrs: {
|
|
212
|
+
id: msgId,
|
|
213
|
+
type: 'retry',
|
|
214
|
+
to: node.attrs.from
|
|
215
|
+
},
|
|
216
|
+
content: [
|
|
217
|
+
{
|
|
218
|
+
tag: 'retry',
|
|
219
|
+
attrs: {
|
|
220
|
+
count: retryCount.toString(),
|
|
221
|
+
id: node.attrs.id,
|
|
222
|
+
t: node.attrs.t,
|
|
223
|
+
v: '1'
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
tag: 'registration',
|
|
228
|
+
attrs: {},
|
|
229
|
+
content: (0, Utils_1.encodeBigEndian)(authState.creds.registrationId)
|
|
230
|
+
}
|
|
231
|
+
]
|
|
232
|
+
};
|
|
233
|
+
if (node.attrs.recipient) {
|
|
234
|
+
receipt.attrs.recipient = node.attrs.recipient;
|
|
235
|
+
}
|
|
236
|
+
if (node.attrs.participant) {
|
|
237
|
+
receipt.attrs.participant = node.attrs.participant;
|
|
238
|
+
}
|
|
239
|
+
if (retryCount > 1 || forceIncludeKeys) {
|
|
240
|
+
const { update, preKeys } = await (0, Utils_1.getNextPreKeys)(authState, 1);
|
|
241
|
+
const [keyId] = Object.keys(preKeys);
|
|
242
|
+
const key = preKeys[+keyId];
|
|
243
|
+
const content = receipt.content;
|
|
244
|
+
content.push({
|
|
245
|
+
tag: 'keys',
|
|
246
|
+
attrs: {},
|
|
247
|
+
content: [
|
|
248
|
+
{ tag: 'type', attrs: {}, content: Buffer.from(Defaults_1.KEY_BUNDLE_TYPE) },
|
|
249
|
+
{ tag: 'identity', attrs: {}, content: identityKey.public },
|
|
250
|
+
(0, Utils_1.xmppPreKey)(key, +keyId),
|
|
251
|
+
(0, Utils_1.xmppSignedPreKey)(signedPreKey),
|
|
252
|
+
{ tag: 'device-identity', attrs: {}, content: deviceIdentity }
|
|
253
|
+
]
|
|
254
|
+
});
|
|
255
|
+
ev.emit('creds.update', update);
|
|
256
|
+
}
|
|
257
|
+
await sendNode(receipt);
|
|
258
|
+
logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt');
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
const handleEncryptNotification = async (node) => {
|
|
262
|
+
const from = node.attrs.from;
|
|
263
|
+
if (from === WABinary_1.S_WHATSAPP_NET) {
|
|
264
|
+
const countChild = (0, WABinary_1.getBinaryNodeChild)(node, 'count');
|
|
265
|
+
const count = +countChild.attrs.value;
|
|
266
|
+
const shouldUploadMorePreKeys = count < Defaults_1.MIN_PREKEY_COUNT;
|
|
267
|
+
logger.debug({ count, shouldUploadMorePreKeys }, 'recv pre-key count');
|
|
268
|
+
if (shouldUploadMorePreKeys) {
|
|
269
|
+
await uploadPreKeys();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
const identityNode = (0, WABinary_1.getBinaryNodeChild)(node, 'identity');
|
|
274
|
+
if (identityNode) {
|
|
275
|
+
logger.info({ jid: from }, 'identity changed');
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const toLidIfNecessary = (jid) => {
|
|
281
|
+
if (typeof jid !== 'string')
|
|
282
|
+
return jid;
|
|
283
|
+
if (!jid.includes('@') && /^[0-9]+$/.test(jid)) {
|
|
284
|
+
return `${jid}@s.whatsapp.net`;
|
|
285
|
+
}
|
|
286
|
+
if ((0, WABinary_1.isLid)(jid)) {
|
|
287
|
+
const cached = config.lidCache?.get(jid);
|
|
288
|
+
if (cached && typeof cached === 'string') {
|
|
289
|
+
return cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
290
|
+
}
|
|
291
|
+
return jid;
|
|
292
|
+
}
|
|
293
|
+
return jid;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// Helper for LID resolution in group context by samakavare
|
|
297
|
+
const resolveLidFromGroupContext = async (lid, groupJid) => {
|
|
298
|
+
if (!(0, WABinary_1.isLid)(lid) || !(0, WABinary_1.isJidGroup)(groupJid)) {
|
|
299
|
+
return lid;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
const metadata = await groupMetadata(groupJid);
|
|
304
|
+
const found = metadata.participants.find(p => p.id === lid);
|
|
305
|
+
const jid = found?.jid;
|
|
306
|
+
if (jid) {
|
|
307
|
+
return jid;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
catch (_err) {
|
|
311
|
+
// ignore & fallback
|
|
312
|
+
}
|
|
313
|
+
const cached = config.lidCache?.get(lid);
|
|
314
|
+
if (cached && typeof cached === 'string') {
|
|
315
|
+
return cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
316
|
+
}
|
|
317
|
+
return lid;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const resolveLidOrMaskedJidFromGroupContext = async (jid, groupJid) => {
|
|
321
|
+
if (typeof jid !== 'string') {
|
|
322
|
+
return jid;
|
|
323
|
+
}
|
|
324
|
+
if ((0, WABinary_1.isLid)(jid)) {
|
|
325
|
+
return await resolveLidFromGroupContext(jid, groupJid);
|
|
326
|
+
}
|
|
327
|
+
const decoded = (0, WABinary_1.jidDecode)(jid);
|
|
328
|
+
const user = decoded === null || decoded === void 0 ? void 0 : decoded.user;
|
|
329
|
+
const server = decoded === null || decoded === void 0 ? void 0 : decoded.server;
|
|
330
|
+
if (server === 's.whatsapp.net' && user && /^[0-9]+$/.test(user)) {
|
|
331
|
+
const asLid = `${user}@lid`;
|
|
332
|
+
const resolved = await resolveLidFromGroupContext(asLid, groupJid);
|
|
333
|
+
return resolved === asLid ? jid : resolved;
|
|
334
|
+
}
|
|
335
|
+
return jid;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const pnToJid = (pn) => {
|
|
339
|
+
if (typeof pn !== 'string' || !pn) {
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
return pn.includes('@') ? pn : `${pn}@s.whatsapp.net`;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const collectContextInfos = (obj, acc) => {
|
|
346
|
+
if (!obj || typeof obj !== 'object') {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (obj instanceof Uint8Array || Buffer.isBuffer(obj)) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (Array.isArray(obj)) {
|
|
353
|
+
for (const item of obj) {
|
|
354
|
+
collectContextInfos(item, acc);
|
|
355
|
+
}
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
359
|
+
if (key === 'contextInfo' && value && typeof value === 'object') {
|
|
360
|
+
acc.push(value);
|
|
361
|
+
}
|
|
362
|
+
collectContextInfos(value, acc);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const normalizeContextInfoJids = async (contextInfo, groupJid) => {
|
|
367
|
+
if (!contextInfo || typeof contextInfo !== 'object') {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const normalizeJid = async (jid) => {
|
|
371
|
+
if (typeof jid !== 'string') {
|
|
372
|
+
return jid;
|
|
373
|
+
}
|
|
374
|
+
if ((0, WABinary_1.isLid)(jid)) {
|
|
375
|
+
if (groupJid) {
|
|
376
|
+
return await resolveLidFromGroupContext(jid, groupJid);
|
|
377
|
+
}
|
|
378
|
+
const cached = config.lidCache?.get(jid);
|
|
379
|
+
if (cached && typeof cached === 'string') {
|
|
380
|
+
return cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
381
|
+
}
|
|
382
|
+
return (0, WABinary_1.lidToJid)(jid);
|
|
383
|
+
}
|
|
384
|
+
return jid;
|
|
385
|
+
};
|
|
386
|
+
if (typeof contextInfo.participant === 'string') {
|
|
387
|
+
contextInfo.participant = await normalizeJid(contextInfo.participant);
|
|
388
|
+
}
|
|
389
|
+
if (Array.isArray(contextInfo.mentionedJid)) {
|
|
390
|
+
contextInfo.mentionedJid = await Promise.all(contextInfo.mentionedJid.map(j => normalizeJid(j)));
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
const handleGroupNotification = async (participant, participantPn, child, groupJid, msg) => {
|
|
395
|
+
var _a, _b, _c, _d;
|
|
396
|
+
const childTag = child === null || child === void 0 ? void 0 : child.tag;
|
|
397
|
+
if (participantPn && participant && (0, WABinary_1.isLid)(participant) && config.lidCache?.set) {
|
|
398
|
+
// NOTE: in most if not every w:gp2 stubs participant_pn refer to the actor (admin) not the target
|
|
399
|
+
const pnAsJid = typeof participantPn === 'string' ? (participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`) : participantPn;
|
|
400
|
+
config.lidCache.set(participant, pnAsJid);
|
|
401
|
+
}
|
|
402
|
+
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);
|
|
403
|
+
if (participantPn && participantJid && (0, WABinary_1.isLid)(participantJid) && config.lidCache?.set &&
|
|
404
|
+
(childTag === 'created_membership_requests' || childTag === 'revoked_membership_requests')) {
|
|
405
|
+
// For membership requests, participant_pn refers to the requester (target), not the actor
|
|
406
|
+
const pnAsJid = typeof participantPn === 'string' ? (participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`) : participantPn;
|
|
407
|
+
config.lidCache.set(participantJid, pnAsJid);
|
|
408
|
+
}
|
|
409
|
+
switch (child === null || child === void 0 ? void 0 : child.tag) {
|
|
410
|
+
case 'create':
|
|
411
|
+
let metadata = (0, groups_1.extractGroupMetadata)(child);
|
|
412
|
+
const fullMetadata = await groupMetadata(groupJid);
|
|
413
|
+
if (metadata.owner && metadata.owner.endsWith('@lid')) {
|
|
414
|
+
const found = fullMetadata.participants.find(p => p.id === metadata.owner);
|
|
415
|
+
metadata.owner = found?.jid || (0, WABinary_1.lidToJid)(metadata.owner);
|
|
416
|
+
}
|
|
417
|
+
let resolvedAuthor = participant;
|
|
418
|
+
if (participant.endsWith('@lid')) {
|
|
419
|
+
const found = fullMetadata.participants.find(p => p.id === participant);
|
|
420
|
+
resolvedAuthor = found?.jid || (0, WABinary_1.lidToJid)(participant);
|
|
421
|
+
}
|
|
422
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CREATE;
|
|
423
|
+
msg.messageStubParameters = [metadata.subject];
|
|
424
|
+
msg.key = { participant: metadata.owner };
|
|
425
|
+
ev.emit('chats.upsert', [{
|
|
426
|
+
id: metadata.id,
|
|
427
|
+
name: metadata.subject,
|
|
428
|
+
conversationTimestamp: metadata.creation,
|
|
429
|
+
}]);
|
|
430
|
+
ev.emit('groups.upsert', [{
|
|
431
|
+
...metadata,
|
|
432
|
+
author: resolvedAuthor
|
|
433
|
+
}]);
|
|
434
|
+
break;
|
|
435
|
+
case 'ephemeral':
|
|
436
|
+
case 'not_ephemeral':
|
|
437
|
+
msg.message = {
|
|
438
|
+
protocolMessage: {
|
|
439
|
+
type: WAProto_1.proto.Message.ProtocolMessage.Type.EPHEMERAL_SETTING,
|
|
440
|
+
ephemeralExpiration: +(child.attrs.expiration || 0)
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
break;
|
|
444
|
+
case 'modify':
|
|
445
|
+
const modifyNodes = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant');
|
|
446
|
+
const oldNumber = modifyNodes.map(p => {
|
|
447
|
+
const phoneNumber = p.attrs.phone_number;
|
|
448
|
+
const pn = p.attrs.participant_pn;
|
|
449
|
+
if (phoneNumber) {
|
|
450
|
+
return typeof phoneNumber === 'string' ? (phoneNumber.includes('@') ? phoneNumber : `${phoneNumber}@s.whatsapp.net`) : phoneNumber;
|
|
451
|
+
}
|
|
452
|
+
if (pn) {
|
|
453
|
+
return typeof pn === 'string' ? (pn.includes('@') ? pn : `${pn}@s.whatsapp.net`) : pn;
|
|
454
|
+
}
|
|
455
|
+
return p.attrs.jid;
|
|
456
|
+
});
|
|
457
|
+
msg.messageStubParameters = oldNumber || [];
|
|
458
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
|
|
459
|
+
break;
|
|
460
|
+
case 'promote':
|
|
461
|
+
case 'demote':
|
|
462
|
+
case 'remove':
|
|
463
|
+
case 'add':
|
|
464
|
+
case 'leave':
|
|
465
|
+
const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
|
|
466
|
+
msg.messageStubType = Types_1.WAMessageStubType[stubType];
|
|
467
|
+
const participantNodes = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant');
|
|
468
|
+
const participants = await Promise.all(participantNodes.map(async (p) => {
|
|
469
|
+
const jid = p.attrs.jid;
|
|
470
|
+
const pn = p.attrs.participant_pn;
|
|
471
|
+
const phoneNumber = p.attrs.phone_number;
|
|
472
|
+
// Cache LID to JID mapping if phone_number or participant_pn is available
|
|
473
|
+
const realPhone = phoneNumber || pn;
|
|
474
|
+
if (realPhone && jid && (0, WABinary_1.isLid)(jid) && config.lidCache?.set) {
|
|
475
|
+
const pnAsJid = typeof realPhone === 'string' ? (realPhone.includes('@') ? realPhone : `${realPhone}@s.whatsapp.net`) : realPhone;
|
|
476
|
+
config.lidCache.set(jid, pnAsJid);
|
|
477
|
+
}
|
|
478
|
+
// For ALL participant events, prefer phone_number or participant_pn over jid why i didn't think of it b4 🤕
|
|
479
|
+
if (phoneNumber) {
|
|
480
|
+
return typeof phoneNumber === 'string' ? (phoneNumber.includes('@') ? phoneNumber : `${phoneNumber}@s.whatsapp.net`) : phoneNumber;
|
|
481
|
+
}
|
|
482
|
+
if (pn) {
|
|
483
|
+
return typeof pn === 'string' ? (pn.includes('@') ? pn : `${pn}@s.whatsapp.net`) : pn;
|
|
484
|
+
}
|
|
485
|
+
if ((0, WABinary_1.isLid)(jid) && config.lidCache?.get) {
|
|
486
|
+
const cached = config.lidCache.get(jid);
|
|
487
|
+
if (cached && typeof cached === 'string') {
|
|
488
|
+
return cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
if (jid && typeof jid === 'string' && jid.endsWith('@s.whatsapp.net')) {
|
|
492
|
+
const user = jid.replace('@s.whatsapp.net', '');
|
|
493
|
+
if (/^[0-9]+$/.test(user) && user.length > 12) {
|
|
494
|
+
const resolved = await resolveLidOrMaskedJidFromGroupContext(jid, groupJid);
|
|
495
|
+
if (resolved !== jid) {
|
|
496
|
+
return resolved;
|
|
497
|
+
}
|
|
498
|
+
// If resolution failed, validate the JID - if invalid, convert back to LID
|
|
499
|
+
const validated = (0, WABinary_1.validateAndCleanJid)(jid);
|
|
500
|
+
return validated;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
// Final validation for any JID before returning
|
|
504
|
+
if (jid && typeof jid === 'string') {
|
|
505
|
+
return (0, WABinary_1.validateAndCleanJid)(jid);
|
|
506
|
+
}
|
|
507
|
+
return jid;
|
|
508
|
+
}));
|
|
509
|
+
if (participants.length === 1 &&
|
|
510
|
+
(0, WABinary_1.areJidsSameUser)(participants[0], participant) &&
|
|
511
|
+
child.tag === 'remove') {
|
|
512
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;
|
|
513
|
+
}
|
|
514
|
+
if ((child.tag === 'leave' || msg.messageStubType === Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE)
|
|
515
|
+
&& participants.length === 1
|
|
516
|
+
&& participantPn
|
|
517
|
+
&& typeof participantPn === 'string') {
|
|
518
|
+
msg.messageStubParameters = [toLidIfNecessary(participantPn)];
|
|
519
|
+
if (participant && (0, WABinary_1.isLid)(participant)) {
|
|
520
|
+
participant = toLidIfNecessary(participantPn);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
else {
|
|
524
|
+
msg.messageStubParameters = participants;
|
|
525
|
+
}
|
|
526
|
+
break;
|
|
527
|
+
case 'subject':
|
|
528
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;
|
|
529
|
+
msg.messageStubParameters = [child.attrs.subject];
|
|
530
|
+
break;
|
|
531
|
+
case 'description':
|
|
532
|
+
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();
|
|
533
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_DESCRIPTION;
|
|
534
|
+
msg.messageStubParameters = description ? [description] : undefined;
|
|
535
|
+
break;
|
|
536
|
+
case 'announcement':
|
|
537
|
+
case 'not_announcement':
|
|
538
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;
|
|
539
|
+
msg.messageStubParameters = [(child.tag === 'announcement') ? 'on' : 'off'];
|
|
540
|
+
break;
|
|
541
|
+
case 'locked':
|
|
542
|
+
case 'unlocked':
|
|
543
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT;
|
|
544
|
+
msg.messageStubParameters = [(child.tag === 'locked') ? 'on' : 'off'];
|
|
545
|
+
break;
|
|
546
|
+
case 'invite':
|
|
547
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_INVITE_LINK;
|
|
548
|
+
msg.messageStubParameters = [child.attrs.code];
|
|
549
|
+
break;
|
|
550
|
+
case 'member_add_mode':
|
|
551
|
+
const addMode = child.content;
|
|
552
|
+
if (addMode) {
|
|
553
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBER_ADD_MODE;
|
|
554
|
+
msg.messageStubParameters = [addMode.toString()];
|
|
555
|
+
}
|
|
556
|
+
break;
|
|
557
|
+
case 'membership_approval_mode':
|
|
558
|
+
const approvalMode = (0, WABinary_1.getBinaryNodeChild)(child, 'group_join');
|
|
559
|
+
if (approvalMode) {
|
|
560
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE;
|
|
561
|
+
msg.messageStubParameters = [approvalMode.attrs.state];
|
|
562
|
+
}
|
|
563
|
+
break;
|
|
564
|
+
case 'created_membership_requests':
|
|
565
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
|
|
566
|
+
// Resolve participantJid to phone number if it's a LID
|
|
567
|
+
let resolvedParticipantJid = participantJid;
|
|
568
|
+
if (participantPn && typeof participantPn === 'string') {
|
|
569
|
+
resolvedParticipantJid = participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`;
|
|
570
|
+
} else if ((0, WABinary_1.isLid)(participantJid) && config.lidCache?.get) {
|
|
571
|
+
const cached = config.lidCache.get(participantJid);
|
|
572
|
+
if (cached && typeof cached === 'string') {
|
|
573
|
+
resolvedParticipantJid = cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
msg.messageStubParameters = [resolvedParticipantJid, 'created', child.attrs.request_method];
|
|
577
|
+
break;
|
|
578
|
+
case 'revoked_membership_requests':
|
|
579
|
+
const isDenied = (0, WABinary_1.areJidsSameUser)(participantJid, participant);
|
|
580
|
+
msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
|
|
581
|
+
// Resolve participantJid to phone number if it's a LID
|
|
582
|
+
let resolvedRevokedJid = participantJid;
|
|
583
|
+
if (participantPn && typeof participantPn === 'string') {
|
|
584
|
+
resolvedRevokedJid = participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`;
|
|
585
|
+
} else if ((0, WABinary_1.isLid)(participantJid) && config.lidCache?.get) {
|
|
586
|
+
const cached = config.lidCache.get(participantJid);
|
|
587
|
+
if (cached && typeof cached === 'string') {
|
|
588
|
+
resolvedRevokedJid = cached.includes('@') ? cached : `${cached}@s.whatsapp.net`;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
msg.messageStubParameters = [resolvedRevokedJid, isDenied ? 'revoked' : 'rejected'];
|
|
592
|
+
break;
|
|
593
|
+
default:
|
|
594
|
+
// console.log("BAILEYS-DEBUG:", JSON.stringify({ ...child, content: Buffer.isBuffer(child.content) ? child.content.toString() : child.content, participant }, null, 2))
|
|
595
|
+
}
|
|
596
|
+
const isAddEvent = msg.messageStubType === Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD ||
|
|
597
|
+
msg.messageStubType === Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE ||
|
|
598
|
+
msg.messageStubType === Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN;
|
|
599
|
+
|
|
600
|
+
if (msg.messageStubParameters && !isAddEvent) {
|
|
601
|
+
msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' ? await resolveLidOrMaskedJidFromGroupContext(param, groupJid) : param)));
|
|
602
|
+
}
|
|
603
|
+
participant = toLidIfNecessary(participant);
|
|
604
|
+
if (msg.key?.participant) {
|
|
605
|
+
msg.key.participant = toLidIfNecessary(msg.key.participant);
|
|
606
|
+
}
|
|
607
|
+
const needsResolving = !isAddEvent && ((msg.messageStubParameters && msg.messageStubParameters.some(p => (typeof p === 'string' && ((0, WABinary_1.isLid)(p) || (p.endsWith('@s.whatsapp.net') && /^[0-9]+$/.test(p.split('@')[0])))))) ||
|
|
608
|
+
(participant && ((0, WABinary_1.isLid)(participant) || (typeof participant === 'string' && participant.endsWith('@s.whatsapp.net') && /^[0-9]+$/.test(participant.split('@')[0])))) ||
|
|
609
|
+
(msg.key?.participant && ((0, WABinary_1.isLid)(msg.key.participant) || (typeof msg.key.participant === 'string' && msg.key.participant.endsWith('@s.whatsapp.net') && /^[0-9]+$/.test(msg.key.participant.split('@')[0])))));
|
|
610
|
+
if (needsResolving) {
|
|
611
|
+
if (msg.messageStubParameters) {
|
|
612
|
+
msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' ? await resolveLidOrMaskedJidFromGroupContext(param, groupJid) : param)));
|
|
613
|
+
}
|
|
614
|
+
if (typeof participant === 'string' && ((0, WABinary_1.isLid)(participant) || (participant.endsWith('@s.whatsapp.net') && /^[0-9]+$/.test(participant.split('@')[0])))) {
|
|
615
|
+
msg.participant = await resolveLidOrMaskedJidFromGroupContext(participant, groupJid);
|
|
616
|
+
}
|
|
617
|
+
else if (participant) {
|
|
618
|
+
//If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah
|
|
619
|
+
msg.participant = participant;
|
|
620
|
+
}
|
|
621
|
+
if (msg.key && typeof msg.key.participant === 'string' && ((0, WABinary_1.isLid)(msg.key.participant) || (msg.key.participant.endsWith('@s.whatsapp.net') && /^[0-9]+$/.test(msg.key.participant.split('@')[0])))) {
|
|
622
|
+
msg.key.participant = await resolveLidOrMaskedJidFromGroupContext(msg.key.participant, groupJid);
|
|
623
|
+
}
|
|
624
|
+
else if (msg.key && msg.key.participant) {
|
|
625
|
+
// If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah pt2
|
|
626
|
+
msg.key.participant = msg.key.participant;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const handleNewsletterNotification = (id, node) => {
|
|
632
|
+
const messages = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
|
|
633
|
+
const message = (0, WABinary_1.getBinaryNodeChild)(messages, 'message');
|
|
634
|
+
const serverId = message.attrs.server_id;
|
|
635
|
+
const reactionsList = (0, WABinary_1.getBinaryNodeChild)(message, 'reactions');
|
|
636
|
+
const viewsList = (0, WABinary_1.getBinaryNodeChildren)(message, 'views_count');
|
|
637
|
+
if (reactionsList) {
|
|
638
|
+
const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionsList, 'reaction');
|
|
639
|
+
if (reactions.length === 0) {
|
|
640
|
+
ev.emit('newsletter.reaction', { id, 'server_id': serverId, reaction: { removed: true } });
|
|
641
|
+
}
|
|
642
|
+
reactions.forEach(item => {
|
|
643
|
+
var _a, _b;
|
|
644
|
+
ev.emit('newsletter.reaction', { id, 'server_id': serverId, 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) } });
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
if (viewsList.length) {
|
|
648
|
+
viewsList.forEach(item => {
|
|
649
|
+
ev.emit('newsletter.view', { id, 'server_id': serverId, count: +item.attrs.count });
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
const handleMexNewsletterNotification = (id, node) => {
|
|
655
|
+
var _a;
|
|
656
|
+
const operation = node === null || node === void 0 ? void 0 : node.attrs.op_name;
|
|
657
|
+
const content = JSON.parse((_a = node === null || node === void 0 ? void 0 : node.content) === null || _a === void 0 ? void 0 : _a.toString());
|
|
658
|
+
let contentPath;
|
|
659
|
+
if (operation === Types_1.MexOperations.PROMOTE || operation === Types_1.MexOperations.DEMOTE) {
|
|
660
|
+
let action;
|
|
661
|
+
if (operation === Types_1.MexOperations.PROMOTE) {
|
|
662
|
+
action = 'promote';
|
|
663
|
+
contentPath = content.data[Types_1.XWAPaths.PROMOTE];
|
|
664
|
+
}
|
|
665
|
+
if (operation === Types_1.MexOperations.DEMOTE) {
|
|
666
|
+
action = 'demote';
|
|
667
|
+
contentPath = content.data[Types_1.XWAPaths.DEMOTE];
|
|
668
|
+
}
|
|
669
|
+
const author = resolveJid(contentPath.actor.pn);
|
|
670
|
+
const user = resolveJid(contentPath.user.pn);
|
|
671
|
+
ev.emit('newsletter-participants.update', { id, author, user, new_role: contentPath.user_new_role, action });
|
|
672
|
+
}
|
|
673
|
+
if (operation === Types_1.MexOperations.UPDATE) {
|
|
674
|
+
contentPath = content.data[Types_1.XWAPaths.METADATA_UPDATE];
|
|
675
|
+
ev.emit('newsletter-settings.update', { id, update: contentPath.thread_metadata.settings });
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
const processNotification = async (node) => {
|
|
680
|
+
var _a;
|
|
681
|
+
const result = {};
|
|
682
|
+
const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
|
683
|
+
const nodeType = node.attrs.type;
|
|
684
|
+
const from = resolveJid((0, WABinary_1.jidNormalizedUser)(node.attrs.from));
|
|
685
|
+
switch (nodeType) {
|
|
686
|
+
case 'privacy_token':
|
|
687
|
+
const tokenList = (0, WABinary_1.getBinaryNodeChildren)(child, 'token');
|
|
688
|
+
for (const { attrs, content } of tokenList) {
|
|
689
|
+
const jid = resolveJid(attrs.jid);
|
|
690
|
+
ev.emit('chats.update', [
|
|
691
|
+
{
|
|
692
|
+
id: jid,
|
|
693
|
+
tcToken: content
|
|
694
|
+
}
|
|
695
|
+
]);
|
|
696
|
+
logger.debug({ jid }, 'got privacy token update');
|
|
697
|
+
}
|
|
698
|
+
break;
|
|
699
|
+
case 'newsletter':
|
|
700
|
+
handleNewsletterNotification(node.attrs.from, child);
|
|
701
|
+
break;
|
|
702
|
+
case 'mex':
|
|
703
|
+
handleMexNewsletterNotification(node.attrs.from, child);
|
|
704
|
+
break;
|
|
705
|
+
case 'w:gp2':
|
|
706
|
+
await handleGroupNotification(node.attrs.participant, node.attrs.participant_pn, child, from, result);
|
|
707
|
+
break;
|
|
708
|
+
case 'mediaretry':
|
|
709
|
+
const event = (0, Utils_1.decodeMediaRetryNode)(node);
|
|
710
|
+
ev.emit('messages.media-update', [event]);
|
|
711
|
+
break;
|
|
712
|
+
case 'encrypt':
|
|
713
|
+
await handleEncryptNotification(node);
|
|
714
|
+
break;
|
|
715
|
+
case 'devices':
|
|
716
|
+
const devices = (0, WABinary_1.getBinaryNodeChildren)(child, 'device');
|
|
717
|
+
if ((0, WABinary_1.areJidsSameUser)(child.attrs.jid, authState.creds.me.id)) {
|
|
718
|
+
const deviceJids = devices.map(d => resolveJid(d.attrs.jid));
|
|
719
|
+
logger.info({ deviceJids }, 'got my own devices');
|
|
720
|
+
}
|
|
721
|
+
break;
|
|
722
|
+
case 'server_sync':
|
|
723
|
+
const update = (0, WABinary_1.getBinaryNodeChild)(node, 'collection');
|
|
724
|
+
if (update) {
|
|
725
|
+
const name = update.attrs.name;
|
|
726
|
+
await resyncAppState([name], false);
|
|
727
|
+
}
|
|
728
|
+
break;
|
|
729
|
+
case 'picture':
|
|
730
|
+
const setPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'set');
|
|
731
|
+
const delPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'delete');
|
|
732
|
+
ev.emit('contacts.update', [{
|
|
733
|
+
id: resolveJid(from) || ((_b = (_a = (setPicture || delPicture)) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.hash) || '',
|
|
734
|
+
imgUrl: setPicture ? 'changed' : 'removed'
|
|
735
|
+
}]);
|
|
736
|
+
if ((0, WABinary_1.isJidGroup)(from)) {
|
|
737
|
+
const node = setPicture || delPicture;
|
|
738
|
+
result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ICON;
|
|
739
|
+
if (setPicture) {
|
|
740
|
+
result.messageStubParameters = [setPicture.attrs.id];
|
|
741
|
+
}
|
|
742
|
+
result.participant = node === null || node === void 0 ? void 0 : node.attrs.author;
|
|
743
|
+
result.key = {
|
|
744
|
+
...result.key || {},
|
|
745
|
+
participant: setPicture === null || setPicture === void 0 ? void 0 : setPicture.attrs.author
|
|
746
|
+
};
|
|
747
|
+
if (result.participant && (0, WABinary_1.isLid)(result.participant)) {
|
|
748
|
+
result.participant = await resolveLidFromGroupContext(result.participant, from);
|
|
749
|
+
}
|
|
750
|
+
if (result.key?.participant && (0, WABinary_1.isLid)(result.key.participant)) {
|
|
751
|
+
result.key.participant = await resolveLidFromGroupContext(result.key.participant, from);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
break;
|
|
755
|
+
case 'account_sync':
|
|
756
|
+
if (child.tag === 'disappearing_mode') {
|
|
757
|
+
const newDuration = +child.attrs.duration;
|
|
758
|
+
const timestamp = +child.attrs.t;
|
|
759
|
+
logger.info({ newDuration }, 'updated account disappearing mode');
|
|
760
|
+
ev.emit('creds.update', {
|
|
761
|
+
accountSettings: {
|
|
762
|
+
...authState.creds.accountSettings,
|
|
763
|
+
defaultDisappearingMode: {
|
|
764
|
+
ephemeralExpiration: newDuration,
|
|
765
|
+
ephemeralSettingTimestamp: timestamp,
|
|
766
|
+
},
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
else if (child.tag === 'blocklist') {
|
|
771
|
+
const blocklists = (0, WABinary_1.getBinaryNodeChildren)(child, 'item');
|
|
772
|
+
for (const { attrs } of blocklists) {
|
|
773
|
+
const blocklist = [resolveJid(attrs.jid)];
|
|
774
|
+
const type = (attrs.action === 'block') ? 'add' : 'remove';
|
|
775
|
+
ev.emit('blocklist.update', { blocklist, type });
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
break;
|
|
779
|
+
case 'link_code_companion_reg':
|
|
780
|
+
const linkCodeCompanionReg = (0, WABinary_1.getBinaryNodeChild)(node, 'link_code_companion_reg');
|
|
781
|
+
const ref = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_ref'));
|
|
782
|
+
const primaryIdentityPublicKey = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'primary_identity_pub'));
|
|
783
|
+
const primaryEphemeralPublicKeyWrapped = toRequiredBuffer((0, WABinary_1.getBinaryNodeChildBuffer)(linkCodeCompanionReg, 'link_code_pairing_wrapped_primary_ephemeral_pub'));
|
|
784
|
+
const codePairingPublicKey = await decipherLinkPublicKey(primaryEphemeralPublicKeyWrapped);
|
|
785
|
+
const companionSharedKey = Utils_1.Curve.sharedKey(authState.creds.pairingEphemeralKeyPair.private, codePairingPublicKey);
|
|
786
|
+
const random = (0, crypto_1.randomBytes)(32);
|
|
787
|
+
const linkCodeSalt = (0, crypto_1.randomBytes)(32);
|
|
788
|
+
const linkCodePairingExpanded = await (0, Utils_1.hkdf)(companionSharedKey, 32, {
|
|
789
|
+
salt: linkCodeSalt,
|
|
790
|
+
info: 'link_code_pairing_key_bundle_encryption_key'
|
|
791
|
+
});
|
|
792
|
+
const encryptPayload = Buffer.concat([Buffer.from(authState.creds.signedIdentityKey.public), primaryIdentityPublicKey, random]);
|
|
793
|
+
const encryptIv = (0, crypto_1.randomBytes)(12);
|
|
794
|
+
const encrypted = (0, Utils_1.aesEncryptGCM)(encryptPayload, linkCodePairingExpanded, encryptIv, Buffer.alloc(0));
|
|
795
|
+
const encryptedPayload = Buffer.concat([linkCodeSalt, encryptIv, encrypted]);
|
|
796
|
+
const identitySharedKey = Utils_1.Curve.sharedKey(authState.creds.signedIdentityKey.private, primaryIdentityPublicKey);
|
|
797
|
+
const identityPayload = Buffer.concat([companionSharedKey, identitySharedKey, random]);
|
|
798
|
+
authState.creds.advSecretKey = (await (0, Utils_1.hkdf)(identityPayload, 32, { info: 'adv_secret' })).toString('base64');
|
|
799
|
+
await query({
|
|
800
|
+
tag: 'iq',
|
|
801
|
+
attrs: {
|
|
802
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
803
|
+
type: 'set',
|
|
804
|
+
id: sock.generateMessageTag(),
|
|
805
|
+
xmlns: 'md'
|
|
806
|
+
},
|
|
807
|
+
content: [
|
|
808
|
+
{
|
|
809
|
+
tag: 'link_code_companion_reg',
|
|
810
|
+
attrs: {
|
|
811
|
+
jid: authState.creds.me.id,
|
|
812
|
+
stage: 'companion_finish',
|
|
813
|
+
},
|
|
814
|
+
content: [
|
|
815
|
+
{
|
|
816
|
+
tag: 'link_code_pairing_wrapped_key_bundle',
|
|
817
|
+
attrs: {},
|
|
818
|
+
content: encryptedPayload
|
|
819
|
+
},
|
|
820
|
+
{
|
|
821
|
+
tag: 'companion_identity_public',
|
|
822
|
+
attrs: {},
|
|
823
|
+
content: authState.creds.signedIdentityKey.public
|
|
824
|
+
},
|
|
825
|
+
{
|
|
826
|
+
tag: 'link_code_pairing_ref',
|
|
827
|
+
attrs: {},
|
|
828
|
+
content: ref
|
|
829
|
+
}
|
|
830
|
+
]
|
|
831
|
+
}
|
|
832
|
+
]
|
|
833
|
+
});
|
|
834
|
+
authState.creds.registered = true;
|
|
835
|
+
ev.emit('creds.update', authState.creds);
|
|
836
|
+
}
|
|
837
|
+
if (Object.keys(result).length) {
|
|
838
|
+
return result;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
async function decipherLinkPublicKey(data) {
|
|
843
|
+
const buffer = toRequiredBuffer(data);
|
|
844
|
+
const salt = buffer.slice(0, 32);
|
|
845
|
+
const secretKey = await (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
|
|
846
|
+
const iv = buffer.slice(32, 48);
|
|
847
|
+
const payload = buffer.slice(48, 80);
|
|
848
|
+
return (0, Utils_1.aesDecryptCTR)(payload, secretKey, iv);
|
|
849
|
+
}
|
|
850
|
+
function toRequiredBuffer(data) {
|
|
851
|
+
if (data === undefined) {
|
|
852
|
+
throw new boom_1.Boom('Invalid buffer', { statusCode: 400 });
|
|
853
|
+
}
|
|
854
|
+
return data instanceof Buffer ? data : Buffer.from(data);
|
|
855
|
+
}
|
|
856
|
+
const willSendMessageAgain = (id, participant) => {
|
|
857
|
+
const key = `${id}:${participant}`;
|
|
858
|
+
const retryCount = msgRetryCache.get(key) || 0;
|
|
859
|
+
return retryCount < maxMsgRetryCount;
|
|
860
|
+
};
|
|
861
|
+
const updateSendMessageAgainCount = (id, participant) => {
|
|
862
|
+
const key = `${id}:${participant}`;
|
|
863
|
+
const newValue = (msgRetryCache.get(key) || 0) + 1;
|
|
864
|
+
msgRetryCache.set(key, newValue);
|
|
865
|
+
};
|
|
866
|
+
const sendMessagesAgain = async (key, ids, retryNode) => {
|
|
867
|
+
var _a;
|
|
868
|
+
// implement a cache to store the last 256 sent messages (copy whatsmeow) | (maybe should lower it)
|
|
869
|
+
const msgs = await Promise.all(ids.map(async (id) => {
|
|
870
|
+
const msg = await getMessage({ ...key, id });
|
|
871
|
+
return msg || getCachedSentMessage(key.remoteJid, id);
|
|
872
|
+
}));
|
|
873
|
+
const remoteJid = key.remoteJid;
|
|
874
|
+
const participant = key.participant || remoteJid;
|
|
875
|
+
const sendToAll = !((_a = (0, WABinary_1.jidDecode)(participant)) === null || _a === void 0 ? void 0 : _a.device);
|
|
876
|
+
await assertSessions([participant], true);
|
|
877
|
+
if ((0, WABinary_1.isJidGroup)(remoteJid)) {
|
|
878
|
+
await authState.keys.set({ 'sender-key-memory': { [remoteJid]: null } });
|
|
879
|
+
}
|
|
880
|
+
logger.debug({ participant, sendToAll }, 'forced new session for retry recp');
|
|
881
|
+
for (const [i, msg] of msgs.entries()) {
|
|
882
|
+
if (msg) {
|
|
883
|
+
updateSendMessageAgainCount(ids[i], participant);
|
|
884
|
+
const msgRelayOpts = { messageId: ids[i] };
|
|
885
|
+
if (sendToAll) {
|
|
886
|
+
msgRelayOpts.useUserDevicesCache = false;
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
msgRelayOpts.participant = {
|
|
890
|
+
jid: participant,
|
|
891
|
+
count: +retryNode.attrs.count
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
await relayMessage(key.remoteJid, msg, msgRelayOpts);
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
logger.debug({ jid: key.remoteJid, id: ids[i] }, 'recv retry request, but message not available');
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
const handleReceipt = async (node) => {
|
|
902
|
+
var _a, _b;
|
|
903
|
+
const { attrs, content } = node;
|
|
904
|
+
let participant = attrs.participant;
|
|
905
|
+
if (participant && (0, WABinary_1.isLid)(participant) && (0, WABinary_1.isJidGroup)(attrs.from)) {
|
|
906
|
+
const cached = config.lidCache.get(participant);
|
|
907
|
+
if (cached) {
|
|
908
|
+
participant = typeof cached === 'string' && !cached.includes('@') ? `${cached}@s.whatsapp.net` : cached;
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
try {
|
|
912
|
+
const metadata = await groupMetadata(attrs.from);
|
|
913
|
+
const found = metadata.participants.find(p => p.id === participant);
|
|
914
|
+
const jid = found === null || found === void 0 ? void 0 : found.jid;
|
|
915
|
+
if (jid && !(0, WABinary_1.isLid)(jid)) {
|
|
916
|
+
participant = jid;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
catch (_e) {
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
const isLid = attrs.from.includes('lid');
|
|
924
|
+
const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(resolveJid(participant) || resolveJid(attrs.from), isLid ? (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid : (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.id);
|
|
925
|
+
const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? resolveJid(attrs.from) : attrs.recipient;
|
|
926
|
+
const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe);
|
|
927
|
+
const key = {
|
|
928
|
+
remoteJid,
|
|
929
|
+
id: '',
|
|
930
|
+
fromMe,
|
|
931
|
+
participant: resolveJid(participant)
|
|
932
|
+
};
|
|
933
|
+
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
|
934
|
+
logger.debug({ remoteJid }, 'ignoring receipt from jid');
|
|
935
|
+
await sendMessageAck(node);
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
const ids = [attrs.id];
|
|
939
|
+
if (Array.isArray(content)) {
|
|
940
|
+
const items = (0, WABinary_1.getBinaryNodeChildren)(content[0], 'item');
|
|
941
|
+
ids.push(...items.map(i => i.attrs.id));
|
|
942
|
+
}
|
|
943
|
+
try {
|
|
944
|
+
await Promise.all([
|
|
945
|
+
processingMutex.mutex(async () => {
|
|
946
|
+
const status = (0, Utils_1.getStatusFromReceiptType)(attrs.type);
|
|
947
|
+
if (typeof status !== 'undefined' &&
|
|
948
|
+
(
|
|
949
|
+
status >= WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK ||
|
|
950
|
+
!isNodeFromMe)) {
|
|
951
|
+
if ((0, WABinary_1.isJidGroup)(remoteJid) || (0, WABinary_1.isJidStatusBroadcast)(remoteJid)) {
|
|
952
|
+
if (attrs.participant) {
|
|
953
|
+
const updateKey = status === WAProto_1.proto.WebMessageInfo.Status.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';
|
|
954
|
+
ev.emit('message-receipt.update', ids.map(id => ({
|
|
955
|
+
key: { ...key, id },
|
|
956
|
+
receipt: {
|
|
957
|
+
userJid: (0, WABinary_1.jidNormalizedUser)(resolveJid(attrs.participant)),
|
|
958
|
+
[updateKey]: +attrs.t
|
|
959
|
+
}
|
|
960
|
+
})));
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
else {
|
|
964
|
+
ids.forEach(id => {
|
|
965
|
+
const statusName = Object.keys(WAProto_1.proto.WebMessageInfo.Status)[status] || `UNKNOWN_${status}`;
|
|
966
|
+
logger.debug({ remoteJid, id, status: statusName }, 'ACK status update');
|
|
967
|
+
});
|
|
968
|
+
ev.emit('messages.update', ids.map(id => ({
|
|
969
|
+
key: { ...key, id },
|
|
970
|
+
update: { status }
|
|
971
|
+
})));
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
if (status === WAProto_1.proto.WebMessageInfo.Status.ERROR) {
|
|
975
|
+
ev.emit('messages.update', ids.map(id => ({
|
|
976
|
+
key: { ...key, id },
|
|
977
|
+
update: { status: WAProto_1.proto.WebMessageInfo.Status.SERVER_ACK }
|
|
978
|
+
})));
|
|
979
|
+
}
|
|
980
|
+
if (attrs.type === 'retry') {
|
|
981
|
+
key.participant = key.participant || attrs.from;
|
|
982
|
+
const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
|
|
983
|
+
if (willSendMessageAgain(ids[0], key.participant)) {
|
|
984
|
+
if (key.fromMe) {
|
|
985
|
+
try {
|
|
986
|
+
logger.debug({ attrs, key }, 'recv retry request');
|
|
987
|
+
await sendMessagesAgain(key, ids, retryNode);
|
|
988
|
+
}
|
|
989
|
+
catch (error) {
|
|
990
|
+
logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
else {
|
|
994
|
+
logger.info({ attrs, key }, 'recv retry for not fromMe message');
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
else {
|
|
998
|
+
logger.info({ attrs, key }, 'will not send message again, as sent too many times');
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
})
|
|
1002
|
+
]);
|
|
1003
|
+
}
|
|
1004
|
+
finally {
|
|
1005
|
+
await sendMessageAck(node);
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
const handleNotification = async (node) => {
|
|
1009
|
+
const remoteJid = resolveJid(node.attrs.from);
|
|
1010
|
+
if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
|
|
1011
|
+
logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification');
|
|
1012
|
+
await sendMessageAck(node);
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
const notifDedupKey = `${remoteJid}:${node.attrs.id}`;
|
|
1016
|
+
if (notificationDedupCache.get(notifDedupKey)) {
|
|
1017
|
+
await sendMessageAck(node);
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
notificationDedupCache.set(notifDedupKey, true);
|
|
1021
|
+
try {
|
|
1022
|
+
await Promise.all([
|
|
1023
|
+
processingMutex.mutex(async () => {
|
|
1024
|
+
var _a;
|
|
1025
|
+
const msg = await processNotification(node);
|
|
1026
|
+
if (msg) {
|
|
1027
|
+
const stubType = msg.messageStubType;
|
|
1028
|
+
const stubParams = Array.isArray(msg.messageStubParameters) ? msg.messageStubParameters : [];
|
|
1029
|
+
const stubDedupKey = `${remoteJid}:${stubType}:${stubParams.join(',')}`;
|
|
1030
|
+
if (stubType && notificationStubDedupCache.get(stubDedupKey)) {
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
if (stubType) {
|
|
1034
|
+
notificationStubDedupCache.set(stubDedupKey, true);
|
|
1035
|
+
}
|
|
1036
|
+
const participant = msg.participant || resolveJid(node.attrs.participant);
|
|
1037
|
+
const fromMe = (0, WABinary_1.areJidsSameUser)(participant || remoteJid, authState.creds.me.id);
|
|
1038
|
+
const key = msg.key || {};
|
|
1039
|
+
key.remoteJid = remoteJid;
|
|
1040
|
+
key.fromMe = fromMe;
|
|
1041
|
+
key.id = node.attrs.id;
|
|
1042
|
+
key.participant = key.participant || participant;
|
|
1043
|
+
msg.key = key;
|
|
1044
|
+
msg.participant = participant;
|
|
1045
|
+
msg.messageTimestamp = +node.attrs.t;
|
|
1046
|
+
const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
|
|
1047
|
+
await upsertMessage(fullMsg, 'append');
|
|
1048
|
+
}
|
|
1049
|
+
})
|
|
1050
|
+
]);
|
|
1051
|
+
}
|
|
1052
|
+
finally {
|
|
1053
|
+
await sendMessageAck(node);
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
const handleMessage = withAck(async (node) => {
|
|
1057
|
+
var _a, _b, _c;
|
|
1058
|
+
if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
|
|
1059
|
+
logger.debug({ key: node.attrs.key }, 'ignored message');
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
|
|
1063
|
+
// temporary fix for crashes and issues resulting of failed msmsg decryption
|
|
1064
|
+
if (encNode && encNode.attrs.type === 'msmsg') {
|
|
1065
|
+
logger.debug({ key: node.attrs.key }, 'recv msmsg, requesting retry');
|
|
1066
|
+
retryMutex.mutex(async () => {
|
|
1067
|
+
if (ws.isOpen) {
|
|
1068
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
await sendRetryRequest(node, false);
|
|
1072
|
+
if (retryRequestDelayMs) {
|
|
1073
|
+
await (0, Utils_1.delay)(retryRequestDelayMs);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
else {
|
|
1077
|
+
logger.debug({ node }, 'connection closed, ignoring retry req');
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
let response;
|
|
1083
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable') && !encNode) {
|
|
1084
|
+
const { key } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '').fullMessage;
|
|
1085
|
+
response = await requestPlaceholderResend(key);
|
|
1086
|
+
if (response === 'RESOLVED') {
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
logger.debug('received unavailable message, acked and requested resend from phone');
|
|
1090
|
+
}
|
|
1091
|
+
else {
|
|
1092
|
+
if (placeholderResendCache.get(node.attrs.id)) {
|
|
1093
|
+
placeholderResendCache.del(node.attrs.id);
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
const { fullMessage: msg, category, author, decrypt } = (0, Utils_1.decryptMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, logger);
|
|
1097
|
+
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) {
|
|
1098
|
+
msg.messageStubParameters = [Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT, response];
|
|
1099
|
+
}
|
|
1100
|
+
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) {
|
|
1101
|
+
ev.emit('chats.phoneNumberShare', { lid: resolveJid(node.attrs.from), jid: pnToJid(node.attrs.sender_pn) || resolveJid(node.attrs.sender_pn) });
|
|
1102
|
+
}
|
|
1103
|
+
try {
|
|
1104
|
+
await Promise.all([
|
|
1105
|
+
processingMutex.mutex(async () => {
|
|
1106
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1107
|
+
await decrypt();
|
|
1108
|
+
if (msg.message) {
|
|
1109
|
+
const contextInfos = [];
|
|
1110
|
+
collectContextInfos(msg.message, contextInfos);
|
|
1111
|
+
if (contextInfos.length) {
|
|
1112
|
+
const groupJid = (0, WABinary_1.isJidGroup)(msg.key.remoteJid) ? msg.key.remoteJid : undefined;
|
|
1113
|
+
for (const ci of contextInfos) {
|
|
1114
|
+
await normalizeContextInfoJids(ci, groupJid);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
// message failed to decrypt
|
|
1119
|
+
if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
|
|
1120
|
+
if (((_a = msg === null || msg === void 0 ? void 0 : msg.messageStubParameters) === null || _a === void 0 ? void 0 : _a[0]) === Utils_1.MISSING_KEYS_ERROR_TEXT) {
|
|
1121
|
+
return sendMessageAck(node, Utils_1.NACK_REASONS.ParsingError);
|
|
1122
|
+
}
|
|
1123
|
+
retryMutex.mutex(async () => {
|
|
1124
|
+
if (ws.isOpen) {
|
|
1125
|
+
if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
await sendRetryRequest(node, !encNode);
|
|
1129
|
+
if (retryRequestDelayMs) {
|
|
1130
|
+
await (0, Utils_1.delay)(retryRequestDelayMs);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
else {
|
|
1134
|
+
logger.debug({ node }, 'connection closed, ignoring retry req');
|
|
1135
|
+
}
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
else {
|
|
1139
|
+
let type = undefined;
|
|
1140
|
+
if ((_b = msg.key.participant) === null || _b === void 0 ? void 0 : _b.endsWith('@lid')) {
|
|
1141
|
+
msg.key.participant = pnToJid(node.attrs.participant_pn) || authState.creds.me.id;
|
|
1142
|
+
}
|
|
1143
|
+
if (!(0, WABinary_1.isJidGroup)(msg.key.remoteJid) && (0, WABinary_1.isLidUser)(msg.key.remoteJid)) {
|
|
1144
|
+
msg.key.remoteJid = pnToJid(node.attrs.sender_pn) || pnToJid(node.attrs.peer_recipient_pn) || msg.key.remoteJid;
|
|
1145
|
+
}
|
|
1146
|
+
let participant = msg.key.participant;
|
|
1147
|
+
if (category === 'peer') {
|
|
1148
|
+
type = 'peer_msg';
|
|
1149
|
+
}
|
|
1150
|
+
else if (msg.key.fromMe) {
|
|
1151
|
+
type = 'sender';
|
|
1152
|
+
if ((0, WABinary_1.isJidUser)(msg.key.remoteJid)) {
|
|
1153
|
+
participant = author;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
else if (!sendActiveReceipts) {
|
|
1157
|
+
type = 'inactive';
|
|
1158
|
+
}
|
|
1159
|
+
await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
|
|
1160
|
+
const isAnyHistoryMsg = (0, Utils_1.getHistoryMsg)(msg.message);
|
|
1161
|
+
if (isAnyHistoryMsg) {
|
|
1162
|
+
const jid = (0, WABinary_1.jidNormalizedUser)(msg.key.remoteJid);
|
|
1163
|
+
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
if (msg.messageStubType) {
|
|
1167
|
+
const hasLidParam = msg.messageStubParameters && msg.messageStubParameters.some(p => typeof p === 'string' && (0, WABinary_1.isLid)(p));
|
|
1168
|
+
if (hasLidParam) {
|
|
1169
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1170
|
+
msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' && (0, WABinary_1.isLid)(param))
|
|
1171
|
+
? await resolveLidFromGroupContext(param, msg.key.remoteJid)
|
|
1172
|
+
: param));
|
|
1173
|
+
}
|
|
1174
|
+
else {
|
|
1175
|
+
msg.messageStubParameters = msg.messageStubParameters.map(param => (typeof param === 'string' && (0, WABinary_1.isLid)(param))
|
|
1176
|
+
? (0, WABinary_1.lidToJid)(param)
|
|
1177
|
+
: param);
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
if (msg.key?.participant && typeof msg.key.participant === 'string' && (0, WABinary_1.isLid)(msg.key.participant)) {
|
|
1181
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1182
|
+
msg.key.participant = pnToJid(node.attrs.participant_pn) || await resolveLidFromGroupContext(msg.key.participant, msg.key.remoteJid);
|
|
1183
|
+
}
|
|
1184
|
+
else {
|
|
1185
|
+
msg.key.participant = pnToJid(node.attrs.participant_pn) || (0, WABinary_1.lidToJid)(msg.key.participant);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
if (msg.participant && typeof msg.participant === 'string' && (0, WABinary_1.isLid)(msg.participant)) {
|
|
1189
|
+
if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
|
|
1190
|
+
msg.participant = pnToJid(node.attrs.participant_pn) || await resolveLidFromGroupContext(msg.participant, msg.key.remoteJid);
|
|
1191
|
+
}
|
|
1192
|
+
else {
|
|
1193
|
+
msg.participant = pnToJid(node.attrs.participant_pn) || (0, WABinary_1.lidToJid)(msg.participant);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
(0, Utils_1.cleanMessage)(msg, authState.creds.me.id);
|
|
1198
|
+
await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
|
|
1199
|
+
})
|
|
1200
|
+
]);
|
|
1201
|
+
}
|
|
1202
|
+
catch (error) {
|
|
1203
|
+
logger.error({ error, node }, 'error in handling message');
|
|
1204
|
+
}
|
|
1205
|
+
});
|
|
1206
|
+
const fetchMessageHistory = async (count, oldestMsgKey, oldestMsgTimestamp) => {
|
|
1207
|
+
var _a;
|
|
1208
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1209
|
+
throw new boom_1.Boom('Not authenticated');
|
|
1210
|
+
}
|
|
1211
|
+
const pdoMessage = {
|
|
1212
|
+
historySyncOnDemandRequest: {
|
|
1213
|
+
chatJid: oldestMsgKey.remoteJid,
|
|
1214
|
+
oldestMsgFromMe: oldestMsgKey.fromMe,
|
|
1215
|
+
oldestMsgId: oldestMsgKey.id,
|
|
1216
|
+
oldestMsgTimestampMs: oldestMsgTimestamp,
|
|
1217
|
+
onDemandMsgCount: count
|
|
1218
|
+
},
|
|
1219
|
+
peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND
|
|
1220
|
+
};
|
|
1221
|
+
return sendPeerDataOperationMessage(pdoMessage);
|
|
1222
|
+
};
|
|
1223
|
+
const requestPlaceholderResend = async (messageKey) => {
|
|
1224
|
+
var _a;
|
|
1225
|
+
if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
|
|
1226
|
+
throw new boom_1.Boom('Not authenticated');
|
|
1227
|
+
}
|
|
1228
|
+
if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1229
|
+
logger.debug({ messageKey }, 'already requested resend');
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
else {
|
|
1233
|
+
placeholderResendCache.set(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id, true);
|
|
1234
|
+
}
|
|
1235
|
+
await (0, Utils_1.delay)(5000);
|
|
1236
|
+
if (!placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1237
|
+
logger.debug({ messageKey }, 'message received while resend requested');
|
|
1238
|
+
return 'RESOLVED';
|
|
1239
|
+
}
|
|
1240
|
+
const pdoMessage = {
|
|
1241
|
+
placeholderMessageResendRequest: [{
|
|
1242
|
+
messageKey
|
|
1243
|
+
}],
|
|
1244
|
+
peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.PLACEHOLDER_MESSAGE_RESEND
|
|
1245
|
+
};
|
|
1246
|
+
setTimeout(() => {
|
|
1247
|
+
if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
|
|
1248
|
+
logger.debug({ messageKey }, 'PDO message without response after 15 seconds. Phone possibly offline');
|
|
1249
|
+
placeholderResendCache.del(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id);
|
|
1250
|
+
}
|
|
1251
|
+
}, 15000);
|
|
1252
|
+
return sendPeerDataOperationMessage(pdoMessage);
|
|
1253
|
+
};
|
|
1254
|
+
const handleCall = async (node) => {
|
|
1255
|
+
const { attrs } = node;
|
|
1256
|
+
const [infoChild] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
|
|
1257
|
+
const callId = infoChild.attrs['call-id'];
|
|
1258
|
+
const status = (0, Utils_1.getCallStatusFromNode)(infoChild);
|
|
1259
|
+
const contextGroupJid = (0, WABinary_1.isJidGroup)(attrs.from) ? attrs.from : undefined;
|
|
1260
|
+
const resolvedCallCreator = await resolveLidFromGroupContext(infoChild.attrs.from || infoChild.attrs['call-creator'], contextGroupJid);
|
|
1261
|
+
const resolvedChatId = await resolveLidFromGroupContext(attrs.from, contextGroupJid);
|
|
1262
|
+
const call = {
|
|
1263
|
+
chatId: resolvedChatId,
|
|
1264
|
+
from: resolvedCallCreator,
|
|
1265
|
+
id: callId,
|
|
1266
|
+
date: new Date(+attrs.t * 1000),
|
|
1267
|
+
offline: !!attrs.offline,
|
|
1268
|
+
status,
|
|
1269
|
+
};
|
|
1270
|
+
if (status === 'offer') {
|
|
1271
|
+
call.isVideo = !!(0, WABinary_1.getBinaryNodeChild)(infoChild, 'video');
|
|
1272
|
+
call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid'];
|
|
1273
|
+
if (infoChild.attrs['group-jid']) {
|
|
1274
|
+
call.groupJid = await resolveLidFromGroupContext(infoChild.attrs['group-jid'], infoChild.attrs['group-jid']);
|
|
1275
|
+
}
|
|
1276
|
+
callOfferCache.set(call.id, call);
|
|
1277
|
+
}
|
|
1278
|
+
const existingCall = callOfferCache.get(call.id);
|
|
1279
|
+
if (existingCall) {
|
|
1280
|
+
call.isVideo = existingCall.isVideo;
|
|
1281
|
+
call.isGroup = existingCall.isGroup;
|
|
1282
|
+
}
|
|
1283
|
+
if (status === 'reject' || status === 'accept' || status === 'timeout' || status === 'terminate') {
|
|
1284
|
+
callOfferCache.del(call.id);
|
|
1285
|
+
}
|
|
1286
|
+
ev.emit('call', [call]);
|
|
1287
|
+
await sendMessageAck(node);
|
|
1288
|
+
};
|
|
1289
|
+
const handleBadAck = async ({ attrs }) => {
|
|
1290
|
+
const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id, 'server_id': attrs === null || attrs === void 0 ? void 0 : attrs.server_id };
|
|
1291
|
+
// current hypothesis is that if pash is sent in the ack
|
|
1292
|
+
// it means -- the message hasn't reached all devices yet
|
|
1293
|
+
// we'll retry sending the message here
|
|
1294
|
+
if (attrs.phash && attrs.class === 'message') {
|
|
1295
|
+
logger.info({ attrs }, 'received phash in ack, resending message...');
|
|
1296
|
+
const cacheKey = `${key.remoteJid}:${key.id}`;
|
|
1297
|
+
const retryCount = msgRetryCache.get(cacheKey) || 0;
|
|
1298
|
+
if (retryCount >= maxMsgRetryCount) {
|
|
1299
|
+
logger.warn({ attrs }, 'reached max retry count, not sending message again');
|
|
1300
|
+
msgRetryCache.del(cacheKey);
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
const msg = await getMessage(key);
|
|
1304
|
+
if (msg) {
|
|
1305
|
+
await relayMessage(key.remoteJid, msg, { messageId: key.id, useUserDevicesCache: false });
|
|
1306
|
+
msgRetryCache.set(cacheKey, retryCount + 1);
|
|
1307
|
+
}
|
|
1308
|
+
else {
|
|
1309
|
+
logger.warn({ attrs }, 'could not send message again, as it was not found');
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
if (attrs.error) {
|
|
1313
|
+
logger.warn({ attrs }, 'received error in ack');
|
|
1314
|
+
ev.emit('messages.update', [
|
|
1315
|
+
{
|
|
1316
|
+
key,
|
|
1317
|
+
update: {
|
|
1318
|
+
status: Types_1.WAMessageStatus.ERROR,
|
|
1319
|
+
messageStubParameters: [
|
|
1320
|
+
attrs.error
|
|
1321
|
+
]
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
]);
|
|
1325
|
+
}
|
|
1326
|
+
};
|
|
1327
|
+
const processNodeWithBuffer = async (node, identifier, exec) => {
|
|
1328
|
+
ev.buffer();
|
|
1329
|
+
await execTask();
|
|
1330
|
+
ev.flush();
|
|
1331
|
+
function execTask() {
|
|
1332
|
+
return exec(node, false)
|
|
1333
|
+
.catch(err => onUnexpectedError(err, identifier));
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
const makeOfflineNodeProcessor = () => {
|
|
1337
|
+
const nodeProcessorMap = new Map([
|
|
1338
|
+
['message', handleMessage],
|
|
1339
|
+
['call', handleCall],
|
|
1340
|
+
['receipt', handleReceipt],
|
|
1341
|
+
['notification', handleNotification]
|
|
1342
|
+
]);
|
|
1343
|
+
const nodes = [];
|
|
1344
|
+
let isProcessing = false;
|
|
1345
|
+
const enqueue = (type, node) => {
|
|
1346
|
+
nodes.push({ type, node });
|
|
1347
|
+
if (isProcessing) {
|
|
1348
|
+
return;
|
|
1349
|
+
}
|
|
1350
|
+
isProcessing = true;
|
|
1351
|
+
const promise = async () => {
|
|
1352
|
+
try {
|
|
1353
|
+
while (nodes.length && ws.isOpen) {
|
|
1354
|
+
const { type, node } = nodes.shift();
|
|
1355
|
+
const nodeProcessor = nodeProcessorMap.get(type);
|
|
1356
|
+
if (!nodeProcessor) {
|
|
1357
|
+
onUnexpectedError(new Error(`unknown offline node type: ${type}`), 'processing offline node');
|
|
1358
|
+
continue;
|
|
1359
|
+
}
|
|
1360
|
+
try {
|
|
1361
|
+
await nodeProcessor(node);
|
|
1362
|
+
}
|
|
1363
|
+
catch (error) {
|
|
1364
|
+
onUnexpectedError(error, 'processing offline node');
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
finally {
|
|
1369
|
+
isProcessing = false;
|
|
1370
|
+
}
|
|
1371
|
+
};
|
|
1372
|
+
promise().catch(error => onUnexpectedError(error, 'processing offline nodes'));
|
|
1373
|
+
};
|
|
1374
|
+
return { enqueue };
|
|
1375
|
+
};
|
|
1376
|
+
const offlineNodeProcessor = makeOfflineNodeProcessor();
|
|
1377
|
+
const processNode = (type, node, identifier, exec) => {
|
|
1378
|
+
const isOffline = !!node.attrs.offline;
|
|
1379
|
+
if (isOffline) {
|
|
1380
|
+
offlineNodeProcessor.enqueue(type, node);
|
|
1381
|
+
}
|
|
1382
|
+
else {
|
|
1383
|
+
processNodeWithBuffer(node, identifier, exec);
|
|
1384
|
+
}
|
|
1385
|
+
};
|
|
1386
|
+
ws.on('CB:message', (node) => {
|
|
1387
|
+
processNode('message', node, 'processing message', handleMessage);
|
|
1388
|
+
});
|
|
1389
|
+
ws.on('CB:call', async (node) => {
|
|
1390
|
+
processNode('call', node, 'handling call', handleCall);
|
|
1391
|
+
});
|
|
1392
|
+
ws.on('CB:receipt', node => {
|
|
1393
|
+
processNode('receipt', node, 'handling receipt', handleReceipt);
|
|
1394
|
+
});
|
|
1395
|
+
ws.on('CB:notification', async (node) => {
|
|
1396
|
+
processNode('notification', node, 'handling notification', handleNotification);
|
|
1397
|
+
});
|
|
1398
|
+
ws.on('CB:ack,class:message', (node) => {
|
|
1399
|
+
handleBadAck(node)
|
|
1400
|
+
.catch(error => onUnexpectedError(error, 'handling bad ack'));
|
|
1401
|
+
});
|
|
1402
|
+
ev.on('call', ([call]) => {
|
|
1403
|
+
if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {
|
|
1404
|
+
const msg = {
|
|
1405
|
+
key: {
|
|
1406
|
+
remoteJid: call.chatId,
|
|
1407
|
+
id: call.id,
|
|
1408
|
+
fromMe: false
|
|
1409
|
+
},
|
|
1410
|
+
messageTimestamp: (0, Utils_1.unixTimestampSeconds)(call.date),
|
|
1411
|
+
};
|
|
1412
|
+
if (call.status === 'timeout') {
|
|
1413
|
+
if (call.isGroup) {
|
|
1414
|
+
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_GROUP_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_GROUP_VOICE;
|
|
1415
|
+
}
|
|
1416
|
+
else {
|
|
1417
|
+
msg.messageStubType = call.isVideo ? Types_1.WAMessageStubType.CALL_MISSED_VIDEO : Types_1.WAMessageStubType.CALL_MISSED_VOICE;
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
else {
|
|
1421
|
+
msg.message = { call: { callKey: Buffer.from(call.id) } };
|
|
1422
|
+
}
|
|
1423
|
+
const protoMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
|
|
1424
|
+
upsertMessage(protoMsg, call.offline ? 'append' : 'notify');
|
|
1425
|
+
}
|
|
1426
|
+
});
|
|
1427
|
+
ev.on('connection.update', (update) => {
|
|
1428
|
+
const { connection, lastDisconnect } = update;
|
|
1429
|
+
if (connection === 'close') {
|
|
1430
|
+
const statusCode = lastDisconnect?.error?.output?.statusCode;
|
|
1431
|
+
const shouldReconnect = statusCode !== Types_1.DisconnectReason.loggedOut;
|
|
1432
|
+
if (shouldReconnect) {
|
|
1433
|
+
logger.info('Connection closed, will handle reconnection automatically');
|
|
1434
|
+
} else {
|
|
1435
|
+
logger.warn('Logged out, manual re-authentication required');
|
|
1436
|
+
}
|
|
1437
|
+
} else if (connection === 'open') {
|
|
1438
|
+
sendActiveReceipts = true;
|
|
1439
|
+
}
|
|
1440
|
+
if (typeof update.isOnline !== 'undefined' && update.isOnline) {
|
|
1441
|
+
sendActiveReceipts = true;
|
|
1442
|
+
logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);
|
|
1443
|
+
}
|
|
1444
|
+
});
|
|
1445
|
+
ev.on('messages.update', (updates) => {
|
|
1446
|
+
const config = (0, performance_config_1.getPerformanceConfig)();
|
|
1447
|
+
updates.forEach(update => {
|
|
1448
|
+
if (update.update.status === WAProto_1.proto.WebMessageInfo.Status.PENDING &&
|
|
1449
|
+
Date.now() - (update.update.timestamp || 0) > 30000) {
|
|
1450
|
+
logger.debug({ key: update.key }, 'retrying stuck pending message with anti-ban delay');
|
|
1451
|
+
setTimeout(async () => {
|
|
1452
|
+
try {
|
|
1453
|
+
const msg = await getMessage(update.key);
|
|
1454
|
+
if (msg) {
|
|
1455
|
+
await relayMessage(update.key.remoteJid, msg, { messageId: update.key.id });
|
|
1456
|
+
}
|
|
1457
|
+
} catch (err) {
|
|
1458
|
+
logger.error({ err, key: update.key }, 'failed to retry stuck message');
|
|
1459
|
+
}
|
|
1460
|
+
}, config.security?.messageDelay?.min || 1000);
|
|
1461
|
+
}
|
|
1462
|
+
});
|
|
1463
|
+
});
|
|
1464
|
+
return {
|
|
1465
|
+
...sock,
|
|
1466
|
+
sendMessageAck,
|
|
1467
|
+
sendRetryRequest,
|
|
1468
|
+
rejectCall,
|
|
1469
|
+
offerCall,
|
|
1470
|
+
fetchMessageHistory,
|
|
1471
|
+
requestPlaceholderResend,
|
|
1472
|
+
};
|
|
1473
|
+
};
|
|
1229
1474
|
exports.makeMessagesRecvSocket = makeMessagesRecvSocket;
|