@nexustechpro/baileys 1.0.1 → 1.0.3-rc.1

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.
@@ -1,548 +1,547 @@
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';
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"
6
6
  const BUFFERABLE_EVENT = [
7
- 'messaging-history.set',
8
- 'chats.upsert',
9
- 'chats.update',
10
- 'chats.delete',
11
- 'contacts.upsert',
12
- 'contacts.update',
13
- 'messages.upsert',
14
- 'messages.update',
15
- 'messages.delete',
16
- 'messages.reaction',
17
- 'message-receipt.update',
18
- 'groups.update'
19
- ];
20
- const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
7
+ "messaging-history.set",
8
+ "chats.upsert",
9
+ "chats.update",
10
+ "chats.delete",
11
+ "contacts.upsert",
12
+ "contacts.update",
13
+ "messages.upsert",
14
+ "messages.update",
15
+ "messages.delete",
16
+ "messages.reaction",
17
+ "message-receipt.update",
18
+ "groups.update",
19
+ ]
20
+ const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT)
21
21
  /**
22
22
  * The event buffer logically consolidates different events into a single event
23
23
  * making the data processing more efficient.
24
24
  */
25
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; // Limit the history cache size to prevent memory bloat
33
- const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
34
- // take the generic event and fire it as a baileys event
35
- ev.on('event', (map) => {
36
- for (const event in map) {
37
- ev.emit(event, map[event]);
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 // Limit the history cache size to prevent memory bloat
33
+ const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
34
+ // take the generic event and fire it as a baileys event
35
+ ev.on("event", (map) => {
36
+ for (const event in map) {
37
+ ev.emit(event, map[event])
38
+ }
39
+ })
40
+ function buffer() {
41
+ if (!isBuffering) {
42
+ logger.debug("Event buffer activated")
43
+ isBuffering = true
44
+ bufferCount++
45
+ // Auto-flush after a timeout to prevent infinite buffering
46
+ if (bufferTimeout) {
47
+ clearTimeout(bufferTimeout)
48
+ }
49
+ bufferTimeout = setTimeout(() => {
50
+ if (isBuffering) {
51
+ logger.warn("Buffer timeout reached, auto-flushing")
52
+ flush()
38
53
  }
39
- });
40
- function buffer() {
41
- if (!isBuffering) {
42
- logger.debug('Event buffer activated');
43
- isBuffering = true;
44
- bufferCount++;
45
- // Auto-flush after a timeout to prevent infinite buffering
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);
54
+ }, BUFFER_TIMEOUT_MS)
55
+ } else {
56
+ bufferCount++
57
+ }
58
+ }
59
+ function flush() {
60
+ if (!isBuffering) {
61
+ return false
62
+ }
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
+ // Clear history cache if it exceeds the max size
72
+ if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
73
+ logger.debug({ cacheSize: historyCache.size }, "Clearing history cache")
74
+ historyCache.clear()
75
+ }
76
+ const newData = makeBufferData()
77
+ const chatUpdates = Object.values(data.chatUpdates)
78
+ let conditionalChatUpdatesLeft = 0
79
+ for (const update of chatUpdates) {
80
+ if (update.conditional) {
81
+ conditionalChatUpdatesLeft += 1
82
+ newData.chatUpdates[update.id] = update
83
+ delete data.chatUpdates[update.id]
84
+ }
85
+ }
86
+ const consolidatedData = consolidateEvents(data)
87
+ if (Object.keys(consolidatedData).length) {
88
+ ev.emit("event", consolidatedData)
89
+ }
90
+ data = newData
91
+ logger.trace({ conditionalChatUpdatesLeft }, "released buffered events")
92
+ return true
93
+ }
94
+ return {
95
+ process(handler) {
96
+ const listener = (map) => {
97
+ handler(map)
98
+ }
99
+ ev.on("event", listener)
100
+ return () => {
101
+ ev.off("event", listener)
102
+ }
103
+ },
104
+ emit(event, evData) {
105
+ if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
106
+ append(data, historyCache, event, evData, logger)
107
+ return true
108
+ }
109
+ return ev.emit("event", { [event]: evData })
110
+ },
111
+ isBuffering() {
112
+ return isBuffering
113
+ },
114
+ buffer,
115
+ flush,
116
+ createBufferedFunction(work) {
117
+ return async (...args) => {
118
+ buffer()
119
+ try {
120
+ const result = await work(...args)
121
+ // If this is the only buffer, flush after a small delay
122
+ if (bufferCount === 1) {
123
+ setTimeout(() => {
124
+ if (isBuffering && bufferCount === 1) {
125
+ flush()
126
+ }
127
+ }, 100) // Small delay to allow nested buffers
128
+ }
129
+ return result
130
+ } catch (error) {
131
+ throw error
132
+ } finally {
133
+ bufferCount = Math.max(0, bufferCount - 1)
134
+ if (bufferCount === 0) {
135
+ // Auto-flush when no other buffers are active
136
+ setTimeout(flush, 100)
137
+ }
55
138
  }
56
- else {
57
- bufferCount++;
139
+ }
140
+ },
141
+ on: (...args) => ev.on(...args),
142
+ off: (...args) => ev.off(...args),
143
+ listener: (eventName) => ev.listenerCount(eventName),
144
+ removeAllListeners: (...args) => ev.removeAllListeners(...args),
145
+ }
146
+ }
147
+ const makeBufferData = () => {
148
+ return {
149
+ historySets: {
150
+ chats: {},
151
+ messages: {},
152
+ contacts: {},
153
+ isLatest: false,
154
+ empty: true,
155
+ },
156
+ chatUpserts: {},
157
+ chatUpdates: {},
158
+ chatDeletes: new Set(),
159
+ contactUpserts: {},
160
+ contactUpdates: {},
161
+ messageUpserts: {},
162
+ messageUpdates: {},
163
+ messageReactions: {},
164
+ messageDeletes: {},
165
+ messageReceipts: {},
166
+ groupUpdates: {},
167
+ }
168
+ }
169
+ function append(
170
+ data,
171
+ historyCache,
172
+ event,
173
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
+ eventData,
175
+ logger,
176
+ ) {
177
+ switch (event) {
178
+ case "messaging-history.set":
179
+ for (const chat of eventData.chats) {
180
+ const id = chat.id || ""
181
+ const existingChat = data.historySets.chats[id]
182
+ if (existingChat) {
183
+ existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
58
184
  }
59
- }
60
- function flush() {
61
- if (!isBuffering) {
62
- return false;
185
+ if (!existingChat && !historyCache.has(id)) {
186
+ data.historySets.chats[id] = chat
187
+ historyCache.add(id)
188
+ absorbingChatUpdate(chat)
63
189
  }
64
- logger.debug({ bufferCount }, 'Flushing event buffer');
65
- isBuffering = false;
66
- bufferCount = 0;
67
- // Clear timeout
68
- if (bufferTimeout) {
69
- clearTimeout(bufferTimeout);
70
- bufferTimeout = null;
190
+ }
191
+ for (const contact of eventData.contacts) {
192
+ const existingContact = data.historySets.contacts[contact.id]
193
+ if (existingContact) {
194
+ Object.assign(existingContact, trimUndefined(contact))
195
+ } else {
196
+ const historyContactId = `c:${contact.id}`
197
+ const hasAnyName = contact.notify || contact.name || contact.verifiedName
198
+ if (!historyCache.has(historyContactId) || hasAnyName) {
199
+ data.historySets.contacts[contact.id] = contact
200
+ historyCache.add(historyContactId)
201
+ }
71
202
  }
72
- // Clear history cache if it exceeds the max size
73
- if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
74
- logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
75
- historyCache.clear();
203
+ }
204
+ for (const message of eventData.messages) {
205
+ const key = stringifyMessageKey(message.key)
206
+ const existingMsg = data.historySets.messages[key]
207
+ if (!existingMsg && !historyCache.has(key)) {
208
+ data.historySets.messages[key] = message
209
+ historyCache.add(key)
76
210
  }
77
- const newData = makeBufferData();
78
- const chatUpdates = Object.values(data.chatUpdates);
79
- let conditionalChatUpdatesLeft = 0;
80
- for (const update of chatUpdates) {
81
- if (update.conditional) {
82
- conditionalChatUpdatesLeft += 1;
83
- newData.chatUpdates[update.id] = update;
84
- delete data.chatUpdates[update.id];
85
- }
211
+ }
212
+ data.historySets.empty = false
213
+ data.historySets.syncType = eventData.syncType
214
+ data.historySets.progress = eventData.progress
215
+ data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
216
+ data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
217
+ break
218
+ case "chats.upsert":
219
+ for (const chat of eventData) {
220
+ const id = chat.id || ""
221
+ let upsert = data.chatUpserts[id]
222
+ if (id && !upsert) {
223
+ upsert = data.historySets.chats[id]
224
+ if (upsert) {
225
+ logger.debug({ chatId: id }, "absorbed chat upsert in chat set")
226
+ }
86
227
  }
87
- const consolidatedData = consolidateEvents(data);
88
- if (Object.keys(consolidatedData).length) {
89
- ev.emit('event', consolidatedData);
228
+ if (upsert) {
229
+ upsert = concatChats(upsert, chat)
230
+ } else {
231
+ upsert = chat
232
+ data.chatUpserts[id] = upsert
90
233
  }
91
- data = newData;
92
- logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events');
93
- return true;
94
- }
95
- return {
96
- process(handler) {
97
- const listener = (map) => {
98
- handler(map);
99
- };
100
- ev.on('event', listener);
101
- return () => {
102
- ev.off('event', listener);
103
- };
104
- },
105
- emit(event, evData) {
106
- if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
107
- append(data, historyCache, event, evData, logger);
108
- return true;
109
- }
110
- return ev.emit('event', { [event]: evData });
111
- },
112
- isBuffering() {
113
- return isBuffering;
114
- },
115
- buffer,
116
- flush,
117
- createBufferedFunction(work) {
118
- return async (...args) => {
119
- buffer();
120
- try {
121
- const result = await work(...args);
122
- // If this is the only buffer, flush after a small delay
123
- if (bufferCount === 1) {
124
- setTimeout(() => {
125
- if (isBuffering && bufferCount === 1) {
126
- flush();
127
- }
128
- }, 100); // Small delay to allow nested buffers
129
- }
130
- return result;
131
- }
132
- catch (error) {
133
- throw error;
134
- }
135
- finally {
136
- bufferCount = Math.max(0, bufferCount - 1);
137
- if (bufferCount === 0) {
138
- // Auto-flush when no other buffers are active
139
- setTimeout(flush, 100);
140
- }
141
- }
142
- };
143
- },
144
- on: (...args) => ev.on(...args),
145
- off: (...args) => ev.off(...args),
146
- removeAllListeners: (...args) => ev.removeAllListeners(...args)
147
- };
148
- };
149
- const makeBufferData = () => {
150
- return {
151
- historySets: {
152
- chats: {},
153
- messages: {},
154
- contacts: {},
155
- isLatest: false,
156
- empty: true
157
- },
158
- chatUpserts: {},
159
- chatUpdates: {},
160
- chatDeletes: new Set(),
161
- contactUpserts: {},
162
- contactUpdates: {},
163
- messageUpserts: {},
164
- messageUpdates: {},
165
- messageReactions: {},
166
- messageDeletes: {},
167
- messageReceipts: {},
168
- groupUpdates: {}
169
- };
170
- };
171
- function append(data, historyCache, event,
172
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
173
- eventData, logger) {
174
- switch (event) {
175
- case 'messaging-history.set':
176
- for (const chat of eventData.chats) {
177
- const id = chat.id || '';
178
- const existingChat = data.historySets.chats[id];
179
- if (existingChat) {
180
- existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
181
- }
182
- if (!existingChat && !historyCache.has(id)) {
183
- data.historySets.chats[id] = chat;
184
- historyCache.add(id);
185
- absorbingChatUpdate(chat);
186
- }
187
- }
188
- for (const contact of eventData.contacts) {
189
- const existingContact = data.historySets.contacts[contact.id];
190
- if (existingContact) {
191
- Object.assign(existingContact, trimUndefined(contact));
192
- }
193
- else {
194
- const historyContactId = `c:${contact.id}`;
195
- const hasAnyName = contact.notify || contact.name || contact.verifiedName;
196
- if (!historyCache.has(historyContactId) || hasAnyName) {
197
- data.historySets.contacts[contact.id] = contact;
198
- historyCache.add(historyContactId);
199
- }
200
- }
201
- }
202
- for (const message of eventData.messages) {
203
- const key = stringifyMessageKey(message.key);
204
- const existingMsg = data.historySets.messages[key];
205
- if (!existingMsg && !historyCache.has(key)) {
206
- data.historySets.messages[key] = message;
207
- historyCache.add(key);
208
- }
209
- }
210
- data.historySets.empty = false;
211
- data.historySets.syncType = eventData.syncType;
212
- data.historySets.progress = eventData.progress;
213
- data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId;
214
- data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest;
215
- break;
216
- case 'chats.upsert':
217
- for (const chat of eventData) {
218
- const id = chat.id || '';
219
- let upsert = data.chatUpserts[id];
220
- if (id && !upsert) {
221
- upsert = data.historySets.chats[id];
222
- if (upsert) {
223
- logger.debug({ chatId: id }, 'absorbed chat upsert in chat set');
224
- }
225
- }
226
- if (upsert) {
227
- upsert = concatChats(upsert, chat);
228
- }
229
- else {
230
- upsert = chat;
231
- data.chatUpserts[id] = upsert;
232
- }
233
- absorbingChatUpdate(upsert);
234
- if (data.chatDeletes.has(id)) {
235
- data.chatDeletes.delete(id);
236
- }
237
- }
238
- break;
239
- case 'chats.update':
240
- for (const update of eventData) {
241
- const chatId = update.id;
242
- const conditionMatches = update.conditional ? update.conditional(data) : true;
243
- if (conditionMatches) {
244
- delete update.conditional;
245
- // if there is an existing upsert, merge the update into it
246
- const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId];
247
- if (upsert) {
248
- concatChats(upsert, update);
249
- }
250
- else {
251
- // merge the update into the existing update
252
- const chatUpdate = data.chatUpdates[chatId] || {};
253
- data.chatUpdates[chatId] = concatChats(chatUpdate, update);
254
- }
255
- }
256
- else if (conditionMatches === undefined) {
257
- // condition yet to be fulfilled
258
- data.chatUpdates[chatId] = update;
259
- }
260
- // otherwise -- condition not met, update is invalid
261
- // if the chat has been updated
262
- // ignore any existing chat delete
263
- if (data.chatDeletes.has(chatId)) {
264
- data.chatDeletes.delete(chatId);
265
- }
266
- }
267
- break;
268
- case 'chats.delete':
269
- for (const chatId of eventData) {
270
- if (!data.chatDeletes.has(chatId)) {
271
- data.chatDeletes.add(chatId);
272
- }
273
- // remove any prior updates & upserts
274
- if (data.chatUpdates[chatId]) {
275
- delete data.chatUpdates[chatId];
276
- }
277
- if (data.chatUpserts[chatId]) {
278
- delete data.chatUpserts[chatId];
279
- }
280
- if (data.historySets.chats[chatId]) {
281
- delete data.historySets.chats[chatId];
282
- }
283
- }
284
- break;
285
- case 'contacts.upsert':
286
- for (const contact of eventData) {
287
- let upsert = data.contactUpserts[contact.id];
288
- if (!upsert) {
289
- upsert = data.historySets.contacts[contact.id];
290
- if (upsert) {
291
- logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set');
292
- }
293
- }
294
- if (upsert) {
295
- upsert = Object.assign(upsert, trimUndefined(contact));
296
- }
297
- else {
298
- upsert = contact;
299
- data.contactUpserts[contact.id] = upsert;
300
- }
301
- if (data.contactUpdates[contact.id]) {
302
- upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact));
303
- delete data.contactUpdates[contact.id];
304
- }
305
- }
306
- break;
307
- case 'contacts.update':
308
- const contactUpdates = eventData;
309
- for (const update of contactUpdates) {
310
- const id = update.id;
311
- // merge into prior upsert
312
- const upsert = data.historySets.contacts[id] || data.contactUpserts[id];
313
- if (upsert) {
314
- Object.assign(upsert, update);
315
- }
316
- else {
317
- // merge into prior update
318
- const contactUpdate = data.contactUpdates[id] || {};
319
- data.contactUpdates[id] = Object.assign(contactUpdate, update);
320
- }
321
- }
322
- break;
323
- case 'messages.upsert':
324
- const { messages, type } = eventData;
325
- for (const message of messages) {
326
- const key = stringifyMessageKey(message.key);
327
- let existing = data.messageUpserts[key]?.message;
328
- if (!existing) {
329
- existing = data.historySets.messages[key];
330
- if (existing) {
331
- logger.debug({ messageId: key }, 'absorbed message upsert in message set');
332
- }
333
- }
334
- if (existing) {
335
- message.messageTimestamp = existing.messageTimestamp;
336
- }
337
- if (data.messageUpdates[key]) {
338
- logger.debug('absorbed prior message update in message upsert');
339
- Object.assign(message, data.messageUpdates[key].update);
340
- delete data.messageUpdates[key];
341
- }
342
- if (data.historySets.messages[key]) {
343
- data.historySets.messages[key] = message;
344
- }
345
- else {
346
- data.messageUpserts[key] = {
347
- message,
348
- type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
349
- };
350
- }
351
- }
352
- break;
353
- case 'messages.update':
354
- const msgUpdates = eventData;
355
- for (const { key, update } of msgUpdates) {
356
- const keyStr = stringifyMessageKey(key);
357
- const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
358
- if (existing) {
359
- Object.assign(existing, update);
360
- // if the message was received & read by us
361
- // the chat counter must have been incremented
362
- // so we need to decrement it
363
- if (update.status === WAMessageStatus.READ && !key.fromMe) {
364
- decrementChatReadCounterIfMsgDidUnread(existing);
365
- }
366
- }
367
- else {
368
- const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} };
369
- Object.assign(msgUpdate.update, update);
370
- data.messageUpdates[keyStr] = msgUpdate;
371
- }
372
- }
373
- break;
374
- case 'messages.delete':
375
- const deleteData = eventData;
376
- if ('keys' in deleteData) {
377
- const { keys } = deleteData;
378
- for (const key of keys) {
379
- const keyStr = stringifyMessageKey(key);
380
- if (!data.messageDeletes[keyStr]) {
381
- data.messageDeletes[keyStr] = key;
382
- }
383
- if (data.messageUpserts[keyStr]) {
384
- delete data.messageUpserts[keyStr];
385
- }
386
- if (data.messageUpdates[keyStr]) {
387
- delete data.messageUpdates[keyStr];
388
- }
389
- }
390
- }
391
- else {
392
- // TODO: add support
393
- }
394
- break;
395
- case 'messages.reaction':
396
- const reactions = eventData;
397
- for (const { key, reaction } of reactions) {
398
- const keyStr = stringifyMessageKey(key);
399
- const existing = data.messageUpserts[keyStr];
400
- if (existing) {
401
- updateMessageWithReaction(existing.message, reaction);
402
- }
403
- else {
404
- data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
405
- updateMessageWithReaction(data.messageReactions[keyStr], reaction);
406
- }
407
- }
408
- break;
409
- case 'message-receipt.update':
410
- const receipts = eventData;
411
- for (const { key, receipt } of receipts) {
412
- const keyStr = stringifyMessageKey(key);
413
- const existing = data.messageUpserts[keyStr];
414
- if (existing) {
415
- updateMessageWithReceipt(existing.message, receipt);
416
- }
417
- else {
418
- data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
419
- updateMessageWithReceipt(data.messageReceipts[keyStr], receipt);
420
- }
421
- }
422
- break;
423
- case 'groups.update':
424
- const groupUpdates = eventData;
425
- for (const update of groupUpdates) {
426
- const id = update.id;
427
- const groupUpdate = data.groupUpdates[id] || {};
428
- if (!data.groupUpdates[id]) {
429
- data.groupUpdates[id] = Object.assign(groupUpdate, update);
430
- }
431
- }
432
- break;
433
- default:
434
- throw new Error(`"${event}" cannot be buffered`);
435
- }
436
- function absorbingChatUpdate(existing) {
437
- const chatId = existing.id || '';
438
- const update = data.chatUpdates[chatId];
439
- if (update) {
440
- const conditionMatches = update.conditional ? update.conditional(data) : true;
441
- if (conditionMatches) {
442
- delete update.conditional;
443
- logger.debug({ chatId }, 'absorbed chat update in existing chat');
444
- Object.assign(existing, concatChats(update, existing));
445
- delete data.chatUpdates[chatId];
446
- }
447
- else if (conditionMatches === false) {
448
- logger.debug({ chatId }, 'chat update condition fail, removing');
449
- delete data.chatUpdates[chatId];
450
- }
234
+ absorbingChatUpdate(upsert)
235
+ if (data.chatDeletes.has(id)) {
236
+ data.chatDeletes.delete(id)
451
237
  }
452
- }
453
- function decrementChatReadCounterIfMsgDidUnread(message) {
454
- // decrement chat unread counter
455
- // if the message has already been marked read by us
456
- const chatId = message.key.remoteJid;
457
- const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
458
- if (isRealMessage(message) &&
459
- shouldIncrementChatUnread(message) &&
460
- typeof chat?.unreadCount === 'number' &&
461
- chat.unreadCount > 0) {
462
- logger.debug({ chatId: chat.id }, 'decrementing chat counter');
463
- chat.unreadCount -= 1;
464
- if (chat.unreadCount === 0) {
465
- delete chat.unreadCount;
466
- }
238
+ }
239
+ break
240
+ case "chats.update":
241
+ for (const update of eventData) {
242
+ const chatId = update.id
243
+ const conditionMatches = update.conditional ? update.conditional(data) : true
244
+ if (conditionMatches) {
245
+ delete update.conditional
246
+ // if there is an existing upsert, merge the update into it
247
+ const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
248
+ if (upsert) {
249
+ concatChats(upsert, update)
250
+ } else {
251
+ // merge the update into the existing update
252
+ const chatUpdate = data.chatUpdates[chatId] || {}
253
+ data.chatUpdates[chatId] = concatChats(chatUpdate, update)
254
+ }
255
+ } else if (conditionMatches === undefined) {
256
+ // condition yet to be fulfilled
257
+ data.chatUpdates[chatId] = update
258
+ }
259
+ // otherwise -- condition not met, update is invalid
260
+ // if the chat has been updated
261
+ // ignore any existing chat delete
262
+ if (data.chatDeletes.has(chatId)) {
263
+ data.chatDeletes.delete(chatId)
264
+ }
265
+ }
266
+ break
267
+ case "chats.delete":
268
+ for (const chatId of eventData) {
269
+ if (!data.chatDeletes.has(chatId)) {
270
+ data.chatDeletes.add(chatId)
271
+ }
272
+ // remove any prior updates & upserts
273
+ if (data.chatUpdates[chatId]) {
274
+ delete data.chatUpdates[chatId]
275
+ }
276
+ if (data.chatUpserts[chatId]) {
277
+ delete data.chatUpserts[chatId]
278
+ }
279
+ if (data.historySets.chats[chatId]) {
280
+ delete data.historySets.chats[chatId]
281
+ }
282
+ }
283
+ break
284
+ case "contacts.upsert":
285
+ for (const contact of eventData) {
286
+ let upsert = data.contactUpserts[contact.id]
287
+ if (!upsert) {
288
+ upsert = data.historySets.contacts[contact.id]
289
+ if (upsert) {
290
+ logger.debug({ contactId: contact.id }, "absorbed contact upsert in contact set")
291
+ }
292
+ }
293
+ if (upsert) {
294
+ upsert = Object.assign(upsert, trimUndefined(contact))
295
+ } else {
296
+ upsert = contact
297
+ data.contactUpserts[contact.id] = upsert
298
+ }
299
+ if (data.contactUpdates[contact.id]) {
300
+ upsert = Object.assign(data.contactUpdates[contact.id], trimUndefined(contact))
301
+ delete data.contactUpdates[contact.id]
467
302
  }
303
+ }
304
+ break
305
+ case "contacts.update":
306
+ const contactUpdates = eventData
307
+ for (const update of contactUpdates) {
308
+ const id = update.id
309
+ // merge into prior upsert
310
+ const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
311
+ if (upsert) {
312
+ Object.assign(upsert, update)
313
+ } else {
314
+ // merge into prior update
315
+ const contactUpdate = data.contactUpdates[id] || {}
316
+ data.contactUpdates[id] = Object.assign(contactUpdate, update)
317
+ }
318
+ }
319
+ break
320
+ case "messages.upsert":
321
+ const { messages, type } = eventData
322
+ for (const message of messages) {
323
+ const key = stringifyMessageKey(message.key)
324
+ let existing = data.messageUpserts[key]?.message
325
+ if (!existing) {
326
+ existing = data.historySets.messages[key]
327
+ if (existing) {
328
+ logger.debug({ messageId: key }, "absorbed message upsert in message set")
329
+ }
330
+ }
331
+ if (existing) {
332
+ message.messageTimestamp = existing.messageTimestamp
333
+ }
334
+ if (data.messageUpdates[key]) {
335
+ logger.debug("absorbed prior message update in message upsert")
336
+ Object.assign(message, data.messageUpdates[key].update)
337
+ delete data.messageUpdates[key]
338
+ }
339
+ if (data.historySets.messages[key]) {
340
+ data.historySets.messages[key] = message
341
+ } else {
342
+ data.messageUpserts[key] = {
343
+ message,
344
+ type: type === "notify" || data.messageUpserts[key]?.type === "notify" ? "notify" : type,
345
+ }
346
+ }
347
+ }
348
+ break
349
+ case "messages.update":
350
+ const msgUpdates = eventData
351
+ for (const { key, update } of msgUpdates) {
352
+ const keyStr = stringifyMessageKey(key)
353
+ const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
354
+ if (existing) {
355
+ Object.assign(existing, update)
356
+ // if the message was received & read by us
357
+ // the chat counter must have been incremented
358
+ // so we need to decrement it
359
+ if (update.status === WAMessageStatus.READ && !key.fromMe) {
360
+ decrementChatReadCounterIfMsgDidUnread(existing)
361
+ }
362
+ } else {
363
+ const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
364
+ Object.assign(msgUpdate.update, update)
365
+ data.messageUpdates[keyStr] = msgUpdate
366
+ }
367
+ }
368
+ break
369
+ case "messages.delete":
370
+ const deleteData = eventData
371
+ if ("keys" in deleteData) {
372
+ const { keys } = deleteData
373
+ for (const key of keys) {
374
+ const keyStr = stringifyMessageKey(key)
375
+ if (!data.messageDeletes[keyStr]) {
376
+ data.messageDeletes[keyStr] = key
377
+ }
378
+ if (data.messageUpserts[keyStr]) {
379
+ delete data.messageUpserts[keyStr]
380
+ }
381
+ if (data.messageUpdates[keyStr]) {
382
+ delete data.messageUpdates[keyStr]
383
+ }
384
+ }
385
+ } else {
386
+ // TODO: add support
387
+ }
388
+ break
389
+ case "messages.reaction":
390
+ const reactions = eventData
391
+ for (const { key, reaction } of reactions) {
392
+ const keyStr = stringifyMessageKey(key)
393
+ const existing = data.messageUpserts[keyStr]
394
+ if (existing) {
395
+ updateMessageWithReaction(existing.message, reaction)
396
+ } else {
397
+ data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] }
398
+ updateMessageWithReaction(data.messageReactions[keyStr], reaction)
399
+ }
400
+ }
401
+ break
402
+ case "message-receipt.update":
403
+ const receipts = eventData
404
+ for (const { key, receipt } of receipts) {
405
+ const keyStr = stringifyMessageKey(key)
406
+ const existing = data.messageUpserts[keyStr]
407
+ if (existing) {
408
+ updateMessageWithReceipt(existing.message, receipt)
409
+ } else {
410
+ data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] }
411
+ updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
412
+ }
413
+ }
414
+ break
415
+ case "groups.update":
416
+ const groupUpdates = eventData
417
+ for (const update of groupUpdates) {
418
+ const id = update.id
419
+ const groupUpdate = data.groupUpdates[id] || {}
420
+ if (!data.groupUpdates[id]) {
421
+ data.groupUpdates[id] = Object.assign(groupUpdate, update)
422
+ }
423
+ }
424
+ break
425
+ default:
426
+ throw new Error(`"${event}" cannot be buffered`)
427
+ }
428
+ function absorbingChatUpdate(existing) {
429
+ const chatId = existing.id || ""
430
+ const update = data.chatUpdates[chatId]
431
+ if (update) {
432
+ const conditionMatches = update.conditional ? update.conditional(data) : true
433
+ if (conditionMatches) {
434
+ delete update.conditional
435
+ logger.debug({ chatId }, "absorbed chat update in existing chat")
436
+ Object.assign(existing, concatChats(update, existing))
437
+ delete data.chatUpdates[chatId]
438
+ } else if (conditionMatches === false) {
439
+ logger.debug({ chatId }, "chat update condition fail, removing")
440
+ delete data.chatUpdates[chatId]
441
+ }
442
+ }
443
+ }
444
+ function decrementChatReadCounterIfMsgDidUnread(message) {
445
+ // decrement chat unread counter
446
+ // if the message has already been marked read by us
447
+ const chatId = message.key.remoteJid
448
+ const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
449
+ if (
450
+ isRealMessage(message) &&
451
+ shouldIncrementChatUnread(message) &&
452
+ typeof chat?.unreadCount === "number" &&
453
+ chat.unreadCount > 0
454
+ ) {
455
+ logger.debug({ chatId: chat.id }, "decrementing chat counter")
456
+ chat.unreadCount -= 1
457
+ if (chat.unreadCount === 0) {
458
+ delete chat.unreadCount
459
+ }
468
460
  }
461
+ }
469
462
  }
470
463
  function consolidateEvents(data) {
471
- const map = {};
472
- if (!data.historySets.empty) {
473
- map['messaging-history.set'] = {
474
- chats: Object.values(data.historySets.chats),
475
- messages: Object.values(data.historySets.messages),
476
- contacts: Object.values(data.historySets.contacts),
477
- syncType: data.historySets.syncType,
478
- progress: data.historySets.progress,
479
- isLatest: data.historySets.isLatest,
480
- peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
481
- };
482
- }
483
- const chatUpsertList = Object.values(data.chatUpserts);
484
- if (chatUpsertList.length) {
485
- map['chats.upsert'] = chatUpsertList;
464
+ const map = {}
465
+ if (!data.historySets.empty) {
466
+ map["messaging-history.set"] = {
467
+ chats: Object.values(data.historySets.chats),
468
+ messages: Object.values(data.historySets.messages),
469
+ contacts: Object.values(data.historySets.contacts),
470
+ syncType: data.historySets.syncType,
471
+ progress: data.historySets.progress,
472
+ isLatest: data.historySets.isLatest,
473
+ peerDataRequestSessionId: data.historySets.peerDataRequestSessionId,
486
474
  }
487
- const chatUpdateList = Object.values(data.chatUpdates);
488
- if (chatUpdateList.length) {
489
- map['chats.update'] = chatUpdateList;
475
+ }
476
+ const chatUpsertList = Object.values(data.chatUpserts)
477
+ if (chatUpsertList.length) {
478
+ map["chats.upsert"] = chatUpsertList
479
+ }
480
+ const chatUpdateList = Object.values(data.chatUpdates)
481
+ if (chatUpdateList.length) {
482
+ map["chats.update"] = chatUpdateList
483
+ }
484
+ const chatDeleteList = Array.from(data.chatDeletes)
485
+ if (chatDeleteList.length) {
486
+ map["chats.delete"] = chatDeleteList
487
+ }
488
+ const messageUpsertList = Object.values(data.messageUpserts)
489
+ if (messageUpsertList.length) {
490
+ const type = messageUpsertList[0].type
491
+ map["messages.upsert"] = {
492
+ messages: messageUpsertList.map((m) => m.message),
493
+ type,
490
494
  }
491
- const chatDeleteList = Array.from(data.chatDeletes);
492
- if (chatDeleteList.length) {
493
- map['chats.delete'] = chatDeleteList;
494
- }
495
- const messageUpsertList = Object.values(data.messageUpserts);
496
- if (messageUpsertList.length) {
497
- const type = messageUpsertList[0].type;
498
- map['messages.upsert'] = {
499
- messages: messageUpsertList.map(m => m.message),
500
- type
501
- };
502
- }
503
- const messageUpdateList = Object.values(data.messageUpdates);
504
- if (messageUpdateList.length) {
505
- map['messages.update'] = messageUpdateList;
506
- }
507
- const messageDeleteList = Object.values(data.messageDeletes);
508
- if (messageDeleteList.length) {
509
- map['messages.delete'] = { keys: messageDeleteList };
510
- }
511
- const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) => reactions.flatMap(reaction => ({ key, reaction })));
512
- if (messageReactionList.length) {
513
- map['messages.reaction'] = messageReactionList;
514
- }
515
- const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) => userReceipt.flatMap(receipt => ({ key, receipt })));
516
- if (messageReceiptList.length) {
517
- map['message-receipt.update'] = messageReceiptList;
518
- }
519
- const contactUpsertList = Object.values(data.contactUpserts);
520
- if (contactUpsertList.length) {
521
- map['contacts.upsert'] = contactUpsertList;
522
- }
523
- const contactUpdateList = Object.values(data.contactUpdates);
524
- if (contactUpdateList.length) {
525
- map['contacts.update'] = contactUpdateList;
526
- }
527
- const groupUpdateList = Object.values(data.groupUpdates);
528
- if (groupUpdateList.length) {
529
- map['groups.update'] = groupUpdateList;
530
- }
531
- return map;
495
+ }
496
+ const messageUpdateList = Object.values(data.messageUpdates)
497
+ if (messageUpdateList.length) {
498
+ map["messages.update"] = messageUpdateList
499
+ }
500
+ const messageDeleteList = Object.values(data.messageDeletes)
501
+ if (messageDeleteList.length) {
502
+ map["messages.delete"] = { keys: messageDeleteList }
503
+ }
504
+ const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) =>
505
+ reactions.flatMap((reaction) => ({ key, reaction })),
506
+ )
507
+ if (messageReactionList.length) {
508
+ map["messages.reaction"] = messageReactionList
509
+ }
510
+ const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) =>
511
+ userReceipt.flatMap((receipt) => ({ key, receipt })),
512
+ )
513
+ if (messageReceiptList.length) {
514
+ map["message-receipt.update"] = messageReceiptList
515
+ }
516
+ const contactUpsertList = Object.values(data.contactUpserts)
517
+ if (contactUpsertList.length) {
518
+ map["contacts.upsert"] = contactUpsertList
519
+ }
520
+ const contactUpdateList = Object.values(data.contactUpdates)
521
+ if (contactUpdateList.length) {
522
+ map["contacts.update"] = contactUpdateList
523
+ }
524
+ const groupUpdateList = Object.values(data.groupUpdates)
525
+ if (groupUpdateList.length) {
526
+ map["groups.update"] = groupUpdateList
527
+ }
528
+ return map
532
529
  }
533
530
  function concatChats(a, b) {
534
- if (b.unreadCount === null && // neutralize unread counter
535
- a.unreadCount < 0) {
536
- a.unreadCount = undefined;
537
- b.unreadCount = undefined;
538
- }
539
- if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
540
- b = { ...b };
541
- if (b.unreadCount >= 0) {
542
- b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0);
543
- }
531
+ if (
532
+ b.unreadCount === null && // neutralize unread counter
533
+ a.unreadCount < 0
534
+ ) {
535
+ a.unreadCount = undefined
536
+ b.unreadCount = undefined
537
+ }
538
+ if (typeof a.unreadCount === "number" && typeof b.unreadCount === "number") {
539
+ b = { ...b }
540
+ if (b.unreadCount >= 0) {
541
+ b.unreadCount = Math.max(b.unreadCount, 0) + Math.max(a.unreadCount, 0)
544
542
  }
545
- return Object.assign(a, b);
543
+ }
544
+ return Object.assign(a, b)
546
545
  }
547
- const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`;
548
- //# sourceMappingURL=event-buffer.js.map
546
+ const stringifyMessageKey = (key) => `${key.remoteJid},${key.id},${key.fromMe ? "1" : "0"}`
547
+ //# sourceMappingURL=event-buffer.js.map