@prototypevip/baileys 0.1.1 → 0.1.2

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.
@@ -466,6 +466,7 @@ export const makeMessagesSocket = (config) => {
466
466
  content: undefined,
467
467
  },
468
468
  ],
469
+ skipStoreEmit: true,
469
470
  },
470
471
  );
471
472
  };
@@ -540,6 +541,7 @@ export const makeMessagesSocket = (config) => {
540
541
  const msgId = await relayMessage(meJid, message, {
541
542
  additionalAttributes: { category: "peer", push_priority: "high_force" },
542
543
  additionalNodes: [{ tag: "meta", attrs: { appdata: "default" } }],
544
+ skipStoreEmit: true,
543
545
  });
544
546
  return msgId;
545
547
  };
@@ -647,6 +649,7 @@ export const makeMessagesSocket = (config) => {
647
649
  useCachedGroupMetadata,
648
650
  addBizAttributes,
649
651
  statusJidList,
652
+ skipStoreEmit,
650
653
  },
651
654
  ) => {
652
655
  // --- PN-First API Resolver: Resolve Mentions ---
@@ -1297,6 +1300,34 @@ export const makeMessagesSocket = (config) => {
1297
1300
  messageRetryManager.addRecentMessage(destinationJid, msgId, message);
1298
1301
  }
1299
1302
  }, meId);
1303
+
1304
+ // Ensure ANY call to relayMessage -- not just ones routed through
1305
+ // sendMessage's own wrapper -- respects emitOwnEvents and reaches
1306
+ // the store via messages.upsert. Callers that already build and
1307
+ // upsert a richer fullMsg themselves (sendMessage's internal call
1308
+ // sites) pass skipStoreEmit: true to avoid a duplicate upsert.
1309
+ if (config.emitOwnEvents && !skipStoreEmit && msgId && message) {
1310
+ const relayedKey = {
1311
+ remoteJid: destinationJid,
1312
+ fromMe: true,
1313
+ id: msgId,
1314
+ };
1315
+ if (isGroup) {
1316
+ relayedKey.participant = jidNormalizedUser(meId);
1317
+ } else if (participant?.jid) {
1318
+ relayedKey.participant = jidNormalizedUser(participant.jid);
1319
+ }
1320
+ const relayedMsg = {
1321
+ key: relayedKey,
1322
+ message: message,
1323
+ messageTimestamp: unixTimestampSeconds(),
1324
+ status: 1,
1325
+ };
1326
+ process.nextTick(async () => {
1327
+ await messageMutex.mutex(() => upsertMessage(relayedMsg, "append"));
1328
+ });
1329
+ }
1330
+
1300
1331
  return msgId;
1301
1332
  };
1302
1333
  const getMessageType = (message) => {
@@ -1529,6 +1560,14 @@ export const makeMessagesSocket = (config) => {
1529
1560
 
1530
1561
  const userJid = authState.creds.me.id;
1531
1562
 
1563
+ const emitUpsert = (msg, type = "append") => {
1564
+ if (config.emitOwnEvents && msg && msg.key) {
1565
+ process.nextTick(async () => {
1566
+ await messageMutex.mutex(() => upsertMessage(msg, type));
1567
+ });
1568
+ }
1569
+ };
1570
+
1532
1571
  if (Array.isArray(jid)) {
1533
1572
  const { delayMs = 1500 } = options;
1534
1573
  const allUsers = new Set();
@@ -1565,6 +1604,7 @@ export const makeMessagesSocket = (config) => {
1565
1604
  await relayMessage("status@broadcast", fullMsg.message, {
1566
1605
  messageId: fullMsg.key.id,
1567
1606
  statusJidList: Array.from(allUsers),
1607
+ skipStoreEmit: true,
1568
1608
  additionalNodes: [
1569
1609
  {
1570
1610
  tag: "meta",
@@ -1604,6 +1644,8 @@ export const makeMessagesSocket = (config) => {
1604
1644
  { userJid: userJid },
1605
1645
  );
1606
1646
  await relayMessage(id, mentionMsg.message, {
1647
+ messageId: mentionMsg.key.id,
1648
+ skipStoreEmit: true,
1607
1649
  additionalNodes: [
1608
1650
  {
1609
1651
  tag: "meta",
@@ -1634,29 +1676,39 @@ export const makeMessagesSocket = (config) => {
1634
1676
  : disappearingMessagesInChat;
1635
1677
  await groupToggleEphemeral(jid, value);
1636
1678
  } else {
1637
- const interactiveHandler = new InteractiveMessageHandler(Utils_1, waUploadToServer, relayMessage);
1679
+ const interactiveHandler = new InteractiveMessageHandler(Utils_1, waUploadToServer, relayMessage, emitUpsert);
1638
1680
  const messageType = interactiveHandler.detectType(content);
1639
1681
  if (messageType) {
1640
1682
  switch (messageType) {
1641
1683
  case 'PAYMENT': {
1642
1684
  const paymentContent = await interactiveHandler.handlePayment(content, options.quoted);
1643
- return await relayMessage(jid, paymentContent, {
1644
- messageId: generateMessageID()
1685
+ const paymentMsg = generateWAMessageFromContent(jid, paymentContent, { userJid: userJid });
1686
+ await relayMessage(jid, paymentMsg.message, {
1687
+ messageId: paymentMsg.key.id,
1688
+ skipStoreEmit: true
1645
1689
  });
1690
+ emitUpsert(paymentMsg);
1691
+ return paymentMsg;
1646
1692
  }
1647
1693
  case 'PRODUCT': {
1648
1694
  const productContent = await interactiveHandler.handleProduct(content, jid, options.quoted);
1649
1695
  const productMsg = await generateWAMessageFromContent(jid, productContent, { quoted: options.quoted });
1650
- return await relayMessage(jid, productMsg.message, {
1651
- messageId: productMsg.key.id
1696
+ await relayMessage(jid, productMsg.message, {
1697
+ messageId: productMsg.key.id,
1698
+ skipStoreEmit: true
1652
1699
  });
1700
+ emitUpsert(productMsg);
1701
+ return productMsg;
1653
1702
  }
1654
1703
  case 'INTERACTIVE': {
1655
1704
  const interactiveContent = await interactiveHandler.handleInteractive(content, jid, options.quoted);
1656
1705
  const interactiveMsg = await generateWAMessageFromContent(jid, interactiveContent, { quoted: options.quoted });
1657
- return await relayMessage(jid, interactiveMsg.message, {
1658
- messageId: interactiveMsg.key.id
1706
+ await relayMessage(jid, interactiveMsg.message, {
1707
+ messageId: interactiveMsg.key.id,
1708
+ skipStoreEmit: true
1659
1709
  });
1710
+ emitUpsert(interactiveMsg);
1711
+ return interactiveMsg;
1660
1712
  }
1661
1713
  case 'ALBUM':
1662
1714
  return await interactiveHandler.handleAlbum(content, jid, options.quoted);
@@ -1784,6 +1836,7 @@ export const makeMessagesSocket = (config) => {
1784
1836
  statusJidList: options.statusJidList,
1785
1837
  additionalAttributes: additionalAttributes,
1786
1838
  additionalNodes: additionalNodes,
1839
+ skipStoreEmit: true,
1787
1840
  });
1788
1841
  if (config.emitOwnEvents) {
1789
1842
  process.nextTick(async () => {
@@ -1825,6 +1878,7 @@ export const makeMessagesSocket = (config) => {
1825
1878
  statusJidList: options.statusJidList,
1826
1879
  additionalAttributes: additionalAttributes,
1827
1880
  additionalNodes: additionalNodes,
1881
+ skipStoreEmit: true,
1828
1882
  });
1829
1883
  if (config.emitOwnEvents) {
1830
1884
  process.nextTick(async () => {
@@ -736,30 +736,42 @@ export const makePrototypeStore = (config = {}) => {
736
736
 
737
737
  listen("messages.upsert", async ({ messages }) => {
738
738
  for (let msg of messages) {
739
- const rawChatId = msg?.key?.remoteJid;
740
- if (!rawChatId) continue;
741
- const chatId = await resolveJIDAsync(
742
- rawChatId,
743
- store.signalRepository?.lidMapping,
744
- );
745
- if (!chatId) continue;
746
- const chat = store.chats[chatId];
747
- if (!chat) continue;
748
- if (!chat.messages || typeof chat.messages.upsert !== "function") {
749
- chat.messages = makeMessagesStore();
750
- }
751
- chat.messages.upsert(cloneM(msg));
752
-
753
- if (db) {
754
- const msgKey = getMessageKey(
755
- msg,
756
- {},
739
+ try {
740
+ const rawChatId = msg?.key?.remoteJid;
741
+ if (!rawChatId) continue;
742
+ const chatId = await resolveJIDAsync(
743
+ rawChatId,
757
744
  store.signalRepository?.lidMapping,
758
745
  );
759
- pendingUpdates.set(`m:${chatId}:${msgKey}`, {
760
- type: "message",
761
- data: cloneM(msg),
762
- });
746
+ if (!chatId) continue;
747
+ const chat = store.chats[chatId];
748
+ if (!chat) continue;
749
+ if (!chat.messages || typeof chat.messages.upsert !== "function") {
750
+ chat.messages = makeMessagesStore();
751
+ }
752
+ chat.messages.upsert(cloneM(msg));
753
+
754
+ if (db) {
755
+ const msgKey = getMessageKey(
756
+ msg,
757
+ {},
758
+ store.signalRepository?.lidMapping,
759
+ );
760
+ pendingUpdates.set(`m:${chatId}:${msgKey}`, {
761
+ type: "message",
762
+ data: cloneM(msg),
763
+ });
764
+ }
765
+ } catch (err) {
766
+ // مهم: من غير try/catch هنا، اي رساله واحده تفشل (JID غريب،
767
+ // مهله lidMapping، اي حاجه) كانت بتوقف اللوب وبتفقد كل الرسايل
768
+ // الباقيه في نفس الـ upsert batch من غير أي خطأ ظاهر.
769
+ console.error(
770
+ "PrototypeStore messages.upsert item error (id:",
771
+ msg?.key?.id,
772
+ "):",
773
+ err?.message || err,
774
+ );
763
775
  }
764
776
  }
765
777
  });