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