@realvare/based 2.7.5 → 2.7.52

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.
@@ -46,7 +46,7 @@ const makeMessagesRecvSocket = (config) => {
46
46
  tag: 'ack',
47
47
  attrs: {
48
48
  id: attrs.id,
49
- to: attrs.from,
49
+ to: resolveJid(attrs.from),
50
50
  class: tag
51
51
  }
52
52
  };
@@ -54,10 +54,10 @@ const makeMessagesRecvSocket = (config) => {
54
54
  stanza.attrs.error = errorCode.toString();
55
55
  }
56
56
  if (!!attrs.participant) {
57
- stanza.attrs.participant = attrs.participant;
57
+ stanza.attrs.participant = resolveJid(attrs.participant);
58
58
  }
59
59
  if (!!attrs.recipient) {
60
- stanza.attrs.recipient = attrs.recipient;
60
+ stanza.attrs.recipient = resolveJid(attrs.recipient);
61
61
  }
62
62
  if (!!attrs.type && (tag !== 'message' || (0, WABinary_1.getBinaryNodeChild)({ tag, attrs, content }, 'unavailable') || errorCode !== 0)) {
63
63
  stanza.attrs.type = attrs.type;
@@ -68,7 +68,6 @@ const makeMessagesRecvSocket = (config) => {
68
68
  logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
69
69
  await sendNode(stanza);
70
70
  };
71
-
72
71
  // Add withAck wrapper for guaranteed acknowledgments
73
72
  const withAck = (processFn) => async (node) => {
74
73
  try {
@@ -118,7 +117,7 @@ const makeMessagesRecvSocket = (config) => {
118
117
  const stanza = ({
119
118
  tag: 'call',
120
119
  attrs: {
121
- to: toJid,
120
+ to: resolveJid(toJid),
122
121
  },
123
122
  content: [{
124
123
  tag: 'offer',
@@ -141,13 +140,13 @@ const makeMessagesRecvSocket = (config) => {
141
140
  tag: 'call',
142
141
  attrs: {
143
142
  from: authState.creds.me.id,
144
- to: callFrom,
143
+ to: resolveJid(callFrom),
145
144
  },
146
145
  content: [{
147
146
  tag: 'reject',
148
147
  attrs: {
149
148
  'call-id': callId,
150
- 'call-creator': callFrom,
149
+ 'call-creator': resolveJid(callFrom),
151
150
  count: '0',
152
151
  },
153
152
  content: undefined,
@@ -181,7 +180,7 @@ const makeMessagesRecvSocket = (config) => {
181
180
  attrs: {
182
181
  id: msgId,
183
182
  type: 'retry',
184
- to: node.attrs.from
183
+ to: resolveJid(node.attrs.from)
185
184
  },
186
185
  content: [
187
186
  {
@@ -201,10 +200,10 @@ const makeMessagesRecvSocket = (config) => {
201
200
  ]
202
201
  };
203
202
  if (node.attrs.recipient) {
204
- receipt.attrs.recipient = node.attrs.recipient;
203
+ receipt.attrs.recipient = resolveJid(node.attrs.recipient);
205
204
  }
206
205
  if (node.attrs.participant) {
207
- receipt.attrs.participant = node.attrs.participant;
206
+ receipt.attrs.participant = resolveJid(node.attrs.participant);
208
207
  }
209
208
  if (retryCount > 1 || forceIncludeKeys) {
210
209
  const { update, preKeys } = await (0, Utils_1.getNextPreKeys)(authState, 1);
@@ -229,7 +228,7 @@ const makeMessagesRecvSocket = (config) => {
229
228
  });
230
229
  };
231
230
  const handleEncryptNotification = async (node) => {
232
- const from = node.attrs.from;
231
+ const from = resolveJid(node.attrs.from);
233
232
  if (from === WABinary_1.S_WHATSAPP_NET) {
234
233
  const countChild = (0, WABinary_1.getBinaryNodeChild)(node, 'count');
235
234
  const count = +countChild.attrs.value;
@@ -294,22 +293,24 @@ const makeMessagesRecvSocket = (config) => {
294
293
  msg.messageStubParameters = oldNumber || [];
295
294
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER;
296
295
  break;
296
+ case 'add':
297
+ case 'remove':
297
298
  case 'promote':
298
299
  case 'demote':
299
- case 'remove':
300
- case 'add':
301
300
  case 'leave':
302
- const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;
303
- msg.messageStubType = Types_1.WAMessageStubType[stubType];
304
- const participants = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
305
- if (participants.length === 1 &&
306
- // if recv. "remove" message and sender removed themselves
307
- // mark as left
308
- (0, WABinary_1.areJidsSameUser)(participants[0], participant) &&
309
- child.tag === 'remove') {
310
- msg.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;
301
+ const stubTypes = {
302
+ add: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
303
+ remove: Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE,
304
+ promote: Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE,
305
+ demote: Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE,
306
+ leave: Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE,
307
+ };
308
+ msg.messageStubType = stubTypes[child.tag];
309
+ let affected = (0, WABinary_1.getBinaryNodeChildren)(child, 'participant').map(p => p.attrs.jid);
310
+ if (affected.length === 0) {
311
+ affected = [participant];
311
312
  }
312
- msg.messageStubParameters = participants;
313
+ msg.messageStubParameters = affected;
313
314
  break;
314
315
  case 'subject':
315
316
  msg.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;
@@ -622,7 +623,7 @@ const makeMessagesRecvSocket = (config) => {
622
623
  // todo: implement a cache to store the last 256 sent messages (copy whatsmeow)
623
624
  const msgs = await Promise.all(ids.map(id => getMessage({ ...key, id })));
624
625
  const remoteJid = key.remoteJid;
625
- const participant = key.participant || remoteJid;
626
+ const participant = resolveJid(key.participant || remoteJid);
626
627
  // if it's the primary jid sending the request
627
628
  // just re-send the message to everyone
628
629
  // prevents the first message decryption failure
@@ -657,7 +658,7 @@ const makeMessagesRecvSocket = (config) => {
657
658
  const { attrs, content } = node;
658
659
  const isLid = attrs.from.includes('lid');
659
660
  const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(resolveJid(attrs.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);
660
- const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? resolveJid(attrs.from) : attrs.recipient;
661
+ const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? resolveJid(attrs.from) : resolveJid(attrs.recipient);
661
662
  const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe);
662
663
  const key = {
663
664
  remoteJid,
@@ -718,13 +719,14 @@ const makeMessagesRecvSocket = (config) => {
718
719
  }
719
720
  if (attrs.type === 'retry') {
720
721
  // correctly set who is asking for the retry
721
- key.participant = key.participant || attrs.from;
722
+ key.participant = key.participant || resolveJid(attrs.from);
722
723
  const retryNode = (0, WABinary_1.getBinaryNodeChild)(node, 'retry');
723
724
  if (willSendMessageAgain(ids[0], key.participant)) {
724
725
  if (key.fromMe) {
725
726
  try {
726
727
  logger.debug({ attrs, key }, 'recv retry request');
727
- await sendMessagesAgain(key, ids, retryNode);
728
+ sendMessagesAgain(key, ids, retryNode)
729
+ .catch(error => logger.error({ key, ids, trace: error.stack }, 'error in sending message again'));
728
730
  }
729
731
  catch (error) {
730
732
  logger.error({ key, ids, trace: error.stack }, 'error in sending message again');
@@ -780,7 +782,7 @@ const makeMessagesRecvSocket = (config) => {
780
782
  };
781
783
  const handleMessage = withAck(async (node) => {
782
784
  var _a, _b, _c;
783
- if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
785
+ if (shouldIgnoreJid(resolveJid(node.attrs.from)) && node.attrs.from !== '@s.whatsapp.net') {
784
786
  logger.debug({ key: node.attrs.key }, 'ignored message');
785
787
  return;
786
788
  }
@@ -815,19 +817,7 @@ const makeMessagesRecvSocket = (config) => {
815
817
  await Promise.all([
816
818
  processingMutex.mutex(async () => {
817
819
  var _a, _b, _c, _d, _e, _f;
818
- try {
819
- await decrypt();
820
- } catch (error) {
821
- if(error.message.includes('Bad MAC') || error.message.includes('No matching sessions')) {
822
- logger.warn({ key: msg.key, jid: author, error: error.message }, 'Decryption failed, clearing session to recover');
823
- const { SessionRecord } = require('../Signal');
824
- await signalRepository.storeSession(author, new SessionRecord());
825
- return sendMessageAck(node, Utils_1.NACK_REASONS.ParsingError);
826
- }
827
-
828
- throw error;
829
- }
830
-
820
+ await decrypt();
831
821
  // message failed to decrypt
832
822
  if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.StubType.CIPHERTEXT) {
833
823
  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) {
@@ -860,8 +850,7 @@ const makeMessagesRecvSocket = (config) => {
860
850
  const metadata = await groupMetadata(msg.key.remoteJid);
861
851
  const sender = msg.message.extendedTextMessage.contextInfo.participant;
862
852
  const found = metadata.participants.find(p => p.id === sender);
863
- msg.message.extendedTextMessage.contextInfo.participant = (found === null || found === void 0 ? void 0 : found.jid) || sender;
864
- }
853
+ msg.message.extendedTextMessage.contextInfo.participant = (found === null || found === void 0 ? void 0 : found.jid) || (0, WABinary_1.lidToJid)(sender); }
865
854
  }
866
855
  if (!(0, WABinary_1.isJidGroup)(msg.key.remoteJid) && (0, WABinary_1.isLidUser)(msg.key.remoteJid)) {
867
856
  msg.key.remoteJid = node.attrs.sender_pn || node.attrs.peer_recipient_pn;
@@ -937,13 +926,15 @@ const makeMessagesRecvSocket = (config) => {
937
926
  }],
938
927
  peerDataOperationRequestType: WAProto_1.proto.Message.PeerDataOperationRequestType.PLACEHOLDER_MESSAGE_RESEND
939
928
  };
940
- setTimeout(() => {
941
- if (placeholderResendCache.get(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id)) {
942
- logger.debug({ messageKey }, 'PDO message without response after 15 seconds. Phone possibly offline');
943
- placeholderResendCache.del(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id);
944
- }
945
- }, 15000);
946
- return sendPeerDataOperationMessage(pdoMessage);
929
+ const result = await Promise.race([
930
+ sendPeerDataOperationMessage(pdoMessage),
931
+ (0, Utils_1.delay)(15000).then(() => 'timeout')
932
+ ]);
933
+ if (result === 'timeout') {
934
+ logger.debug({ messageKey }, 'PDO message without response after 15 seconds. Phone possibly offline');
935
+ placeholderResendCache.del(messageKey === null || messageKey === void 0 ? void 0 : messageKey.id);
936
+ }
937
+ return result;
947
938
  };
948
939
  const handleCall = async (node) => {
949
940
  const { attrs } = node;
@@ -979,7 +970,7 @@ const makeMessagesRecvSocket = (config) => {
979
970
  await sendMessageAck(node);
980
971
  };
981
972
  const handleBadAck = async ({ attrs }) => {
982
- const key = { remoteJid: attrs.from, fromMe: true, id: attrs.id, 'server_id': attrs === null || attrs === void 0 ? void 0 : attrs.server_id };
973
+ const key = { remoteJid: resolveJid(attrs.from), fromMe: true, id: attrs.id, 'server_id': attrs === null || attrs === void 0 ? void 0 : attrs.server_id };
983
974
  // current hypothesis is that if pash is sent in the ack
984
975
  // it means -- the message hasn't reached all devices yet
985
976
  // we'll retry sending the message here
@@ -2,6 +2,6 @@ export declare const makeMutex: () => {
2
2
  mutex<T>(code: () => Promise<T> | T): Promise<T>;
3
3
  };
4
4
  export type Mutex = ReturnType<typeof makeMutex>;
5
- export declare const makeKeyedMutex: () => {
5
+ export declare const makeKeyedMutex: (maxQueueSize?: number) => {
6
6
  mutex<T>(key: string, task: () => Promise<T> | T): Promise<T>;
7
7
  };
@@ -29,12 +29,12 @@ const makeMutex = () => {
29
29
  };
30
30
  };
31
31
  exports.makeMutex = makeMutex;
32
- const makeKeyedMutex = () => {
32
+ const makeKeyedMutex = (maxQueueSize = 1000) => {
33
33
  const map = {};
34
34
  return {
35
35
  mutex(key, task) {
36
36
  if (!map[key]) {
37
- map[key] = (0, exports.makeMutex)();
37
+ map[key] = (0, exports.makeMutex)(maxQueueSize);
38
38
  }
39
39
  return map[key].mutex(task);
40
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realvare/based",
3
- "version": "2.7.5",
3
+ "version": "2.7.52",
4
4
  "description": "whatsapp api by sam",
5
5
  "keywords": [
6
6
  "baileys",
@@ -10,7 +10,8 @@
10
10
  "whatsapp-bot",
11
11
  "automation",
12
12
  "multi-device",
13
- "based"
13
+ "based",
14
+ "varebot"
14
15
  ],
15
16
  "homepage": "git+https://github.com/realvare/based.git",
16
17
  "repository": {
@@ -72,7 +73,7 @@
72
73
  "jest": "^29.7.0",
73
74
  "jimp": "^1.6.0",
74
75
  "json": "^11.0.0",
75
- "link-preview-js": "^3.0.0",
76
+ "link-preview-js": "^4.0.0",
76
77
  "open": "^10.1.0",
77
78
  "qrcode-terminal": "^0.12.0",
78
79
  "release-it": "^16.1.0",
@@ -84,7 +85,7 @@
84
85
  },
85
86
  "peerDependencies": {
86
87
  "audio-decode": "^2.1.3",
87
- "link-preview-js": "^3.0.0",
88
+ "link-preview-js": "^4.0.0",
88
89
  "qrcode-terminal": "^0.12.0",
89
90
  "sharp": "^0.33.0"
90
91
  },