@alannxd/baileys 6.0.6 → 6.0.9

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.
Files changed (220) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +341 -286
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +6902 -0
  5. package/WAProto/fix-imports.js +85 -0
  6. package/WAProto/index.d.ts +79257 -0
  7. package/WAProto/index.js +205861 -60565
  8. package/engine-requirements.js +1 -1
  9. package/lib/Defaults/index.js +119 -136
  10. package/lib/Signal/Group/ciphertext-message.js +2 -5
  11. package/lib/Signal/Group/group-session-builder.js +7 -41
  12. package/lib/Signal/Group/group_cipher.js +37 -51
  13. package/lib/Signal/Group/index.js +12 -57
  14. package/lib/Signal/Group/keyhelper.js +7 -44
  15. package/lib/Signal/Group/sender-chain-key.js +7 -15
  16. package/lib/Signal/Group/sender-key-distribution-message.js +8 -11
  17. package/lib/Signal/Group/sender-key-message.js +9 -12
  18. package/lib/Signal/Group/sender-key-name.js +2 -5
  19. package/lib/Signal/Group/sender-key-record.js +9 -21
  20. package/lib/Signal/Group/sender-key-state.js +27 -42
  21. package/lib/Signal/Group/sender-message-key.js +4 -7
  22. package/lib/Signal/libsignal.js +347 -90
  23. package/lib/Signal/lid-mapping.js +277 -0
  24. package/lib/Socket/Client/index.js +3 -19
  25. package/lib/Socket/Client/types.js +11 -0
  26. package/lib/Socket/Client/websocket.js +54 -0
  27. package/lib/Socket/business.js +162 -43
  28. package/lib/Socket/chats.js +627 -427
  29. package/lib/Socket/communities.js +90 -80
  30. package/lib/Socket/groups.js +154 -161
  31. package/lib/Socket/index.js +11 -10
  32. package/lib/Socket/luxu.js +315 -469
  33. package/lib/Socket/messages-recv.js +1421 -615
  34. package/lib/Socket/messages-send.js +1150 -799
  35. package/lib/Socket/mex.js +42 -0
  36. package/lib/Socket/newsletter.js +152 -204
  37. package/lib/Socket/socket.js +544 -313
  38. package/lib/Store/index.js +10 -10
  39. package/lib/Store/keyed-db.js +108 -0
  40. package/lib/Store/make-cache-manager-store.js +43 -41
  41. package/lib/Store/make-in-memory-store.js +112 -341
  42. package/lib/Store/make-ordered-dictionary.js +14 -20
  43. package/lib/Store/object-repository.js +11 -6
  44. package/lib/Types/Auth.js +2 -2
  45. package/lib/Types/Bussines.js +2 -0
  46. package/lib/Types/Call.js +2 -2
  47. package/lib/Types/Chat.js +8 -4
  48. package/lib/Types/Contact.js +2 -2
  49. package/lib/Types/Events.js +2 -2
  50. package/lib/Types/GroupMetadata.js +2 -2
  51. package/lib/Types/Label.js +3 -5
  52. package/lib/Types/LabelAssociation.js +3 -5
  53. package/lib/Types/Message.js +11 -9
  54. package/lib/Types/Mex.js +37 -0
  55. package/lib/Types/Product.js +2 -2
  56. package/lib/Types/Signal.js +2 -2
  57. package/lib/Types/Socket.js +3 -2
  58. package/lib/Types/State.js +56 -2
  59. package/lib/Types/USync.js +2 -2
  60. package/lib/Types/index.js +15 -31
  61. package/lib/Utils/auth-utils.js +239 -143
  62. package/lib/Utils/browser-utils.js +48 -0
  63. package/lib/Utils/business.js +66 -69
  64. package/lib/Utils/chat-utils.js +396 -253
  65. package/lib/Utils/companion-reg-client-utils.js +35 -0
  66. package/lib/Utils/crypto.js +57 -90
  67. package/lib/Utils/decode-wa-message.js +236 -84
  68. package/lib/Utils/event-buffer.js +185 -77
  69. package/lib/Utils/generics.js +189 -209
  70. package/lib/Utils/history.js +93 -55
  71. package/lib/Utils/identity-change-handler.js +50 -0
  72. package/lib/Utils/index.js +23 -33
  73. package/lib/Utils/link-preview.js +16 -24
  74. package/lib/Utils/logger.js +3 -7
  75. package/lib/Utils/lt-hash.js +3 -46
  76. package/lib/Utils/make-mutex.js +24 -34
  77. package/lib/Utils/message-composer.js +273 -0
  78. package/lib/Utils/message-retry-manager.js +265 -0
  79. package/lib/Utils/messages-media.js +451 -482
  80. package/lib/Utils/messages.js +795 -369
  81. package/lib/Utils/noise-handler.js +145 -99
  82. package/lib/Utils/offline-node-processor.js +40 -0
  83. package/lib/Utils/pre-key-manager.js +106 -0
  84. package/lib/Utils/process-message.js +459 -150
  85. package/lib/Utils/reporting-utils.js +258 -0
  86. package/lib/Utils/signal.js +120 -72
  87. package/lib/Utils/stanza-ack.js +38 -0
  88. package/lib/Utils/sync-action-utils.js +49 -0
  89. package/lib/Utils/tc-token-utils.js +163 -0
  90. package/lib/Utils/use-multi-file-auth-state.js +29 -27
  91. package/lib/Utils/validate-connection.js +73 -99
  92. package/lib/WABinary/constants.js +1281 -20
  93. package/lib/WABinary/decode.js +52 -42
  94. package/lib/WABinary/encode.js +110 -155
  95. package/lib/WABinary/generic-utils.js +55 -49
  96. package/lib/WABinary/index.js +6 -21
  97. package/lib/WABinary/jid-utils.js +76 -40
  98. package/lib/WABinary/types.js +2 -2
  99. package/lib/WAM/BinaryInfo.js +2 -5
  100. package/lib/WAM/constants.js +19071 -11568
  101. package/lib/WAM/encode.js +17 -22
  102. package/lib/WAM/index.js +4 -19
  103. package/lib/WAUSync/Protocols/USyncContactProtocol.js +33 -13
  104. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
  105. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
  106. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
  107. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  108. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
  109. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +13 -8
  110. package/lib/WAUSync/Protocols/index.js +6 -20
  111. package/lib/WAUSync/USyncQuery.js +44 -35
  112. package/lib/WAUSync/USyncUser.js +10 -5
  113. package/lib/WAUSync/index.js +4 -19
  114. package/lib/index.js +13 -36
  115. package/package.json +85 -51
  116. package/WAProto/fix-import.js +0 -29
  117. package/lib/Defaults/baileys-version.json +0 -3
  118. package/lib/Defaults/index.d.ts +0 -53
  119. package/lib/Defaults/phonenumber-mcc.json +0 -223
  120. package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
  121. package/lib/Signal/Group/group-session-builder.d.ts +0 -14
  122. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  123. package/lib/Signal/Group/index.d.ts +0 -11
  124. package/lib/Signal/Group/keyhelper.d.ts +0 -10
  125. package/lib/Signal/Group/queue-job.d.ts +0 -1
  126. package/lib/Signal/Group/queue-job.js +0 -57
  127. package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
  128. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
  129. package/lib/Signal/Group/sender-key-message.d.ts +0 -18
  130. package/lib/Signal/Group/sender-key-name.d.ts +0 -17
  131. package/lib/Signal/Group/sender-key-record.d.ts +0 -30
  132. package/lib/Signal/Group/sender-key-state.d.ts +0 -38
  133. package/lib/Signal/Group/sender-message-key.d.ts +0 -11
  134. package/lib/Signal/libsignal.d.ts +0 -3
  135. package/lib/Socket/Client/abstract-socket-client.d.ts +0 -17
  136. package/lib/Socket/Client/abstract-socket-client.js +0 -13
  137. package/lib/Socket/Client/index.d.ts +0 -3
  138. package/lib/Socket/Client/mobile-socket-client.d.ts +0 -13
  139. package/lib/Socket/Client/mobile-socket-client.js +0 -65
  140. package/lib/Socket/Client/web-socket-client.d.ts +0 -12
  141. package/lib/Socket/Client/web-socket-client.js +0 -62
  142. package/lib/Socket/business.d.ts +0 -171
  143. package/lib/Socket/chats.d.ts +0 -267
  144. package/lib/Socket/communities.d.ts +0 -180
  145. package/lib/Socket/groups.d.ts +0 -115
  146. package/lib/Socket/index.d.ts +0 -173
  147. package/lib/Socket/luxu.d.ts +0 -266
  148. package/lib/Socket/messages-recv.d.ts +0 -161
  149. package/lib/Socket/messages-send.d.ts +0 -183
  150. package/lib/Socket/newsletter.d.ts +0 -134
  151. package/lib/Socket/registration.d.ts +0 -267
  152. package/lib/Socket/registration.js +0 -166
  153. package/lib/Socket/socket.d.ts +0 -44
  154. package/lib/Socket/usync.d.ts +0 -36
  155. package/lib/Socket/usync.js +0 -70
  156. package/lib/Store/index.d.ts +0 -3
  157. package/lib/Store/make-cache-manager-store.d.ts +0 -13
  158. package/lib/Store/make-in-memory-store.d.ts +0 -118
  159. package/lib/Store/make-ordered-dictionary.d.ts +0 -13
  160. package/lib/Store/object-repository.d.ts +0 -10
  161. package/lib/Types/Auth.d.ts +0 -110
  162. package/lib/Types/Call.d.ts +0 -13
  163. package/lib/Types/Chat.d.ts +0 -102
  164. package/lib/Types/Contact.d.ts +0 -19
  165. package/lib/Types/Events.d.ts +0 -157
  166. package/lib/Types/GroupMetadata.d.ts +0 -55
  167. package/lib/Types/Label.d.ts +0 -35
  168. package/lib/Types/LabelAssociation.d.ts +0 -29
  169. package/lib/Types/Message.d.ts +0 -273
  170. package/lib/Types/Newsletter.d.ts +0 -103
  171. package/lib/Types/Newsletter.js +0 -38
  172. package/lib/Types/Product.d.ts +0 -78
  173. package/lib/Types/Signal.d.ts +0 -57
  174. package/lib/Types/Socket.d.ts +0 -111
  175. package/lib/Types/State.d.ts +0 -27
  176. package/lib/Types/USync.d.ts +0 -25
  177. package/lib/Types/index.d.ts +0 -57
  178. package/lib/Utils/auth-utils.d.ts +0 -18
  179. package/lib/Utils/baileys-event-stream.d.ts +0 -16
  180. package/lib/Utils/baileys-event-stream.js +0 -63
  181. package/lib/Utils/business.d.ts +0 -22
  182. package/lib/Utils/chat-utils.d.ts +0 -71
  183. package/lib/Utils/crypto.d.ts +0 -41
  184. package/lib/Utils/decode-wa-message.d.ts +0 -19
  185. package/lib/Utils/event-buffer.d.ts +0 -35
  186. package/lib/Utils/generics.d.ts +0 -92
  187. package/lib/Utils/history.d.ts +0 -15
  188. package/lib/Utils/index.d.ts +0 -17
  189. package/lib/Utils/link-preview.d.ts +0 -21
  190. package/lib/Utils/logger.d.ts +0 -4
  191. package/lib/Utils/lt-hash.d.ts +0 -12
  192. package/lib/Utils/make-mutex.d.ts +0 -7
  193. package/lib/Utils/messages-media.d.ts +0 -116
  194. package/lib/Utils/messages.d.ts +0 -77
  195. package/lib/Utils/noise-handler.d.ts +0 -21
  196. package/lib/Utils/process-message.d.ts +0 -41
  197. package/lib/Utils/signal.d.ts +0 -32
  198. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  199. package/lib/Utils/validate-connection.d.ts +0 -11
  200. package/lib/WABinary/constants.d.ts +0 -30
  201. package/lib/WABinary/decode.d.ts +0 -7
  202. package/lib/WABinary/encode.d.ts +0 -3
  203. package/lib/WABinary/generic-utils.d.ts +0 -17
  204. package/lib/WABinary/index.d.ts +0 -5
  205. package/lib/WABinary/jid-utils.d.ts +0 -31
  206. package/lib/WABinary/types.d.ts +0 -18
  207. package/lib/WAM/BinaryInfo.d.ts +0 -17
  208. package/lib/WAM/constants.d.ts +0 -38
  209. package/lib/WAM/encode.d.ts +0 -3
  210. package/lib/WAM/index.d.ts +0 -3
  211. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -9
  212. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -22
  213. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -12
  214. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -12
  215. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -25
  216. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -8
  217. package/lib/WAUSync/Protocols/index.d.ts +0 -4
  218. package/lib/WAUSync/USyncQuery.d.ts +0 -28
  219. package/lib/WAUSync/USyncUser.d.ts +0 -12
  220. package/lib/index.d.ts +0 -12
@@ -1,14 +1,8 @@
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.makeEventBuffer = void 0;
7
- const events_1 = __importDefault(require("events"));
8
- const Types_1 = require("../Types");
9
- const generics_1 = require("./generics");
10
- const messages_1 = require("./messages");
11
- const process_message_1 = require("./process-message");
1
+ import EventEmitter from 'events';
2
+ import { WAMessageStatus } from '../Types/index.js';
3
+ import { trimUndefined } from './generics.js';
4
+ import { updateMessageWithReaction, updateMessageWithReceipt } from './messages.js';
5
+ import { isRealMessage, shouldIncrementChatUnread } from './process-message.js';
12
6
  const BUFFERABLE_EVENT = [
13
7
  'messaging-history.set',
14
8
  'chats.upsert',
@@ -21,19 +15,23 @@ const BUFFERABLE_EVENT = [
21
15
  'messages.delete',
22
16
  'messages.reaction',
23
17
  'message-receipt.update',
24
- 'groups.update',
18
+ 'groups.update'
25
19
  ];
26
20
  const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
27
21
  /**
28
22
  * The event buffer logically consolidates different events into a single event
29
23
  * making the data processing more efficient.
30
- * @param ev the baileys event emitter
31
24
  */
32
- const makeEventBuffer = (logger) => {
33
- const ev = new events_1.default();
25
+ export const makeEventBuffer = (logger) => {
26
+ const ev = new EventEmitter();
34
27
  const historyCache = new Set();
35
28
  let data = makeBufferData();
36
- let buffersInProgress = 0;
29
+ let isBuffering = false;
30
+ let bufferTimeout = null;
31
+ let flushPendingTimeout = null; // Add a specific timer for the debounced flush to prevent leak
32
+ let bufferCount = 0;
33
+ const MAX_HISTORY_CACHE_SIZE = 10000; // Limit the history cache size to prevent memory bloat
34
+ const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
37
35
  // take the generic event and fire it as a baileys event
38
36
  ev.on('event', (map) => {
39
37
  for (const event in map) {
@@ -41,25 +39,46 @@ const makeEventBuffer = (logger) => {
41
39
  }
42
40
  });
43
41
  function buffer() {
44
- buffersInProgress += 1;
42
+ if (!isBuffering) {
43
+ logger.debug('Event buffer activated');
44
+ isBuffering = true;
45
+ bufferCount = 0;
46
+ if (bufferTimeout) {
47
+ clearTimeout(bufferTimeout);
48
+ }
49
+ bufferTimeout = setTimeout(() => {
50
+ if (isBuffering) {
51
+ logger.warn('Buffer timeout reached, auto-flushing');
52
+ flush();
53
+ }
54
+ }, BUFFER_TIMEOUT_MS);
55
+ }
56
+ // Always increment count when requested
57
+ bufferCount++;
45
58
  }
46
- function flush(force = false) {
47
- // no buffer going on
48
- if (!buffersInProgress) {
59
+ function flush() {
60
+ if (!isBuffering) {
49
61
  return false;
50
62
  }
51
- if (!force) {
52
- // reduce the number of buffers in progress
53
- buffersInProgress -= 1;
54
- // if there are still some buffers going on
55
- // then we don't flush now
56
- if (buffersInProgress) {
57
- return false;
58
- }
63
+ logger.debug({ bufferCount }, 'Flushing event buffer');
64
+ isBuffering = false;
65
+ bufferCount = 0;
66
+ // Clear timeout
67
+ if (bufferTimeout) {
68
+ clearTimeout(bufferTimeout);
69
+ bufferTimeout = null;
70
+ }
71
+ if (flushPendingTimeout) {
72
+ clearTimeout(flushPendingTimeout);
73
+ flushPendingTimeout = null;
74
+ }
75
+ // Clear history cache if it exceeds the max size
76
+ if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
77
+ logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
78
+ historyCache.clear();
59
79
  }
60
80
  const newData = makeBufferData();
61
81
  const chatUpdates = Object.values(data.chatUpdates);
62
- // gather the remaining conditional events so we re-queue them
63
82
  let conditionalChatUpdatesLeft = 0;
64
83
  for (const update of chatUpdates) {
65
84
  if (update.conditional) {
@@ -78,8 +97,8 @@ const makeEventBuffer = (logger) => {
78
97
  }
79
98
  return {
80
99
  process(handler) {
81
- const listener = (map) => {
82
- handler(map);
100
+ const listener = async (map) => {
101
+ await handler(map);
83
102
  };
84
103
  ev.on('event', listener);
85
104
  return () => {
@@ -87,14 +106,35 @@ const makeEventBuffer = (logger) => {
87
106
  };
88
107
  },
89
108
  emit(event, evData) {
90
- if (buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
109
+ // Check if this is a messages.upsert with a different type than what's buffered
110
+ // If so, flush the buffered messages first to avoid type overshadowing
111
+ if (event === 'messages.upsert') {
112
+ const { type } = evData;
113
+ const existingUpserts = Object.values(data.messageUpserts);
114
+ if (existingUpserts.length > 0) {
115
+ const bufferedType = existingUpserts[0].type;
116
+ if (bufferedType !== type) {
117
+ logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
118
+ // Emit the buffered messages with their correct type
119
+ ev.emit('event', {
120
+ 'messages.upsert': {
121
+ messages: existingUpserts.map(m => m.message),
122
+ type: bufferedType
123
+ }
124
+ });
125
+ // Clear the message upserts from the buffer
126
+ data.messageUpserts = {};
127
+ }
128
+ }
129
+ }
130
+ if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
91
131
  append(data, historyCache, event, evData, logger);
92
132
  return true;
93
133
  }
94
134
  return ev.emit('event', { [event]: evData });
95
135
  },
96
136
  isBuffering() {
97
- return buffersInProgress > 0;
137
+ return isBuffering;
98
138
  },
99
139
  buffer,
100
140
  flush,
@@ -103,19 +143,55 @@ const makeEventBuffer = (logger) => {
103
143
  buffer();
104
144
  try {
105
145
  const result = await work(...args);
146
+ // If this is the only buffer, flush after a small delay
147
+ if (bufferCount === 1) {
148
+ setTimeout(() => {
149
+ if (isBuffering && bufferCount === 1) {
150
+ flush();
151
+ }
152
+ }, 100); // Small delay to allow nested buffers
153
+ }
106
154
  return result;
107
155
  }
156
+ catch (error) {
157
+ throw error;
158
+ }
108
159
  finally {
109
- flush();
160
+ bufferCount = Math.max(0, bufferCount - 1);
161
+ if (bufferCount === 0) {
162
+ // Only schedule ONE timeout, not 10,000
163
+ if (!flushPendingTimeout) {
164
+ flushPendingTimeout = setTimeout(flush, 100);
165
+ }
166
+ }
110
167
  }
111
168
  };
112
169
  },
113
170
  on: (...args) => ev.on(...args),
114
171
  off: (...args) => ev.off(...args),
115
172
  removeAllListeners: (...args) => ev.removeAllListeners(...args),
173
+ destroy() {
174
+ // Clear buffer timeout
175
+ if (bufferTimeout) {
176
+ clearTimeout(bufferTimeout);
177
+ bufferTimeout = null;
178
+ }
179
+ if (flushPendingTimeout) {
180
+ clearTimeout(flushPendingTimeout);
181
+ flushPendingTimeout = null;
182
+ }
183
+ // Clear history cache
184
+ historyCache.clear();
185
+ // Reset buffer data
186
+ data = makeBufferData();
187
+ isBuffering = false;
188
+ bufferCount = 0;
189
+ // Remove all listeners
190
+ ev.removeAllListeners();
191
+ logger.debug('Event buffer destroyed');
192
+ }
116
193
  };
117
194
  };
118
- exports.makeEventBuffer = makeEventBuffer;
119
195
  const makeBufferData = () => {
120
196
  return {
121
197
  historySets: {
@@ -138,25 +214,27 @@ const makeBufferData = () => {
138
214
  groupUpdates: {}
139
215
  };
140
216
  };
141
- function append(data, historyCache, event, eventData, logger) {
142
- var _a, _b, _c;
217
+ function append(data, historyCache, event,
218
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
219
+ eventData, logger) {
143
220
  switch (event) {
144
221
  case 'messaging-history.set':
145
222
  for (const chat of eventData.chats) {
146
- const existingChat = data.historySets.chats[chat.id];
223
+ const id = chat.id || '';
224
+ const existingChat = data.historySets.chats[id];
147
225
  if (existingChat) {
148
226
  existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
149
227
  }
150
- if (!existingChat && !historyCache.has(chat.id)) {
151
- data.historySets.chats[chat.id] = chat;
152
- historyCache.add(chat.id);
228
+ if (!existingChat && !historyCache.has(id)) {
229
+ data.historySets.chats[id] = chat;
230
+ historyCache.add(id);
153
231
  absorbingChatUpdate(chat);
154
232
  }
155
233
  }
156
234
  for (const contact of eventData.contacts) {
157
235
  const existingContact = data.historySets.contacts[contact.id];
158
236
  if (existingContact) {
159
- Object.assign(existingContact, (0, generics_1.trimUndefined)(contact));
237
+ Object.assign(existingContact, trimUndefined(contact));
160
238
  }
161
239
  else {
162
240
  const historyContactId = `c:${contact.id}`;
@@ -176,15 +254,45 @@ function append(data, historyCache, event, eventData, logger) {
176
254
  }
177
255
  }
178
256
  data.historySets.empty = false;
257
+ data.historySets.syncType = eventData.syncType;
258
+ if (eventData.pastParticipants?.length) {
259
+ const merged = new Map();
260
+ const sigOf = (p) => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`;
261
+ const ingest = (entry) => {
262
+ const key = entry.groupJid ?? JSON.stringify(entry);
263
+ const existing = merged.get(key);
264
+ if (!existing) {
265
+ merged.set(key, { ...entry, pastParticipants: [...(entry.pastParticipants || [])] });
266
+ return;
267
+ }
268
+ const seen = new Set((existing.pastParticipants || []).map(sigOf));
269
+ for (const p of entry.pastParticipants || []) {
270
+ const sig = sigOf(p);
271
+ if (!seen.has(sig)) {
272
+ existing.pastParticipants.push(p);
273
+ seen.add(sig);
274
+ }
275
+ }
276
+ };
277
+ for (const entry of data.historySets.pastParticipants || [])
278
+ ingest(entry);
279
+ for (const entry of eventData.pastParticipants)
280
+ ingest(entry);
281
+ data.historySets.pastParticipants = [...merged.values()];
282
+ }
283
+ data.historySets.progress = eventData.progress;
284
+ data.historySets.chunkOrder = eventData.chunkOrder;
285
+ data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
179
286
  data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
180
287
  break;
181
288
  case 'chats.upsert':
182
289
  for (const chat of eventData) {
183
- let upsert = data.chatUpserts[chat.id];
184
- if (!upsert) {
185
- upsert = data.historySets[chat.id];
290
+ const id = chat.id || '';
291
+ let upsert = data.chatUpserts[id];
292
+ if (id && !upsert) {
293
+ upsert = data.historySets.chats[id];
186
294
  if (upsert) {
187
- logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set');
295
+ logger.debug({ chatId: id }, 'absorbed chat upsert in chat set');
188
296
  }
189
297
  }
190
298
  if (upsert) {
@@ -192,11 +300,11 @@ function append(data, historyCache, event, eventData, logger) {
192
300
  }
193
301
  else {
194
302
  upsert = chat;
195
- data.chatUpserts[chat.id] = upsert;
303
+ data.chatUpserts[id] = upsert;
196
304
  }
197
305
  absorbingChatUpdate(upsert);
198
- if (data.chatDeletes.has(chat.id)) {
199
- data.chatDeletes.delete(chat.id);
306
+ if (data.chatDeletes.has(id)) {
307
+ data.chatDeletes.delete(id);
200
308
  }
201
309
  }
202
310
  break;
@@ -256,14 +364,14 @@ function append(data, historyCache, event, eventData, logger) {
256
364
  }
257
365
  }
258
366
  if (upsert) {
259
- upsert = Object.assign(upsert, (0, generics_1.trimUndefined)(contact));
367
+ upsert = Object.assign(upsert, trimUndefined(contact));
260
368
  }
261
369
  else {
262
370
  upsert = contact;
263
371
  data.contactUpserts[contact.id] = upsert;
264
372
  }
265
373
  if (data.contactUpdates[contact.id]) {
266
- upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_1.trimUndefined)(contact));
374
+ upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
267
375
  delete data.contactUpdates[contact.id];
268
376
  }
269
377
  }
@@ -288,7 +396,7 @@ function append(data, historyCache, event, eventData, logger) {
288
396
  const { messages, type } = eventData;
289
397
  for (const message of messages) {
290
398
  const key = stringifyMessageKey(message.key);
291
- let existing = (_a = data.messageUpserts[key]) === null || _a === void 0 ? void 0 : _a.message;
399
+ let existing = data.messageUpserts[key]?.message;
292
400
  if (!existing) {
293
401
  existing = data.historySets.messages[key];
294
402
  if (existing) {
@@ -309,9 +417,7 @@ function append(data, historyCache, event, eventData, logger) {
309
417
  else {
310
418
  data.messageUpserts[key] = {
311
419
  message,
312
- type: type === 'notify' || ((_b = data.messageUpserts[key]) === null || _b === void 0 ? void 0 : _b.type) === 'notify'
313
- ? 'notify'
314
- : type
420
+ type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
315
421
  };
316
422
  }
317
423
  }
@@ -320,13 +426,13 @@ function append(data, historyCache, event, eventData, logger) {
320
426
  const msgUpdates = eventData;
321
427
  for (const { key, update } of msgUpdates) {
322
428
  const keyStr = stringifyMessageKey(key);
323
- const existing = data.historySets.messages[keyStr] || ((_c = data.messageUpserts[keyStr]) === null || _c === void 0 ? void 0 : _c.message);
429
+ const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
324
430
  if (existing) {
325
431
  Object.assign(existing, update);
326
432
  // if the message was received & read by us
327
433
  // the chat counter must have been incremented
328
434
  // so we need to decrement it
329
- if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
435
+ if (update.status === WAMessageStatus.READ && !key.fromMe) {
330
436
  decrementChatReadCounterIfMsgDidUnread(existing);
331
437
  }
332
438
  }
@@ -364,12 +470,11 @@ function append(data, historyCache, event, eventData, logger) {
364
470
  const keyStr = stringifyMessageKey(key);
365
471
  const existing = data.messageUpserts[keyStr];
366
472
  if (existing) {
367
- (0, messages_1.updateMessageWithReaction)(existing.message, reaction);
473
+ updateMessageWithReaction(existing.message, reaction);
368
474
  }
369
475
  else {
370
- data.messageReactions[keyStr] = data.messageReactions[keyStr]
371
- || { key, reactions: [] };
372
- (0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
476
+ data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
477
+ updateMessageWithReaction(data.messageReactions[keyStr], reaction);
373
478
  }
374
479
  }
375
480
  break;
@@ -379,12 +484,11 @@ function append(data, historyCache, event, eventData, logger) {
379
484
  const keyStr = stringifyMessageKey(key);
380
485
  const existing = data.messageUpserts[keyStr];
381
486
  if (existing) {
382
- (0, messages_1.updateMessageWithReceipt)(existing.message, receipt);
487
+ updateMessageWithReceipt(existing.message, receipt);
383
488
  }
384
489
  else {
385
- data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
386
- || { key, userReceipt: [] };
387
- (0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
490
+ data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
491
+ updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
388
492
  }
389
493
  }
390
494
  break;
@@ -402,7 +506,7 @@ function append(data, historyCache, event, eventData, logger) {
402
506
  throw new Error(`"${event}" cannot be buffered`);
403
507
  }
404
508
  function absorbingChatUpdate(existing) {
405
- const chatId = existing.id;
509
+ const chatId = existing.id || '';
406
510
  const update = data.chatUpdates[chatId];
407
511
  if (update) {
408
512
  const conditionMatches = update.conditional ? update.conditional(data) : true;
@@ -423,10 +527,10 @@ function append(data, historyCache, event, eventData, logger) {
423
527
  // if the message has already been marked read by us
424
528
  const chatId = message.key.remoteJid;
425
529
  const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
426
- if ((0, process_message_1.isRealMessage)(message, '')
427
- && (0, process_message_1.shouldIncrementChatUnread)(message)
428
- && typeof (chat === null || chat === void 0 ? void 0 : chat.unreadCount) === 'number'
429
- && chat.unreadCount > 0) {
530
+ if (isRealMessage(message) &&
531
+ shouldIncrementChatUnread(message) &&
532
+ typeof chat?.unreadCount === 'number' &&
533
+ chat.unreadCount > 0) {
430
534
  logger.debug({ chatId: chat.id }, 'decrementing chat counter');
431
535
  chat.unreadCount -= 1;
432
536
  if (chat.unreadCount === 0) {
@@ -442,7 +546,12 @@ function consolidateEvents(data) {
442
546
  chats: Object.values(data.historySets.chats),
443
547
  messages: Object.values(data.historySets.messages),
444
548
  contacts: Object.values(data.historySets.contacts),
445
- isLatest: data.historySets.isLatest
549
+ pastParticipants: data.historySets.pastParticipants,
550
+ syncType: data.historySets.syncType,
551
+ progress: data.historySets.progress,
552
+ isLatest: data.historySets.isLatest,
553
+ chunkOrder: data.historySets.chunkOrder,
554
+ peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
446
555
  };
447
556
  }
448
557
  const chatUpsertList = Object.values(data.chatUpserts);
@@ -496,12 +605,10 @@ function consolidateEvents(data) {
496
605
  return map;
497
606
  }
498
607
  function concatChats(a, b) {
499
- if (b.unreadCount === null) {
500
- // neutralize unread counter
501
- if (a.unreadCount < 0) {
502
- a.unreadCount = undefined;
503
- b.unreadCount = undefined;
504
- }
608
+ if (b.unreadCount === null && // neutralize unread counter
609
+ a.unreadCount < 0) {
610
+ a.unreadCount = undefined;
611
+ b.unreadCount = undefined;
505
612
  }
506
613
  if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
507
614
  b = { ...b };
@@ -512,3 +619,4 @@ function concatChats(a, b) {
512
619
  return Object.assign(a, b);
513
620
  }
514
621
  const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
622
+ //# sourceMappingURL=event-buffer.js.map