@sanzoffc/baileys 3.0.1 → 3.0.3

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 (114) hide show
  1. package/LICENSE +1 -1
  2. package/WAProto/WAProto.proto +769 -233
  3. package/WAProto/index.js +65801 -141371
  4. package/lib/Defaults/index.js +117 -114
  5. package/lib/Defaults/index.js.bak +123 -0
  6. package/lib/KeyDB/BinarySearch.js +20 -0
  7. package/lib/KeyDB/KeyedDB.js +167 -0
  8. package/lib/KeyDB/index.js +4 -0
  9. package/lib/Signal/Group/ciphertext-message.js +12 -14
  10. package/lib/Signal/Group/group-session-builder.js +10 -42
  11. package/lib/Signal/Group/group_cipher.js +75 -87
  12. package/lib/Signal/Group/index.js +13 -57
  13. package/lib/Signal/Group/keyhelper.js +17 -52
  14. package/lib/Signal/Group/sender-chain-key.js +27 -33
  15. package/lib/Signal/Group/sender-key-distribution-message.js +62 -63
  16. package/lib/Signal/Group/sender-key-message.js +65 -66
  17. package/lib/Signal/Group/sender-key-name.js +45 -44
  18. package/lib/Signal/Group/sender-key-record.js +39 -49
  19. package/lib/Signal/Group/sender-key-state.js +80 -93
  20. package/lib/Signal/Group/sender-message-key.js +27 -28
  21. package/lib/Signal/libsignal.js +313 -163
  22. package/lib/Signal/lid-mapping.js +155 -0
  23. package/lib/Socket/Client/index.js +4 -18
  24. package/lib/Socket/Client/types.js +12 -12
  25. package/lib/Socket/Client/websocket.js +51 -71
  26. package/lib/Socket/Client/websocket.js.bak +53 -0
  27. package/lib/Socket/business.js +359 -242
  28. package/lib/Socket/chats.js +858 -945
  29. package/lib/Socket/communities.js +413 -0
  30. package/lib/Socket/groups.js +304 -324
  31. package/lib/Socket/index.js +15 -9
  32. package/lib/Socket/messages-recv.js +1105 -1046
  33. package/lib/Socket/messages-send.js +615 -389
  34. package/lib/Socket/mex.js +45 -0
  35. package/lib/Socket/newsletter.js +224 -227
  36. package/lib/Socket/socket.js +795 -621
  37. package/lib/Store/index.js +6 -8
  38. package/lib/Store/make-cache-manager-store.js +75 -0
  39. package/lib/Store/make-in-memory-store.js +286 -435
  40. package/lib/Store/make-ordered-dictionary.js +77 -79
  41. package/lib/Store/object-repository.js +24 -26
  42. package/lib/Types/Auth.js +3 -2
  43. package/lib/Types/Bussines.js +3 -0
  44. package/lib/Types/Call.js +3 -2
  45. package/lib/Types/Chat.js +9 -4
  46. package/lib/Types/Contact.js +3 -2
  47. package/lib/Types/Events.js +3 -2
  48. package/lib/Types/GroupMetadata.js +3 -2
  49. package/lib/Types/Label.js +24 -26
  50. package/lib/Types/LabelAssociation.js +6 -8
  51. package/lib/Types/Message.js +12 -7
  52. package/lib/Types/Newsletter.js +32 -17
  53. package/lib/Types/Newsletter.js.bak +33 -0
  54. package/lib/Types/Product.js +3 -2
  55. package/lib/Types/Signal.js +3 -2
  56. package/lib/Types/Socket.js +4 -2
  57. package/lib/Types/State.js +11 -2
  58. package/lib/Types/USync.js +3 -2
  59. package/lib/Types/index.js +27 -41
  60. package/lib/Utils/auth-utils.js +211 -191
  61. package/lib/Utils/baileys-event-stream.js +44 -0
  62. package/lib/Utils/browser-utils.js +21 -31
  63. package/lib/Utils/business.js +213 -214
  64. package/lib/Utils/chat-utils.js +711 -689
  65. package/lib/Utils/crypto.js +112 -175
  66. package/lib/Utils/decode-wa-message.js +254 -194
  67. package/lib/Utils/event-buffer.js +510 -500
  68. package/lib/Utils/generics.js +318 -430
  69. package/lib/Utils/history.js +83 -90
  70. package/lib/Utils/index.js +21 -35
  71. package/lib/Utils/link-preview.js +71 -116
  72. package/lib/Utils/logger.js +5 -7
  73. package/lib/Utils/lt-hash.js +40 -46
  74. package/lib/Utils/make-mutex.js +34 -41
  75. package/lib/Utils/message-retry-manager.js +33 -48
  76. package/lib/Utils/messages-media.js +573 -825
  77. package/lib/Utils/messages.js +349 -489
  78. package/lib/Utils/noise-handler.js +138 -144
  79. package/lib/Utils/pre-key-manager.js +85 -0
  80. package/lib/Utils/process-message.js +321 -384
  81. package/lib/Utils/signal.js +147 -139
  82. package/lib/Utils/use-multi-file-auth-state.js +95 -109
  83. package/lib/Utils/validate-connection.js +183 -212
  84. package/lib/WABinary/constants.js +1298 -1298
  85. package/lib/WABinary/decode.js +231 -256
  86. package/lib/WABinary/encode.js +207 -239
  87. package/lib/WABinary/generic-utils.js +119 -40
  88. package/lib/WABinary/index.js +7 -21
  89. package/lib/WABinary/jid-utils.js +87 -79
  90. package/lib/WABinary/types.js +3 -2
  91. package/lib/WAM/BinaryInfo.js +10 -12
  92. package/lib/WAM/constants.js +22851 -15348
  93. package/lib/WAM/encode.js +135 -136
  94. package/lib/WAM/index.js +5 -19
  95. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -30
  96. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +49 -53
  97. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -28
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +36 -39
  99. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  100. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +26 -20
  101. package/lib/WAUSync/Protocols/index.js +6 -20
  102. package/lib/WAUSync/USyncQuery.js +86 -85
  103. package/lib/WAUSync/USyncUser.js +23 -25
  104. package/lib/WAUSync/index.js +5 -19
  105. package/lib/index.js +18 -49
  106. package/package.json +65 -78
  107. package/README.MD +0 -1295
  108. package/WAProto/GenerateStatics.sh +0 -4
  109. package/WAProto/p.html +0 -1
  110. package/engine-requirements.js +0 -10
  111. package/lib/Defaults/wileys-version.json +0 -3
  112. package/lib/Signal/Group/queue-job.js +0 -57
  113. package/lib/Socket/usync.js +0 -70
  114. package/lib/Utils/wileys-event-stream.js +0 -63
@@ -1,518 +1,528 @@
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
+ //=======================================================//
2
+ import { updateMessageWithReaction, updateMessageWithReceipt } from "./messages.js";
3
+ import { isRealMessage, shouldIncrementChatUnread } from "./process-message.js";
4
+ import { WAMessageStatus } from "../Types/index.js";
5
+ import { trimUndefined } from "./generics.js";
6
+ import EventEmitter from "events";
7
+ //=======================================================//
12
8
  const BUFFERABLE_EVENT = [
13
- 'messaging-history.set',
14
- 'chats.upsert',
15
- 'chats.update',
16
- 'chats.delete',
17
- 'contacts.upsert',
18
- 'contacts.update',
19
- 'messages.upsert',
20
- 'messages.update',
21
- 'messages.delete',
22
- 'messages.reaction',
23
- 'message-receipt.update',
24
- 'groups.update',
9
+ "messaging-history.set",
10
+ "chats.upsert",
11
+ "chats.update",
12
+ "chats.delete",
13
+ "contacts.upsert",
14
+ "contacts.update",
15
+ "messages.upsert",
16
+ "messages.update",
17
+ "messages.delete",
18
+ "messages.reaction",
19
+ "message-receipt.update",
20
+ "groups.update"
25
21
  ];
22
+ //=======================================================//
26
23
  const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
27
- /**
28
- * The event buffer logically consolidates different events into a single event
29
- * making the data processing more efficient.
30
- * @param ev the wileys event emitter
31
- */
32
- const makeEventBuffer = (logger) => {
33
- const ev = new events_1.default();
34
- const historyCache = new Set();
35
- let data = makeBufferData();
36
- let buffersInProgress = 0;
37
- // take the generic event and fire it as a wileys event
38
- ev.on('event', (map) => {
39
- for (const event in map) {
40
- ev.emit(event, map[event]);
41
- }
42
- });
43
- function buffer() {
44
- buffersInProgress += 1;
45
- }
46
- function flush(force = false) {
47
- // no buffer going on
48
- if (!buffersInProgress) {
49
- return false;
50
- }
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
- }
59
- }
60
- const newData = makeBufferData();
61
- const chatUpdates = Object.values(data.chatUpdates);
62
- // gather the remaining conditional events so we re-queue them
63
- let conditionalChatUpdatesLeft = 0;
64
- for (const update of chatUpdates) {
65
- if (update.conditional) {
66
- conditionalChatUpdatesLeft += 1;
67
- newData.chatUpdates[update.id] = update;
68
- delete data.chatUpdates[update.id];
69
- }
70
- }
71
- const consolidatedData = consolidateEvents(data);
72
- if (Object.keys(consolidatedData).length) {
73
- ev.emit('event', consolidatedData);
74
- }
75
- data = newData;
76
- logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events');
77
- return true;
24
+ //=======================================================//
25
+ export const makeEventBuffer = (logger) => {
26
+ const ev = new EventEmitter();
27
+ const historyCache = new Set();
28
+ let data = makeBufferData();
29
+ let isBuffering = false;
30
+ let bufferTimeout = null;
31
+ let bufferCount = 0;
32
+ const MAX_HISTORY_CACHE_SIZE = 10000;
33
+ const BUFFER_TIMEOUT_MS = 30000;
34
+ ev.on("event", (map) => {
35
+ for (const event in map) {
36
+ ev.emit(event, map[event]);
78
37
  }
79
- return {
80
- process(handler) {
81
- const listener = (map) => {
82
- handler(map);
83
- };
84
- ev.on('event', listener);
85
- return () => {
86
- ev.off('event', listener);
87
- };
88
- },
89
- emit(event, evData) {
90
- if (buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
91
- append(data, historyCache, event, evData, logger);
92
- return true;
93
- }
94
- return ev.emit('event', { [event]: evData });
95
- },
96
- isBuffering() {
97
- return buffersInProgress > 0;
98
- },
99
- buffer,
100
- flush,
101
- createBufferedFunction(work) {
102
- return async (...args) => {
103
- buffer();
104
- try {
105
- const result = await work(...args);
106
- return result;
107
- }
108
- finally {
109
- flush();
110
- }
111
- };
112
- },
113
- on: (...args) => ev.on(...args),
114
- off: (...args) => ev.off(...args),
115
- removeAllListeners: (...args) => ev.removeAllListeners(...args),
116
- };
117
- };
118
- exports.makeEventBuffer = makeEventBuffer;
119
- const makeBufferData = () => {
120
- return {
121
- historySets: {
122
- chats: {},
123
- messages: {},
124
- contacts: {},
125
- isLatest: false,
126
- empty: true
127
- },
128
- chatUpserts: {},
129
- chatUpdates: {},
130
- chatDeletes: new Set(),
131
- contactUpserts: {},
132
- contactUpdates: {},
133
- messageUpserts: {},
134
- messageUpdates: {},
135
- messageReactions: {},
136
- messageDeletes: {},
137
- messageReceipts: {},
138
- groupUpdates: {}
139
- };
140
- };
141
- function append(data, historyCache, event, eventData, logger) {
142
- var _a, _b, _c;
143
- switch (event) {
144
- case 'messaging-history.set':
145
- for (const chat of eventData.chats) {
146
- const existingChat = data.historySets.chats[chat.id];
147
- if (existingChat) {
148
- existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
149
- }
150
- if (!existingChat && !historyCache.has(chat.id)) {
151
- data.historySets.chats[chat.id] = chat;
152
- historyCache.add(chat.id);
153
- absorbingChatUpdate(chat);
154
- }
155
- }
156
- for (const contact of eventData.contacts) {
157
- const existingContact = data.historySets.contacts[contact.id];
158
- if (existingContact) {
159
- Object.assign(existingContact, (0, generics_1.trimUndefined)(contact));
160
- }
161
- else {
162
- const historyContactId = `c:${contact.id}`;
163
- const hasAnyName = contact.notify || contact.name || contact.verifiedName;
164
- if (!historyCache.has(historyContactId) || hasAnyName) {
165
- data.historySets.contacts[contact.id] = contact;
166
- historyCache.add(historyContactId);
167
- }
168
- }
169
- }
170
- for (const message of eventData.messages) {
171
- const key = stringifyMessageKey(message.key);
172
- const existingMsg = data.historySets.messages[key];
173
- if (!existingMsg && !historyCache.has(key)) {
174
- data.historySets.messages[key] = message;
175
- historyCache.add(key);
176
- }
177
- }
178
- data.historySets.empty = false;
179
- data.historySets.syncType = eventData.syncType;
180
- data.historySets.progress = eventData.progress;
181
- data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
182
- data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
183
- break;
184
- case 'chats.upsert':
185
- for (const chat of eventData) {
186
- let upsert = data.chatUpserts[chat.id];
187
- if (!upsert) {
188
- upsert = data.historySets[chat.id];
189
- if (upsert) {
190
- logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set');
191
- }
192
- }
193
- if (upsert) {
194
- upsert = concatChats(upsert, chat);
195
- }
196
- else {
197
- upsert = chat;
198
- data.chatUpserts[chat.id] = upsert;
199
- }
200
- absorbingChatUpdate(upsert);
201
- if (data.chatDeletes.has(chat.id)) {
202
- data.chatDeletes.delete(chat.id);
203
- }
204
- }
205
- break;
206
- case 'chats.update':
207
- for (const update of eventData) {
208
- const chatId = update.id;
209
- const conditionMatches = update.conditional ? update.conditional(data) : true;
210
- if (conditionMatches) {
211
- delete update.conditional;
212
- // if there is an existing upsert, merge the update into it
213
- const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
214
- if (upsert) {
215
- concatChats(upsert, update);
216
- }
217
- else {
218
- // merge the update into the existing update
219
- const chatUpdate = data.chatUpdates[chatId] || {};
220
- data.chatUpdates[chatId] = concatChats(chatUpdate, update);
221
- }
222
- }
223
- else if (conditionMatches === undefined) {
224
- // condition yet to be fulfilled
225
- data.chatUpdates[chatId] = update;
226
- }
227
- // otherwise -- condition not met, update is invalid
228
- // if the chat has been updated
229
- // ignore any existing chat delete
230
- if (data.chatDeletes.has(chatId)) {
231
- data.chatDeletes.delete(chatId);
232
- }
233
- }
234
- break;
235
- case 'chats.delete':
236
- for (const chatId of eventData) {
237
- if (!data.chatDeletes.has(chatId)) {
238
- data.chatDeletes.add(chatId);
239
- }
240
- // remove any prior updates & upserts
241
- if (data.chatUpdates[chatId]) {
242
- delete data.chatUpdates[chatId];
243
- }
244
- if (data.chatUpserts[chatId]) {
245
- delete data.chatUpserts[chatId];
246
- }
247
- if (data.historySets.chats[chatId]) {
248
- delete data.historySets.chats[chatId];
249
- }
250
- }
251
- break;
252
- case 'contacts.upsert':
253
- for (const contact of eventData) {
254
- let upsert = data.contactUpserts[contact.id];
255
- if (!upsert) {
256
- upsert = data.historySets.contacts[contact.id];
257
- if (upsert) {
258
- logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set');
259
- }
260
- }
261
- if (upsert) {
262
- upsert = Object.assign(upsert, (0, generics_1.trimUndefined)(contact));
263
- }
264
- else {
265
- upsert = contact;
266
- data.contactUpserts[contact.id] = upsert;
267
- }
268
- if (data.contactUpdates[contact.id]) {
269
- upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_1.trimUndefined)(contact));
270
- delete data.contactUpdates[contact.id];
271
- }
272
- }
273
- break;
274
- case 'contacts.update':
275
- const contactUpdates = eventData;
276
- for (const update of contactUpdates) {
277
- const id = update.id;
278
- // merge into prior upsert
279
- const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
280
- if (upsert) {
281
- Object.assign(upsert, update);
282
- }
283
- else {
284
- // merge into prior update
285
- const contactUpdate = data.contactUpdates[id] || {};
286
- data.contactUpdates[id] = Object.assign(contactUpdate, update);
287
- }
288
- }
289
- break;
290
- case 'messages.upsert':
291
- const { messages, type } = eventData;
292
- for (const message of messages) {
293
- const key = stringifyMessageKey(message.key);
294
- let existing = (_a = data.messageUpserts[key]) === null || _a === void 0 ? void 0 : _a.message;
295
- if (!existing) {
296
- existing = data.historySets.messages[key];
297
- if (existing) {
298
- logger.debug({ messageId: key }, 'absorbed message upsert in message set');
299
- }
300
- }
301
- if (existing) {
302
- message.messageTimestamp = existing.messageTimestamp;
303
- }
304
- if (data.messageUpdates[key]) {
305
- logger.debug('absorbed prior message update in message upsert');
306
- Object.assign(message, data.messageUpdates[key].update);
307
- delete data.messageUpdates[key];
308
- }
309
- if (data.historySets.messages[key]) {
310
- data.historySets.messages[key] = message;
311
- }
312
- else {
313
- data.messageUpserts[key] = {
314
- message,
315
- type: type === 'notify' || ((_b = data.messageUpserts[key]) === null || _b === void 0 ? void 0 : _b.type) === 'notify'
316
- ? 'notify'
317
- : type
318
- };
319
- }
320
- }
321
- break;
322
- case 'messages.update':
323
- const msgUpdates = eventData;
324
- for (const { key, update } of msgUpdates) {
325
- const keyStr = stringifyMessageKey(key);
326
- const existing = data.historySets.messages[keyStr] || ((_c = data.messageUpserts[keyStr]) === null || _c === void 0 ? void 0 : _c.message);
327
- if (existing) {
328
- Object.assign(existing, update);
329
- // if the message was received & read by us
330
- // the chat counter must have been incremented
331
- // so we need to decrement it
332
- if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
333
- decrementChatReadCounterIfMsgDidUnread(existing);
334
- }
335
- }
336
- else {
337
- const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} };
338
- Object.assign(msgUpdate.update, update);
339
- data.messageUpdates[keyStr] = msgUpdate;
340
- }
341
- }
342
- break;
343
- case 'messages.delete':
344
- const deleteData = eventData;
345
- if ('keys' in deleteData) {
346
- const { keys } = deleteData;
347
- for (const key of keys) {
348
- const keyStr = stringifyMessageKey(key);
349
- if (!data.messageDeletes[keyStr]) {
350
- data.messageDeletes[keyStr] = key;
351
- }
352
- if (data.messageUpserts[keyStr]) {
353
- delete data.messageUpserts[keyStr];
354
- }
355
- if (data.messageUpdates[keyStr]) {
356
- delete data.messageUpdates[keyStr];
357
- }
358
- }
359
- }
360
- else {
361
- // TODO: add support
362
- }
363
- break;
364
- case 'messages.reaction':
365
- const reactions = eventData;
366
- for (const { key, reaction } of reactions) {
367
- const keyStr = stringifyMessageKey(key);
368
- const existing = data.messageUpserts[keyStr];
369
- if (existing) {
370
- (0, messages_1.updateMessageWithReaction)(existing.message, reaction);
371
- }
372
- else {
373
- data.messageReactions[keyStr] = data.messageReactions[keyStr]
374
- || { key, reactions: [] };
375
- (0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
376
- }
377
- }
378
- break;
379
- case 'message-receipt.update':
380
- const receipts = eventData;
381
- for (const { key, receipt } of receipts) {
382
- const keyStr = stringifyMessageKey(key);
383
- const existing = data.messageUpserts[keyStr];
384
- if (existing) {
385
- (0, messages_1.updateMessageWithReceipt)(existing.message, receipt);
386
- }
387
- else {
388
- data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
389
- || { key, userReceipt: [] };
390
- (0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
391
- }
392
- }
393
- break;
394
- case 'groups.update':
395
- const groupUpdates = eventData;
396
- for (const update of groupUpdates) {
397
- const id = update.id;
398
- const groupUpdate = data.groupUpdates[id] || {};
399
- if (!data.groupUpdates[id]) {
400
- data.groupUpdates[id] = Object.assign(groupUpdate, update);
401
- }
402
- }
403
- break;
404
- default:
405
- throw new Error(`"${event}" cannot be buffered`);
406
- }
407
- function absorbingChatUpdate(existing) {
408
- const chatId = existing.id;
409
- const update = data.chatUpdates[chatId];
410
- if (update) {
411
- const conditionMatches = update.conditional ? update.conditional(data) : true;
412
- if (conditionMatches) {
413
- delete update.conditional;
414
- logger.debug({ chatId }, 'absorbed chat update in existing chat');
415
- Object.assign(existing, concatChats(update, existing));
416
- delete data.chatUpdates[chatId];
417
- }
418
- else if (conditionMatches === false) {
419
- logger.debug({ chatId }, 'chat update condition fail, removing');
420
- delete data.chatUpdates[chatId];
421
- }
422
- }
423
- }
424
- function decrementChatReadCounterIfMsgDidUnread(message) {
425
- // decrement chat unread counter
426
- // if the message has already been marked read by us
427
- const chatId = message.key.remoteJid;
428
- const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
429
- if ((0, process_message_1.isRealMessage)(message, '')
430
- && (0, process_message_1.shouldIncrementChatUnread)(message)
431
- && typeof (chat === null || chat === void 0 ? void 0 : chat.unreadCount) === 'number'
432
- && chat.unreadCount > 0) {
433
- logger.debug({ chatId: chat.id }, 'decrementing chat counter');
434
- chat.unreadCount -= 1;
435
- if (chat.unreadCount === 0) {
436
- delete chat.unreadCount;
437
- }
38
+ });
39
+ function buffer() {
40
+ if (!isBuffering) {
41
+ logger.debug("Event buffer activated");
42
+ isBuffering = true;
43
+ bufferCount++;
44
+ if (bufferTimeout) {
45
+ clearTimeout(bufferTimeout);
46
+ }
47
+ bufferTimeout = setTimeout(() => {
48
+ if (isBuffering) {
49
+ logger.warn("Buffer timeout reached, auto-flushing");
50
+ flush();
438
51
  }
52
+ }, BUFFER_TIMEOUT_MS);
439
53
  }
440
- }
441
- function consolidateEvents(data) {
442
- const map = {};
443
- if (!data.historySets.empty) {
444
- map['messaging-history.set'] = {
445
- chats: Object.values(data.historySets.chats),
446
- messages: Object.values(data.historySets.messages),
447
- contacts: Object.values(data.historySets.contacts),
448
- syncType: data.historySets.syncType,
449
- progress: data.historySets.progress,
450
- isLatest: data.historySets.isLatest,
451
- peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
452
- };
453
- }
454
- const chatUpsertList = Object.values(data.chatUpserts);
455
- if (chatUpsertList.length) {
456
- map['chats.upsert'] = chatUpsertList;
457
- }
458
- const chatUpdateList = Object.values(data.chatUpdates);
459
- if (chatUpdateList.length) {
460
- map['chats.update'] = chatUpdateList;
461
- }
462
- const chatDeleteList = Array.from(data.chatDeletes);
463
- if (chatDeleteList.length) {
464
- map['chats.delete'] = chatDeleteList;
54
+ else {
55
+ bufferCount++;
465
56
  }
466
- const messageUpsertList = Object.values(data.messageUpserts);
467
- if (messageUpsertList.length) {
468
- const type = messageUpsertList[0].type;
469
- map['messages.upsert'] = {
470
- messages: messageUpsertList.map(m => m.message),
471
- type
472
- };
57
+ }
58
+ function flush() {
59
+ if (!isBuffering) {
60
+ return false;
473
61
  }
474
- const messageUpdateList = Object.values(data.messageUpdates);
475
- if (messageUpdateList.length) {
476
- map['messages.update'] = messageUpdateList;
62
+ logger.debug({ bufferCount }, "Flushing event buffer");
63
+ isBuffering = false;
64
+ bufferCount = 0;
65
+ if (bufferTimeout) {
66
+ clearTimeout(bufferTimeout);
67
+ bufferTimeout = null;
477
68
  }
478
- const messageDeleteList = Object.values(data.messageDeletes);
479
- if (messageDeleteList.length) {
480
- map['messages.delete'] = { keys: messageDeleteList };
69
+ if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
70
+ logger.debug({ cacheSize: historyCache.size }, "Clearing history cache");
71
+ historyCache.clear();
481
72
  }
482
- const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })));
483
- if (messageReactionList.length) {
484
- map['messages.reaction'] = messageReactionList;
73
+ const newData = makeBufferData();
74
+ const chatUpdates = Object.values(data.chatUpdates);
75
+ let conditionalChatUpdatesLeft = 0;
76
+ for (const update of chatUpdates) {
77
+ if (update.conditional) {
78
+ conditionalChatUpdatesLeft += 1;
79
+ newData.chatUpdates[update.id] = update;
80
+ delete data.chatUpdates[update.id];
81
+ }
485
82
  }
486
- const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })));
487
- if (messageReceiptList.length) {
488
- map['message-receipt.update'] = messageReceiptList;
83
+ const consolidatedData = consolidateEvents(data);
84
+ if (Object.keys(consolidatedData).length) {
85
+ ev.emit("event", consolidatedData);
489
86
  }
490
- const contactUpsertList = Object.values(data.contactUpserts);
491
- if (contactUpsertList.length) {
492
- map['contacts.upsert'] = contactUpsertList;
493
- }
494
- const contactUpdateList = Object.values(data.contactUpdates);
495
- if (contactUpdateList.length) {
496
- map['contacts.update'] = contactUpdateList;
87
+ data = newData;
88
+ logger.trace({ conditionalChatUpdatesLeft }, "released buffered events");
89
+ return true;
90
+ }
91
+ return {
92
+ process(handler) {
93
+ const listener = (map) => {
94
+ handler(map);
95
+ };
96
+ ev.on("event", listener);
97
+ return () => {
98
+ ev.off("event", listener);
99
+ };
100
+ },
101
+ emit(event, evData) {
102
+ if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
103
+ append(data, historyCache, event, evData, logger);
104
+ return true;
105
+ }
106
+ return ev.emit("event", { [event]: evData });
107
+ },
108
+ isBuffering() {
109
+ return isBuffering;
110
+ },
111
+ buffer,
112
+ flush,
113
+ createBufferedFunction(work) {
114
+ return async (...args) => {
115
+ buffer();
116
+ try {
117
+ const result = await work(...args);
118
+ if (bufferCount === 1) {
119
+ setTimeout(() => {
120
+ if (isBuffering && bufferCount === 1) {
121
+ flush();
122
+ }
123
+ }, 100);
124
+ }
125
+ return result;
126
+ }
127
+ catch (error) {
128
+ throw error;
129
+ }
130
+ finally {
131
+ bufferCount = Math.max(0, bufferCount - 1);
132
+ if (bufferCount === 0) {
133
+ setTimeout(flush, 100);
134
+ }
135
+ }
136
+ };
137
+ },
138
+ on: (...args) => ev.on(...args),
139
+ off: (...args) => ev.off(...args),
140
+ removeAllListeners: (...args) => ev.removeAllListeners(...args)
141
+ };
142
+ };
143
+ //=======================================================//
144
+ const makeBufferData = () => {
145
+ return {
146
+ "historySets": {
147
+ "chats": {},
148
+ "messages": {},
149
+ "contacts": {},
150
+ "isLatest": false,
151
+ "empty": true
152
+ },
153
+ "chatUpserts": {},
154
+ "chatUpdates": {},
155
+ "chatDeletes": new Set(),
156
+ "contactUpserts": {},
157
+ "contactUpdates": {},
158
+ "messageUpserts": {},
159
+ "messageUpdates": {},
160
+ "messageReactions": {},
161
+ "messageDeletes": {},
162
+ "messageReceipts": {},
163
+ "groupUpdates": {}
164
+ };
165
+ };
166
+ //=======================================================//
167
+ function append(data, historyCache, event, eventData, logger) {
168
+ switch (event) {
169
+ case "messaging-history.set":
170
+ for (const chat of eventData.chats) {
171
+ const id = chat.id || "";
172
+ const existingChat = data.historySets.chats[id];
173
+ if (existingChat) {
174
+ existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
175
+ }
176
+ if (!existingChat && !historyCache.has(id)) {
177
+ data.historySets.chats[id] = chat;
178
+ historyCache.add(id);
179
+ absorbingChatUpdate(chat);
180
+ }
181
+ }
182
+ for (const contact of eventData.contacts) {
183
+ const existingContact = data.historySets.contacts[contact.id];
184
+ if (existingContact) {
185
+ Object.assign(existingContact, trimUndefined(contact));
186
+ }
187
+ else {
188
+ const historyContactId = `c:${contact.id}`;
189
+ const hasAnyName = contact.notify || contact.name || contact.verifiedName;
190
+ if (!historyCache.has(historyContactId) || hasAnyName) {
191
+ data.historySets.contacts[contact.id] = contact;
192
+ historyCache.add(historyContactId);
193
+ }
194
+ }
195
+ }
196
+ for (const message of eventData.messages) {
197
+ const key = stringifyMessageKey(message.key);
198
+ const existingMsg = data.historySets.messages[key];
199
+ if (!existingMsg && !historyCache.has(key)) {
200
+ data.historySets.messages[key] = message;
201
+ historyCache.add(key);
202
+ }
203
+ }
204
+ data.historySets.empty = false;
205
+ data.historySets.syncType = eventData.syncType;
206
+ data.historySets.progress = eventData.progress;
207
+ data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
208
+ data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
209
+ break;
210
+ case "chats.upsert":
211
+ for (const chat of eventData) {
212
+ const id = chat.id || "";
213
+ let upsert = data.chatUpserts[id];
214
+ if (id && !upsert) {
215
+ upsert = data.historySets.chats[id];
216
+ if (upsert) {
217
+ logger.debug({ chatId: id }, "absorbed chat upsert in chat set");
218
+ }
219
+ }
220
+ if (upsert) {
221
+ upsert = concatChats(upsert, chat);
222
+ }
223
+ else {
224
+ upsert = chat;
225
+ data.chatUpserts[id] = upsert;
226
+ }
227
+ absorbingChatUpdate(upsert);
228
+ if (data.chatDeletes.has(id)) {
229
+ data.chatDeletes.delete(id);
230
+ }
231
+ }
232
+ break;
233
+ case "chats.update":
234
+ for (const update of eventData) {
235
+ const chatId = update.id;
236
+ const conditionMatches = update.conditional ? update.conditional(data) : true;
237
+ if (conditionMatches) {
238
+ delete update.conditional;
239
+ const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
240
+ if (upsert) {
241
+ concatChats(upsert, update);
242
+ }
243
+ else {
244
+ const chatUpdate = data.chatUpdates[chatId] || {};
245
+ data.chatUpdates[chatId] = concatChats(chatUpdate, update);
246
+ }
247
+ }
248
+ else if (conditionMatches === undefined) {
249
+ data.chatUpdates[chatId] = update;
250
+ }
251
+ if (data.chatDeletes.has(chatId)) {
252
+ data.chatDeletes.delete(chatId);
253
+ }
254
+ }
255
+ break;
256
+ case "chats.delete":
257
+ for (const chatId of eventData) {
258
+ if (!data.chatDeletes.has(chatId)) {
259
+ data.chatDeletes.add(chatId);
260
+ }
261
+ if (data.chatUpdates[chatId]) {
262
+ delete data.chatUpdates[chatId];
263
+ }
264
+ if (data.chatUpserts[chatId]) {
265
+ delete data.chatUpserts[chatId];
266
+ }
267
+ if (data.historySets.chats[chatId]) {
268
+ delete data.historySets.chats[chatId];
269
+ }
270
+ }
271
+ break;
272
+ case "contacts.upsert":
273
+ for (const contact of eventData) {
274
+ let upsert = data.contactUpserts[contact.id];
275
+ if (!upsert) {
276
+ upsert = data.historySets.contacts[contact.id];
277
+ if (upsert) {
278
+ logger.debug({ contactId: contact.id }, "absorbed contact upsert in contact set");
279
+ }
280
+ }
281
+ if (upsert) {
282
+ upsert = Object.assign(upsert, trimUndefined(contact));
283
+ }
284
+ else {
285
+ upsert = contact;
286
+ data.contactUpserts[contact.id] = upsert;
287
+ }
288
+ if (data.contactUpdates[contact.id]) {
289
+ upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
290
+ delete data.contactUpdates[contact.id];
291
+ }
292
+ }
293
+ break;
294
+ case "contacts.update":
295
+ const contactUpdates = eventData;
296
+ for (const update of contactUpdates) {
297
+ const id = update.id;
298
+ const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
299
+ if (upsert) {
300
+ Object.assign(upsert, update);
301
+ }
302
+ else {
303
+ const contactUpdate = data.contactUpdates[id] || {};
304
+ data.contactUpdates[id] = Object.assign(contactUpdate, update);
305
+ }
306
+ }
307
+ break;
308
+ case "messages.upsert":
309
+ const { messages, type } = eventData;
310
+ for (const message of messages) {
311
+ const key = stringifyMessageKey(message.key);
312
+ let existing = data.messageUpserts[key]?.message;
313
+ if (!existing) {
314
+ existing = data.historySets.messages[key];
315
+ if (existing) {
316
+ logger.debug({ messageId: key }, "absorbed message upsert in message set");
317
+ }
318
+ }
319
+ if (existing) {
320
+ message.messageTimestamp = existing.messageTimestamp;
321
+ }
322
+ if (data.messageUpdates[key]) {
323
+ logger.debug("absorbed prior message update in message upsert");
324
+ Object.assign(message, data.messageUpdates[key].update);
325
+ delete data.messageUpdates[key];
326
+ }
327
+ if (data.historySets.messages[key]) {
328
+ data.historySets.messages[key] = message;
329
+ }
330
+ else {
331
+ data.messageUpserts[key] = {
332
+ message,
333
+ type: type === "notify" || data.messageUpserts[key]?.type === "notify" ? "notify" : type
334
+ };
335
+ }
336
+ }
337
+ break;
338
+ case "messages.update":
339
+ const msgUpdates = eventData;
340
+ for (const { key, update } of msgUpdates) {
341
+ const keyStr = stringifyMessageKey(key);
342
+ const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
343
+ if (existing) {
344
+ Object.assign(existing, update);
345
+ if (update.status === WAMessageStatus.READ && !key.fromMe) {
346
+ decrementChatReadCounterIfMsgDidUnread(existing);
347
+ }
348
+ }
349
+ else {
350
+ const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} };
351
+ Object.assign(msgUpdate.update, update);
352
+ data.messageUpdates[keyStr] = msgUpdate;
353
+ }
354
+ }
355
+ break;
356
+ case "messages.delete":
357
+ const deleteData = eventData;
358
+ if ("keys" in deleteData) {
359
+ const { keys } = deleteData;
360
+ for (const key of keys) {
361
+ const keyStr = stringifyMessageKey(key);
362
+ if (!data.messageDeletes[keyStr]) {
363
+ data.messageDeletes[keyStr] = key;
364
+ }
365
+ if (data.messageUpserts[keyStr]) {
366
+ delete data.messageUpserts[keyStr];
367
+ }
368
+ if (data.messageUpdates[keyStr]) {
369
+ delete data.messageUpdates[keyStr];
370
+ }
371
+ }
372
+ }
373
+ else {
374
+ }
375
+ break;
376
+ case "messages.reaction":
377
+ const reactions = eventData;
378
+ for (const { key, reaction } of reactions) {
379
+ const keyStr = stringifyMessageKey(key);
380
+ const existing = data.messageUpserts[keyStr];
381
+ if (existing) {
382
+ updateMessageWithReaction(existing.message, reaction);
383
+ }
384
+ else {
385
+ data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
386
+ updateMessageWithReaction(data.messageReactions[keyStr], reaction);
387
+ }
388
+ }
389
+ break;
390
+ case "message-receipt.update":
391
+ const receipts = eventData;
392
+ for (const { key, receipt } of receipts) {
393
+ const keyStr = stringifyMessageKey(key);
394
+ const existing = data.messageUpserts[keyStr];
395
+ if (existing) {
396
+ updateMessageWithReceipt(existing.message, receipt);
397
+ }
398
+ else {
399
+ data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
400
+ updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
401
+ }
402
+ }
403
+ break;
404
+ case "groups.update":
405
+ const groupUpdates = eventData;
406
+ for (const update of groupUpdates) {
407
+ const id = update.id;
408
+ const groupUpdate = data.groupUpdates[id] || {};
409
+ if (!data.groupUpdates[id]) {
410
+ data.groupUpdates[id] = Object.assign(groupUpdate, update);
411
+ }
412
+ }
413
+ break;
414
+ default:
415
+ throw new Error(`"${event}" cannot be buffered`);
416
+ }
417
+ function absorbingChatUpdate(existing) {
418
+ const chatId = existing.id || "";
419
+ const update = data.chatUpdates[chatId];
420
+ if (update) {
421
+ const conditionMatches = update.conditional ? update.conditional(data) : true;
422
+ if (conditionMatches) {
423
+ delete update.conditional;
424
+ logger.debug({ chatId }, "absorbed chat update in existing chat");
425
+ Object.assign(existing, concatChats(update, existing));
426
+ delete data.chatUpdates[chatId];
427
+ }
428
+ else if (conditionMatches === false) {
429
+ logger.debug({ chatId }, "chat update condition fail, removing");
430
+ delete data.chatUpdates[chatId];
431
+ }
497
432
  }
498
- const groupUpdateList = Object.values(data.groupUpdates);
499
- if (groupUpdateList.length) {
500
- map['groups.update'] = groupUpdateList;
433
+ }
434
+ function decrementChatReadCounterIfMsgDidUnread(message) {
435
+ const chatId = message.key.remoteJid;
436
+ const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
437
+ if (isRealMessage(message) &&
438
+ shouldIncrementChatUnread(message) &&
439
+ typeof chat?.unreadCount === "number" &&
440
+ chat.unreadCount > 0) {
441
+ logger.debug({ chatId: chat.id }, "decrementing chat counter");
442
+ chat.unreadCount -= 1;
443
+ if (chat.unreadCount === 0) {
444
+ delete chat.unreadCount;
445
+ }
501
446
  }
502
- return map;
447
+ }
448
+ }
449
+ function consolidateEvents(data) {
450
+ const map = {};
451
+ if (!data.historySets.empty) {
452
+ map["messaging-history.set"] = {
453
+ chats: Object.values(data.historySets.chats),
454
+ messages: Object.values(data.historySets.messages),
455
+ contacts: Object.values(data.historySets.contacts),
456
+ syncType: data.historySets.syncType,
457
+ progress: data.historySets.progress,
458
+ isLatest: data.historySets.isLatest,
459
+ peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
460
+ };
461
+ }
462
+ const chatUpsertList = Object.values(data.chatUpserts);
463
+ if (chatUpsertList.length) {
464
+ map["chats.upsert"] = chatUpsertList;
465
+ }
466
+ const chatUpdateList = Object.values(data.chatUpdates);
467
+ if (chatUpdateList.length) {
468
+ map["chats.update"] = chatUpdateList;
469
+ }
470
+ const chatDeleteList = Array.from(data.chatDeletes);
471
+ if (chatDeleteList.length) {
472
+ map["chats.delete"] = chatDeleteList;
473
+ }
474
+ const messageUpsertList = Object.values(data.messageUpserts);
475
+ if (messageUpsertList.length) {
476
+ const type = messageUpsertList[0].type;
477
+ map["messages.upsert"] = {
478
+ messages: messageUpsertList.map(m => m.message),
479
+ type
480
+ };
481
+ }
482
+ const messageUpdateList = Object.values(data.messageUpdates);
483
+ if (messageUpdateList.length) {
484
+ map["messages.update"] = messageUpdateList;
485
+ }
486
+ const messageDeleteList = Object.values(data.messageDeletes);
487
+ if (messageDeleteList.length) {
488
+ map["messages.delete"] = { keys: messageDeleteList };
489
+ }
490
+ const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })));
491
+ if (messageReactionList.length) {
492
+ map["messages.reaction"] = messageReactionList;
493
+ }
494
+ const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })));
495
+ if (messageReceiptList.length) {
496
+ map["message-receipt.update"] = messageReceiptList;
497
+ }
498
+ const contactUpsertList = Object.values(data.contactUpserts);
499
+ if (contactUpsertList.length) {
500
+ map["contacts.upsert"] = contactUpsertList;
501
+ }
502
+ const contactUpdateList = Object.values(data.contactUpdates);
503
+ if (contactUpdateList.length) {
504
+ map["contacts.update"] = contactUpdateList;
505
+ }
506
+ const groupUpdateList = Object.values(data.groupUpdates);
507
+ if (groupUpdateList.length) {
508
+ map["groups.update"] = groupUpdateList;
509
+ }
510
+ return map;
503
511
  }
512
+ //=======================================================//
504
513
  function concatChats(a, b) {
505
- if (b.unreadCount === null && // neutralize unread counter
506
- a.unreadCount < 0) {
507
- a.unreadCount = undefined;
508
- b.unreadCount = undefined;
509
- }
510
- if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
511
- b = { ...b };
512
- if (b.unreadCount >= 0) {
513
- b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
514
- }
514
+ if (b.unreadCount === null &&
515
+ a.unreadCount < 0) {
516
+ a.unreadCount = undefined;
517
+ b.unreadCount = undefined;
518
+ }
519
+ if (typeof a.unreadCount === "number" && typeof b.unreadCount === "number") {
520
+ b = { ...b };
521
+ if (b.unreadCount >= 0) {
522
+ b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
515
523
  }
516
- return Object.assign(a, b);
524
+ }
525
+ return Object.assign(a, b);
517
526
  }
518
- const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
527
+ const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? "1" : "0"}`;
528
+ //=======================================================//