@realvare/based 2.7.70 → 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.
@@ -34,6 +34,14 @@ const makeMessagesRecvSocket = (config) => {
34
34
  stdTTL: Defaults_1.DEFAULT_CACHE_TTLS.MSG_RETRY,
35
35
  useClones: false
36
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
+ });
37
45
  const sentMessageCache = new Map();
38
46
  const sentMessageCacheMaxSize = 256;
39
47
  const getSentMessageCacheKey = (remoteJid, id) => `${remoteJid}:${id}`;
@@ -62,12 +70,7 @@ const makeMessagesRecvSocket = (config) => {
62
70
  }
63
71
  };
64
72
  let sendActiveReceipts = false;
65
- const resolveJid = (jid) => {
66
- if (typeof jid === 'string' && jid.endsWith('@lid')) {
67
- return (0, WABinary_1.lidToJid)(jid);
68
- }
69
- return jid;
70
- };
73
+ const resolveJid = WABinary_1.resolveJid;
71
74
  const sendMessageAck = async ({ tag, attrs, content }, errorCode) => {
72
75
  const stanza = {
73
76
  tag: 'ack',
@@ -271,45 +274,137 @@ const makeMessagesRecvSocket = (config) => {
271
274
  if (identityNode) {
272
275
  logger.info({ jid: from }, 'identity changed');
273
276
  }
274
- else {
275
- logger.info({ node }, 'unknown encrypt notification');
276
- }
277
277
  }
278
278
  };
279
+
279
280
  const toLidIfNecessary = (jid) => {
280
281
  if (typeof jid !== 'string')
281
282
  return jid;
282
- const [user, server] = jid.split('@');
283
- if (server === 's.whatsapp.net' && /^[0-9]+$/.test(user) && user.length >= 12) {
284
- return `${user}@lid`;
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;
285
292
  }
286
293
  return jid;
287
294
  };
295
+
288
296
  // Helper for LID resolution in group context by samakavare
289
297
  const resolveLidFromGroupContext = async (lid, groupJid) => {
290
298
  if (!(0, WABinary_1.isLid)(lid) || !(0, WABinary_1.isJidGroup)(groupJid)) {
291
299
  return lid;
292
300
  }
293
- const metadata = await groupMetadata(groupJid);
294
- const found = metadata.participants.find(p => p.id === lid);
295
- let jid = found?.jid;
296
- if (!jid) {
297
- jid = config.lidCache?.get(lid);
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
298
312
  }
299
- return jid || (0, WABinary_1.lidToJid)(lid); // Fallback to naive if not found
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;
300
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
+
301
394
  const handleGroupNotification = async (participant, participantPn, child, groupJid, msg) => {
302
395
  var _a, _b, _c, _d;
303
396
  const childTag = child === null || child === void 0 ? void 0 : child.tag;
304
397
  if (participantPn && participant && (0, WABinary_1.isLid)(participant) && config.lidCache?.set) {
305
398
  // NOTE: in most if not every w:gp2 stubs participant_pn refer to the actor (admin) not the target
306
- config.lidCache.set(participant, participantPn);
399
+ const pnAsJid = typeof participantPn === 'string' ? (participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`) : participantPn;
400
+ config.lidCache.set(participant, pnAsJid);
307
401
  }
308
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);
309
403
  if (participantPn && participantJid && (0, WABinary_1.isLid)(participantJid) && config.lidCache?.set &&
310
404
  (childTag === 'created_membership_requests' || childTag === 'revoked_membership_requests')) {
311
405
  // For membership requests, participant_pn refers to the requester (target), not the actor
312
- config.lidCache.set(participantJid, participantPn);
406
+ const pnAsJid = typeof participantPn === 'string' ? (participantPn.includes('@') ? participantPn : `${participantPn}@s.whatsapp.net`) : participantPn;
407
+ config.lidCache.set(participantJid, pnAsJid);
313
408
  }
314
409
  switch (child === null || child === void 0 ? void 0 : child.tag) {
315
410
  case 'create':
@@ -347,7 +442,18 @@ const makeMessagesRecvSocket = (config) => {
347
442
  };
348
443
  break;
349
444
  case 'modify':
350
- const oldNumber = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
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
+ });
351
457
  msg.messageStubParameters = oldNumber || [];
352
458
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
353
459
  break;
@@ -358,7 +464,48 @@ const makeMessagesRecvSocket = (config) => {
358
464
  case 'leave':
359
465
  const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
360
466
  msg.messageStubType = Types_1.WAMessageStubType[stubType];
361
- const participants = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
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
+ }));
362
509
  if (participants.length === 1 &&
363
510
  (0, WABinary_1.areJidsSameUser)(participants[0], participant) &&
364
511
  child.tag === 'remove') {
@@ -368,9 +515,9 @@ const makeMessagesRecvSocket = (config) => {
368
515
  && participants.length === 1
369
516
  && participantPn
370
517
  && typeof participantPn === 'string') {
371
- msg.messageStubParameters = [participantPn];
518
+ msg.messageStubParameters = [toLidIfNecessary(participantPn)];
372
519
  if (participant && (0, WABinary_1.isLid)(participant)) {
373
- participant = participantPn;
520
+ participant = toLidIfNecessary(participantPn);
374
521
  }
375
522
  }
376
523
  else {
@@ -416,49 +563,63 @@ const makeMessagesRecvSocket = (config) => {
416
563
  break;
417
564
  case 'created_membership_requests':
418
565
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
419
- msg.messageStubParameters = [participantPn || participantJid, 'created', child.attrs.request_method];
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];
420
577
  break;
421
578
  case 'revoked_membership_requests':
422
579
  const isDenied = (0, WABinary_1.areJidsSameUser)(participantJid, participant);
423
580
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD;
424
- msg.messageStubParameters = [participantPn || participantJid, isDenied ? 'revoked' : 'rejected'];
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'];
425
592
  break;
426
593
  default:
427
- // console.log("BAILEYS-DEBUG:", JSON.stringify({ ...child, content: Buffer.isBuffer(child.content) ? child.content.toString() : child.content, participant }, null, 2))
594
+ // console.log("BAILEYS-DEBUG:", JSON.stringify({ ...child, content: Buffer.isBuffer(child.content) ? child.content.toString() : child.content, participant }, null, 2))
428
595
  }
429
- if (msg.messageStubParameters) {
430
- msg.messageStubParameters = msg.messageStubParameters.map(toLidIfNecessary);
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)));
431
602
  }
432
603
  participant = toLidIfNecessary(participant);
433
604
  if (msg.key?.participant) {
434
605
  msg.key.participant = toLidIfNecessary(msg.key.participant);
435
606
  }
436
- const needsResolving = (msg.messageStubParameters && msg.messageStubParameters.some(p => (0, WABinary_1.isLid)(p))) ||
437
- (participant && (0, WABinary_1.isLid)(participant)) ||
438
- (msg.key?.participant && (0, WABinary_1.isLid)(msg.key.participant));
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])))));
439
610
  if (needsResolving) {
440
- const metadata = await groupMetadata(groupJid);
441
- const { lidCache } = config;
442
- const resolveLid = (lid) => {
443
- const found = metadata.participants.find(p => p.id === lid);
444
- let jid = found === null || found === void 0 ? void 0 : found.jid;
445
- if (!jid) {
446
- jid = lidCache === null || lidCache === void 0 ? void 0 : lidCache.get(lid);
447
- }
448
- return jid || (0, WABinary_1.lidToJid)(lid);
449
- };
450
611
  if (msg.messageStubParameters) {
451
- msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' && (0, WABinary_1.isLid)(param) ? await resolveLidFromGroupContext(param, groupJid) : param)));
612
+ msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' ? await resolveLidOrMaskedJidFromGroupContext(param, groupJid) : param)));
452
613
  }
453
- if ((0, WABinary_1.isLid)(participant)) {
454
- msg.participant = await resolveLidFromGroupContext(participant, groupJid);
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);
455
616
  }
456
617
  else if (participant) {
457
618
  //If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah
458
619
  msg.participant = participant;
459
620
  }
460
- if (msg.key && (0, WABinary_1.isLid)(msg.key.participant)) {
461
- msg.key.participant = await resolveLidFromGroupContext(msg.key.participant, groupJid);
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);
462
623
  }
463
624
  else if (msg.key && msg.key.participant) {
464
625
  // If it's a JID, treat it as a JID. Do not convert back to LID. *smh brah pt2
@@ -466,6 +627,7 @@ const makeMessagesRecvSocket = (config) => {
466
627
  }
467
628
  }
468
629
  };
630
+
469
631
  const handleNewsletterNotification = (id, node) => {
470
632
  const messages = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
471
633
  const message = (0, WABinary_1.getBinaryNodeChild)(messages, 'message');
@@ -488,6 +650,7 @@ const makeMessagesRecvSocket = (config) => {
488
650
  });
489
651
  }
490
652
  };
653
+
491
654
  const handleMexNewsletterNotification = (id, node) => {
492
655
  var _a;
493
656
  const operation = node === null || node === void 0 ? void 0 : node.attrs.op_name;
@@ -512,8 +675,9 @@ const makeMessagesRecvSocket = (config) => {
512
675
  ev.emit('newsletter-settings.update', { id, update: contentPath.thread_metadata.settings });
513
676
  }
514
677
  };
678
+
515
679
  const processNotification = async (node) => {
516
- var _a, _b;
680
+ var _a;
517
681
  const result = {};
518
682
  const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
519
683
  const nodeType = node.attrs.type;
@@ -674,6 +838,7 @@ const makeMessagesRecvSocket = (config) => {
674
838
  return result;
675
839
  }
676
840
  };
841
+
677
842
  async function decipherLinkPublicKey(data) {
678
843
  const buffer = toRequiredBuffer(data);
679
844
  const salt = buffer.slice(0, 32);
@@ -738,16 +903,20 @@ const makeMessagesRecvSocket = (config) => {
738
903
  const { attrs, content } = node;
739
904
  let participant = attrs.participant;
740
905
  if (participant && (0, WABinary_1.isLid)(participant) && (0, WABinary_1.isJidGroup)(attrs.from)) {
741
- const metadata = await groupMetadata(attrs.from);
742
- const found = metadata.participants.find(p => p.id === participant);
743
- const jid = found === null || found === void 0 ? void 0 : found.jid;
744
- if (jid && !(0, WABinary_1.isLid)(jid)) {
745
- participant = jid;
906
+ const cached = config.lidCache.get(participant);
907
+ if (cached) {
908
+ participant = typeof cached === 'string' && !cached.includes('@') ? `${cached}@s.whatsapp.net` : cached;
746
909
  }
747
910
  else {
748
- const cached = config.lidCache.get(participant);
749
- if (cached) {
750
- participant = cached;
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) {
751
920
  }
752
921
  }
753
922
  }
@@ -843,12 +1012,27 @@ const makeMessagesRecvSocket = (config) => {
843
1012
  await sendMessageAck(node);
844
1013
  return;
845
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);
846
1021
  try {
847
1022
  await Promise.all([
848
1023
  processingMutex.mutex(async () => {
849
1024
  var _a;
850
1025
  const msg = await processNotification(node);
851
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
+ }
852
1036
  const participant = msg.participant || resolveJid(node.attrs.participant);
853
1037
  const fromMe = (0, WABinary_1.areJidsSameUser)(participant || remoteJid, authState.creds.me.id);
854
1038
  const key = msg.key || {};
@@ -914,36 +1098,20 @@ const makeMessagesRecvSocket = (config) => {
914
1098
  msg.messageStubParameters = [Utils_1.NO_MESSAGE_FOUND_ERROR_TEXT, response];
915
1099
  }
916
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) {
917
- ev.emit('chats.phoneNumberShare', { lid: resolveJid(node.attrs.from), jid: resolveJid(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) });
918
1102
  }
919
1103
  try {
920
1104
  await Promise.all([
921
1105
  processingMutex.mutex(async () => {
922
1106
  var _a, _b, _c, _d, _e, _f;
923
1107
  await decrypt();
924
- // if the message is from a group, and contains mentions, resolve the LIDs to JIDs by samakavare 🎐
925
- if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
926
- const contextInfo = msg.message?.extendedTextMessage?.contextInfo;
927
- if (contextInfo) {
928
- const participant = contextInfo.participant;
929
- const mentionedJid = contextInfo.mentionedJid;
930
- const hasLidInParticipant = participant && participant.endsWith('@lid');
931
- const hasLidInMention = mentionedJid && mentionedJid.some(j => j.endsWith('@lid'));
932
- if (hasLidInParticipant || hasLidInMention) {
933
- const metadata = await groupMetadata(msg.key.remoteJid);
934
- if (hasLidInParticipant) {
935
- const found = metadata.participants.find(p => p.id === participant);
936
- contextInfo.participant = (found?.jid) || participant;
937
- }
938
- if (hasLidInMention) {
939
- contextInfo.mentionedJid = await Promise.all(mentionedJid.map(async (jid) => {
940
- if (jid.endsWith('@lid')) {
941
- const found = metadata.participants.find(p => p.id === jid);
942
- return (found?.jid) || jid;
943
- }
944
- return jid;
945
- }));
946
- }
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);
947
1115
  }
948
1116
  }
949
1117
  }
@@ -957,7 +1125,6 @@ const makeMessagesRecvSocket = (config) => {
957
1125
  if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable')) {
958
1126
  return;
959
1127
  }
960
- const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
961
1128
  await sendRetryRequest(node, !encNode);
962
1129
  if (retryRequestDelayMs) {
963
1130
  await (0, Utils_1.delay)(retryRequestDelayMs);
@@ -971,10 +1138,10 @@ const makeMessagesRecvSocket = (config) => {
971
1138
  else {
972
1139
  let type = undefined;
973
1140
  if ((_b = msg.key.participant) === null || _b === void 0 ? void 0 : _b.endsWith('@lid')) {
974
- msg.key.participant = node.attrs.participant_pn || authState.creds.me.id;
1141
+ msg.key.participant = pnToJid(node.attrs.participant_pn) || authState.creds.me.id;
975
1142
  }
976
1143
  if (!(0, WABinary_1.isJidGroup)(msg.key.remoteJid) && (0, WABinary_1.isLidUser)(msg.key.remoteJid)) {
977
- msg.key.remoteJid = node.attrs.sender_pn || node.attrs.peer_recipient_pn;
1144
+ msg.key.remoteJid = pnToJid(node.attrs.sender_pn) || pnToJid(node.attrs.peer_recipient_pn) || msg.key.remoteJid;
978
1145
  }
979
1146
  let participant = msg.key.participant;
980
1147
  if (category === 'peer') {
@@ -1000,9 +1167,8 @@ const makeMessagesRecvSocket = (config) => {
1000
1167
  const hasLidParam = msg.messageStubParameters && msg.messageStubParameters.some(p => typeof p === 'string' && (0, WABinary_1.isLid)(p));
1001
1168
  if (hasLidParam) {
1002
1169
  if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
1003
- const groupJid = msg.key.remoteJid;
1004
1170
  msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => (typeof param === 'string' && (0, WABinary_1.isLid)(param))
1005
- ? await resolveLidFromGroupContext(param, groupJid)
1171
+ ? await resolveLidFromGroupContext(param, msg.key.remoteJid)
1006
1172
  : param));
1007
1173
  }
1008
1174
  else {
@@ -1013,18 +1179,18 @@ const makeMessagesRecvSocket = (config) => {
1013
1179
  }
1014
1180
  if (msg.key?.participant && typeof msg.key.participant === 'string' && (0, WABinary_1.isLid)(msg.key.participant)) {
1015
1181
  if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
1016
- msg.key.participant = node.attrs.participant_pn || await resolveLidFromGroupContext(msg.key.participant, msg.key.remoteJid);
1182
+ msg.key.participant = pnToJid(node.attrs.participant_pn) || await resolveLidFromGroupContext(msg.key.participant, msg.key.remoteJid);
1017
1183
  }
1018
1184
  else {
1019
- msg.key.participant = node.attrs.participant_pn || (0, WABinary_1.lidToJid)(msg.key.participant);
1185
+ msg.key.participant = pnToJid(node.attrs.participant_pn) || (0, WABinary_1.lidToJid)(msg.key.participant);
1020
1186
  }
1021
1187
  }
1022
1188
  if (msg.participant && typeof msg.participant === 'string' && (0, WABinary_1.isLid)(msg.participant)) {
1023
1189
  if ((0, WABinary_1.isJidGroup)(msg.key.remoteJid)) {
1024
- msg.participant = node.attrs.participant_pn || await resolveLidFromGroupContext(msg.participant, msg.key.remoteJid);
1190
+ msg.participant = pnToJid(node.attrs.participant_pn) || await resolveLidFromGroupContext(msg.participant, msg.key.remoteJid);
1025
1191
  }
1026
1192
  else {
1027
- msg.participant = node.attrs.participant_pn || (0, WABinary_1.lidToJid)(msg.participant);
1193
+ msg.participant = pnToJid(node.attrs.participant_pn) || (0, WABinary_1.lidToJid)(msg.participant);
1028
1194
  }
1029
1195
  }
1030
1196
  }
@@ -1183,16 +1349,25 @@ const makeMessagesRecvSocket = (config) => {
1183
1349
  }
1184
1350
  isProcessing = true;
1185
1351
  const promise = async () => {
1186
- while (nodes.length && ws.isOpen) {
1187
- const { type, node } = nodes.shift();
1188
- const nodeProcessor = nodeProcessorMap.get(type);
1189
- if (!nodeProcessor) {
1190
- onUnexpectedError(new Error(`unknown offline node type: ${type}`), 'processing offline node');
1191
- continue;
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
+ }
1192
1366
  }
1193
- await nodeProcessor(node);
1194
1367
  }
1195
- isProcessing = false;
1368
+ finally {
1369
+ isProcessing = false;
1370
+ }
1196
1371
  };
1197
1372
  promise().catch(error => onUnexpectedError(error, 'processing offline nodes'));
1198
1373
  };