@violetix/baileys 0.5.4 → 0.5.6

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.
@@ -953,7 +953,35 @@ export const makeChatsSocket = (config) => {
953
953
  const executeInitQueries = async () => {
954
954
  await Promise.all([fetchProps(), fetchBlocklist(), fetchPrivacySettings()]);
955
955
  };
956
+ /**
957
+ * WhatsApp redelivers a message whose ack was lost — the ack goes out after the stanza is
958
+ * processed, so a transport drop in between leaves the server believing nobody took it. The
959
+ * redelivery is indistinguishable from a new message at the stanza level, so the same key.id
960
+ * reached the host twice and every plugin answered twice ("double response" after a reconnect).
961
+ *
962
+ * Per socket, not process-wide: two bots in the same group must each process the group message.
963
+ * Bounded because ids are never reused but this runs for the life of a long-lived socket.
964
+ */
965
+ const seenMessageIds = new Set();
966
+ const SEEN_MESSAGE_LIMIT = 1000;
967
+ const isRepeatMessage = id => {
968
+ if (!id) {
969
+ return false;
970
+ }
971
+ if (seenMessageIds.has(id)) {
972
+ return true;
973
+ }
974
+ seenMessageIds.add(id);
975
+ if (seenMessageIds.size > SEEN_MESSAGE_LIMIT) {
976
+ seenMessageIds.delete(seenMessageIds.values().next().value);
977
+ }
978
+ return false;
979
+ };
956
980
  const upsertMessage = ev.createBufferedFunction(async (msg, type) => {
981
+ if (isRepeatMessage(msg.key?.id)) {
982
+ logger.debug({ msgId: msg.key?.id, type }, 'dropped redelivered message');
983
+ return;
984
+ }
957
985
  ev.emit('messages.upsert', { messages: [msg], type });
958
986
  if (!!msg.pushName) {
959
987
  let jid = msg.key.fromMe ? authState.creds.me?.id : msg.key.participant || msg.key.remoteJid;
@@ -392,7 +392,10 @@ export const makeSocket = (config) => {
392
392
  * listener instead of being processed. Keeping the identity we already know turns that into a
393
393
  * correct answer: a socket that is receiving stanzas is logged in as this user.
394
394
  */
395
- let lastMe = creds.me;
395
+ // A copy, and only when it carries an id: `creds.me` is replaced wholesale by some updates (a
396
+ // `CB:success` or a pushName change can arrive with a lid-only object), and holding the same
397
+ // reference would follow that replacement into the fallback.
398
+ let lastMe = creds.me?.id ? { ...creds.me } : undefined;
396
399
  let userMissingLogged = false;
397
400
  // add transaction capability
398
401
  const keys = addTransactionCapability(authState.keys, logger, transactionOpts);
@@ -1332,8 +1335,8 @@ export const makeSocket = (config) => {
1332
1335
  });
1333
1336
  }
1334
1337
  Object.assign(creds, update);
1335
- if (creds.me) {
1336
- lastMe = creds.me;
1338
+ if (creds.me?.id) {
1339
+ lastMe = { ...creds.me };
1337
1340
  }
1338
1341
  });
1339
1342
  const updateServerTimeOffset = ({ attrs }) => {
@@ -1414,7 +1417,9 @@ export const makeSocket = (config) => {
1414
1417
  authState: { creds, keys },
1415
1418
  signalRepository,
1416
1419
  get user() {
1417
- const me = authState.creds.me ?? lastMe;
1420
+ // `.id`, not just presence: a lid-only `me` is truthy and would be handed to hosts that
1421
+ // read `.id` unguarded, which is the crash this fallback exists to prevent.
1422
+ const me = authState.creds.me?.id ? authState.creds.me : lastMe;
1418
1423
  if (!me && !userMissingLogged) {
1419
1424
  userMissingLogged = true;
1420
1425
  logger.error(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@violetix/baileys",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Enhanced Baileys v7 with fixes for newsletter media uploads, plus support for interactive messages, albums, and additional message types.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",