@sixcore/baileys 1.0.0 → 1.0.2

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