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