@nexustechpro/baileys 2.0.2 → 2.0.6

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 (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/WAProto/index.js +22 -18
  4. package/lib/Defaults/baileys-version.json +6 -2
  5. package/lib/Defaults/index.js +173 -172
  6. package/lib/Signal/libsignal.js +395 -292
  7. package/lib/Signal/lid-mapping.js +264 -171
  8. package/lib/Socket/Client/index.js +2 -2
  9. package/lib/Socket/Client/types.js +10 -10
  10. package/lib/Socket/Client/websocket.js +45 -310
  11. package/lib/Socket/business.js +375 -375
  12. package/lib/Socket/chats.js +916 -963
  13. package/lib/Socket/communities.js +430 -430
  14. package/lib/Socket/groups.js +342 -342
  15. package/lib/Socket/index.js +21 -22
  16. package/lib/Socket/messages-recv.js +963 -743
  17. package/lib/Socket/messages-send.js +273 -321
  18. package/lib/Socket/mex.js +50 -50
  19. package/lib/Socket/newsletter.js +148 -148
  20. package/lib/Socket/nexus-handler.js +296 -247
  21. package/lib/Socket/registration.js +50 -33
  22. package/lib/Socket/socket.js +872 -1201
  23. package/lib/Store/index.js +5 -5
  24. package/lib/Store/make-cache-manager-store.js +81 -81
  25. package/lib/Store/make-in-memory-store.js +416 -416
  26. package/lib/Store/make-ordered-dictionary.js +81 -81
  27. package/lib/Store/object-repository.js +30 -30
  28. package/lib/Types/Auth.js +1 -1
  29. package/lib/Types/Bussines.js +1 -1
  30. package/lib/Types/Call.js +1 -1
  31. package/lib/Types/Chat.js +7 -7
  32. package/lib/Types/Contact.js +1 -1
  33. package/lib/Types/Events.js +1 -1
  34. package/lib/Types/GroupMetadata.js +1 -1
  35. package/lib/Types/Label.js +24 -24
  36. package/lib/Types/LabelAssociation.js +6 -6
  37. package/lib/Types/Message.js +10 -10
  38. package/lib/Types/Newsletter.js +37 -29
  39. package/lib/Types/Product.js +1 -1
  40. package/lib/Types/Signal.js +1 -1
  41. package/lib/Types/Socket.js +2 -2
  42. package/lib/Types/State.js +55 -12
  43. package/lib/Types/USync.js +1 -1
  44. package/lib/Types/index.js +25 -25
  45. package/lib/Utils/auth-utils.js +264 -256
  46. package/lib/Utils/baileys-event-stream.js +55 -55
  47. package/lib/Utils/browser-utils.js +27 -27
  48. package/lib/Utils/business.js +228 -230
  49. package/lib/Utils/chat-utils.js +726 -764
  50. package/lib/Utils/companion-reg-client-utils.js +34 -0
  51. package/lib/Utils/crypto.js +109 -135
  52. package/lib/Utils/decode-wa-message.js +342 -314
  53. package/lib/Utils/event-buffer.js +547 -547
  54. package/lib/Utils/generics.js +295 -297
  55. package/lib/Utils/history.js +91 -83
  56. package/lib/Utils/index.js +25 -20
  57. package/lib/Utils/key-store.js +17 -0
  58. package/lib/Utils/link-preview.js +107 -98
  59. package/lib/Utils/logger.js +2 -2
  60. package/lib/Utils/lt-hash.js +47 -47
  61. package/lib/Utils/make-mutex.js +39 -39
  62. package/lib/Utils/message-retry-manager.js +148 -148
  63. package/lib/Utils/messages-media.js +579 -535
  64. package/lib/Utils/messages.js +821 -706
  65. package/lib/Utils/noise-handler.js +255 -255
  66. package/lib/Utils/pre-key-manager.js +105 -105
  67. package/lib/Utils/process-message.js +430 -412
  68. package/lib/Utils/reporting-utils.js +155 -0
  69. package/lib/Utils/signal.js +191 -159
  70. package/lib/Utils/sync-action-utils.js +33 -0
  71. package/lib/Utils/tc-token-utils.js +162 -0
  72. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  73. package/lib/Utils/validate-connection.js +194 -194
  74. package/lib/WABinary/constants.js +1306 -1300
  75. package/lib/WABinary/decode.js +237 -237
  76. package/lib/WABinary/encode.js +232 -232
  77. package/lib/WABinary/generic-utils.js +252 -211
  78. package/lib/WABinary/index.js +6 -5
  79. package/lib/WABinary/jid-utils.js +279 -95
  80. package/lib/WABinary/types.js +1 -1
  81. package/lib/WAM/BinaryInfo.js +9 -9
  82. package/lib/WAM/constants.js +22852 -22852
  83. package/lib/WAM/encode.js +149 -149
  84. package/lib/WAM/index.js +3 -3
  85. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  86. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  87. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  88. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  89. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  90. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  91. package/lib/WAUSync/Protocols/index.js +4 -4
  92. package/lib/WAUSync/USyncQuery.js +93 -93
  93. package/lib/WAUSync/USyncUser.js +22 -22
  94. package/lib/WAUSync/index.js +3 -3
  95. package/lib/index.js +65 -66
  96. package/package.json +172 -143
  97. package/lib/Signal/Group/ciphertext-message.js +0 -12
  98. package/lib/Signal/Group/group-session-builder.js +0 -30
  99. package/lib/Signal/Group/group_cipher.js +0 -100
  100. package/lib/Signal/Group/index.js +0 -12
  101. package/lib/Signal/Group/keyhelper.js +0 -18
  102. package/lib/Signal/Group/sender-chain-key.js +0 -26
  103. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  104. package/lib/Signal/Group/sender-key-message.js +0 -66
  105. package/lib/Signal/Group/sender-key-name.js +0 -48
  106. package/lib/Signal/Group/sender-key-record.js +0 -41
  107. package/lib/Signal/Group/sender-key-state.js +0 -84
  108. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,416 +1,416 @@
1
- import WAProto from '../../WAProto/index.js';
2
- import * as Defaults from '../Defaults/index.js';
3
- import { LabelAssociationType } from '../Types/LabelAssociation.js';
4
- import { md5, toNumber } from '../Utils/index.js';
5
- import { jidDecode, jidNormalizedUser } from '../WABinary/index.js';
6
- import { makeOrderedDictionary } from './make-ordered-dictionary.js';
7
- import { ObjectRepository } from './object-repository.js';
8
-
9
- const waChatKey = (pin) => ({
10
- key: (c) => (pin ? (c.pinned ? '1' : '0') : '') + (c.archived ? '0' : '1') + (c.conversationTimestamp ? c.conversationTimestamp.toString(16).padStart(8, '0') : '') + c.id,
11
- compare: (k1, k2) => k2.localeCompare(k1)
12
- });
13
-
14
- const waMessageID = (m) => m.key.id || '';
15
-
16
- const waLabelAssociationKey = {
17
- key: (la) => (la.type === LabelAssociationType.Chat ? la.chatId + la.labelId : la.chatId + la.messageId + la.labelId),
18
- compare: (k1, k2) => k2.localeCompare(k1)
19
- };
20
-
21
- const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID);
22
-
23
- const makeInMemoryStore = (config) => {
24
- const socket = config.socket
25
- const chatKey = config.chatKey || waChatKey(true)
26
- const labelAssociationKey = config.labelAssociationKey || waLabelAssociationKey
27
- const logger = config.logger || Defaults_1.DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
28
- const KeyedDB = require('@adiwajshing/keyed-db').default
29
- const chats = new KeyedDB(chatKey, c => c.id)
30
- const messages = {}
31
- const contacts = {}
32
- const groupMetadata = {}
33
- const presences = {}
34
- const state = { connection: 'close' }
35
- const labels = new object_repository_1.ObjectRepository()
36
- const labelAssociations = new KeyedDB(labelAssociationKey, labelAssociationKey.key)
37
- const assertMessageList = (jid) => {
38
- if (!messages[jid]) {
39
- messages[jid] = makeMessagesDictionary()
40
- }
41
- return messages[jid]
42
- }
43
- const contactsUpsert = (newContacts) => {
44
- const oldContacts = new Set(Object.keys(contacts))
45
- for (const contact of newContacts) {
46
- oldContacts.delete(contact.id)
47
- contacts[contact.id] = Object.assign(contacts[contact.id] || {}, contact)
48
- }
49
- return oldContacts
50
- }
51
- const labelsUpsert = (newLabels) => {
52
- for (const label of newLabels) {
53
- labels.upsertById(label.id, label)
54
- }
55
- }
56
- /**
57
- * binds to a BaileysEventEmitter.
58
- * It listens to all events and constructs a state that you can query accurate data from.
59
- * Eg. can use the store to fetch chats, contacts, messages etc.
60
- * @param ev typically the event emitter from the socket connection
61
- */
62
- const bind = (ev) => {
63
- ev.on('connection.update', update => {
64
- Object.assign(state, update)
65
- })
66
- ev.on('messaging-history.set', ({ chats: newChats, contacts: newContacts, messages: newMessages, isLatest, syncType }) => {
67
- if (syncType === WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
68
- return // FOR NOW,
69
- //TODO: HANDLE
70
- }
71
- if (isLatest) {
72
- chats.clear()
73
- for (const id in messages) {
74
- delete messages[id]
75
- }
76
- }
77
- const chatsAdded = chats.insertIfAbsent(...newChats).length
78
- logger.debug({ chatsAdded }, 'synced chats')
79
- const oldContacts = contactsUpsert(newContacts)
80
- if (isLatest) {
81
- for (const jid of oldContacts) {
82
- delete contacts[jid]
83
- }
84
- }
85
- logger.debug({ deletedContacts: isLatest ? oldContacts.size : 0, newContacts }, 'synced contacts')
86
- for (const msg of newMessages) {
87
- const jid = msg.key.remoteJid
88
- const list = assertMessageList(jid)
89
- list.upsert(msg, 'prepend')
90
- }
91
- logger.debug({ messages: newMessages.length }, 'synced messages')
92
- })
93
- ev.on('contacts.upsert', contacts => {
94
- contactsUpsert(contacts)
95
- })
96
- ev.on('contacts.update', async (updates) => {
97
- for (const update of updates) {
98
- let contact
99
- if (contacts[update.id]) {
100
- contact = contacts[update.id]
101
- }
102
- else {
103
- const contactHashes = await Promise.all(Object.keys(contacts).map(async (contactId) => {
104
- const { user } = WABinary_1.jidDecode(contactId)
105
- return [contactId, (await Utils_1.md5(Buffer.from(user + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)]
106
- }))
107
- contact = contacts[contactHashes.find(([, b]) => b === update.id?.[0]) || ''] // find contact by attrs.hash, when user is not saved as a contact
108
- }
109
- if (contact) {
110
- if (update.imgUrl === 'changed') {
111
- contact.imgUrl = socket ? await socket.profilePictureUrl(contact.id) : undefined
112
- }
113
- else if (update.imgUrl === 'removed') {
114
- delete contact.imgUrl
115
- }
116
- }
117
- else {
118
- return logger.debug({ update }, 'got update for non-existant contact')
119
- }
120
- Object.assign(contacts[contact.id], contact)
121
- }
122
- })
123
- ev.on('chats.upsert', newChats => {
124
- chats.upsert(...newChats)
125
- })
126
- ev.on('chats.update', updates => {
127
- for (let update of updates) {
128
- const result = chats.update(update.id, chat => {
129
- if (update.unreadCount > 0) {
130
- update = { ...update }
131
- update.unreadCount = (chat.unreadCount || 0) + update.unreadCount
132
- }
133
- Object.assign(chat, update)
134
- })
135
- if (!result) {
136
- logger.debug({ update }, 'got update for non-existant chat')
137
- }
138
- }
139
- })
140
- ev.on('labels.edit', (label) => {
141
- if (label.deleted) {
142
- return labels.deleteById(label.id)
143
- }
144
- // WhatsApp can store only up to 20 labels
145
- if (labels.count() < 20) {
146
- return labels.upsertById(label.id, label)
147
- }
148
- logger.error('Labels count exceed')
149
- })
150
- ev.on('labels.association', ({ type, association }) => {
151
- switch (type) {
152
- case 'add':
153
- labelAssociations.upsert(association)
154
- break
155
- case 'remove':
156
- labelAssociations.delete(association)
157
- break
158
- default:
159
- console.error(`unknown operation type [${type}]`)
160
- }
161
- })
162
- ev.on('presence.update', ({ id, presences: update }) => {
163
- presences[id] = presences[id] || {}
164
- Object.assign(presences[id], update)
165
- })
166
- ev.on('chats.delete', deletions => {
167
- for (const item of deletions) {
168
- if (chats.get(item)) {
169
- chats.deleteById(item)
170
- }
171
- }
172
- })
173
- ev.on('messages.upsert', ({ messages: newMessages, type }) => {
174
- switch (type) {
175
- case 'append':
176
- case 'notify':
177
- for (const msg of newMessages) {
178
- const jid = WABinary_1.jidNormalizedUser(msg.key.remoteJid)
179
- const list = assertMessageList(jid)
180
- list.upsert(msg, 'append')
181
- if (type === 'notify' && !chats.get(jid)) {
182
- ev.emit('chats.upsert', [
183
- {
184
- id: jid,
185
- conversationTimestamp: Utils_1.toNumber(msg.messageTimestamp),
186
- unreadCount: 1
187
- }
188
- ])
189
- }
190
- }
191
- break
192
- }
193
- })
194
- ev.on('messages.update', updates => {
195
- for (const { update, key } of updates) {
196
- const list = assertMessageList(WABinary_1.jidNormalizedUser(key.remoteJid))
197
- if (update?.status) {
198
- const listStatus = list.get(key.id)?.status
199
- if (listStatus && update?.status <= listStatus) {
200
- logger.debug({ update, storedStatus: listStatus }, 'status stored newer then update')
201
- delete update.status
202
- logger.debug({ update }, 'new update object')
203
- }
204
- }
205
- const result = list.updateAssign(key.id, update)
206
- if (!result) {
207
- logger.debug({ update }, 'got update for non-existent message')
208
- }
209
- }
210
- })
211
- ev.on('messages.delete', item => {
212
- if ('all' in item) {
213
- const list = messages[item.jid]
214
- list?.clear()
215
- }
216
- else {
217
- const jid = item.keys[0].remoteJid
218
- const list = messages[jid]
219
- if (list) {
220
- const idSet = new Set(item.keys.map(k => k.id))
221
- list.filter(m => !idSet.has(m.key.id))
222
- }
223
- }
224
- })
225
- ev.on('groups.update', updates => {
226
- for (const update of updates) {
227
- const id = update.id
228
- if (groupMetadata[id]) {
229
- Object.assign(groupMetadata[id], update)
230
- }
231
- else {
232
- logger.debug({ update }, 'got update for non-existant group metadata')
233
- }
234
- }
235
- })
236
- ev.on('group-participants.update', ({ id, participants, action }) => {
237
- const metadata = groupMetadata[id]
238
- if (metadata) {
239
- switch (action) {
240
- case 'add':
241
- metadata.participants.push(...participants.map(id => ({ id, isAdmin: false, isSuperAdmin: false })))
242
- break
243
- case 'demote':
244
- case 'promote':
245
- for (const participant of metadata.participants) {
246
- if (participants.includes(participant.id)) {
247
- participant.isAdmin = action === 'promote'
248
- }
249
- }
250
- break
251
- case 'remove':
252
- metadata.participants = metadata.participants.filter(p => !participants.includes(p.id))
253
- break
254
- }
255
- }
256
- })
257
- ev.on('message-receipt.update', updates => {
258
- for (const { key, receipt } of updates) {
259
- const obj = messages[key.remoteJid]
260
- const msg = obj?.get(key.id)
261
- if (msg) {
262
- Utils_1.updateMessageWithReceipt(msg, receipt)
263
- }
264
- }
265
- })
266
- ev.on('messages.reaction', (reactions) => {
267
- for (const { key, reaction } of reactions) {
268
- const obj = messages[key.remoteJid]
269
- const msg = obj?.get(key.id)
270
- if (msg) {
271
- Utils_1.updateMessageWithReaction(msg, reaction)
272
- }
273
- }
274
- })
275
- }
276
- const toJSON = () => ({
277
- chats,
278
- contacts,
279
- messages,
280
- labels,
281
- labelAssociations
282
- })
283
- const fromJSON = (json) => {
284
- chats.upsert(...json.chats)
285
- labelAssociations.upsert(...json.labelAssociations || [])
286
- contactsUpsert(Object.values(json.contacts))
287
- labelsUpsert(Object.values(json.labels || {}))
288
- for (const jid in json.messages) {
289
- const list = assertMessageList(jid)
290
- for (const msg of json.messages[jid]) {
291
- list.upsert(WAProto_1.proto.WebMessageInfo.fromObject(msg), 'append')
292
- }
293
- }
294
- }
295
- return {
296
- chats,
297
- contacts,
298
- messages,
299
- groupMetadata,
300
- state,
301
- presences,
302
- labels,
303
- labelAssociations,
304
- bind,
305
- /** loads messages from the store, if not found -- uses the legacy connection */
306
- loadMessages: async (jid, count, cursor) => {
307
- const list = assertMessageList(jid)
308
- const mode = !cursor || 'before' in cursor ? 'before' : 'after'
309
- const cursorKey = !!cursor ? ('before' in cursor ? cursor.before : cursor.after) : undefined
310
- const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined
311
- let messages
312
- if (list && mode === 'before' && (!cursorKey || cursorValue)) {
313
- if (cursorValue) {
314
- const msgIdx = list.array.findIndex(m => m.key.id === cursorKey?.id)
315
- messages = list.array.slice(0, msgIdx)
316
- }
317
- else {
318
- messages = list.array
319
- }
320
- const diff = count - messages.length
321
- if (diff < 0) {
322
- messages = messages.slice(-count) // get the last X messages
323
- }
324
- }
325
- else {
326
- messages = []
327
- }
328
- return messages
329
- },
330
- /**
331
- * Get all available labels for profile
332
- *
333
- * Keep in mind that the list is formed from predefined tags and tags
334
- * that were "caught" during their editing.
335
- */
336
- getLabels: () => {
337
- return labels
338
- },
339
- /**
340
- * Get labels for chat
341
- *
342
- * @returns Label IDs
343
- **/
344
- getChatLabels: (chatId) => {
345
- return labelAssociations.filter((la) => la.chatId === chatId).all()
346
- },
347
- /**
348
- * Get labels for message
349
- *
350
- * @returns Label IDs
351
- **/
352
- getMessageLabels: (messageId) => {
353
- const associations = labelAssociations
354
- .filter((la) => la.messageId === messageId)
355
- .all()
356
- return associations.map(({ labelId }) => labelId)
357
- },
358
- loadMessage: async (jid, id) => messages[jid]?.get(id),
359
- mostRecentMessage: async (jid) => {
360
- const message = messages[jid]?.array.slice(-1)[0]
361
- return message
362
- },
363
- fetchImageUrl: async (jid, sock) => {
364
- const contact = contacts[jid]
365
- if (!contact) {
366
- return sock?.profilePictureUrl(jid)
367
- }
368
- if (typeof contact.imgUrl === 'undefined') {
369
- contact.imgUrl = await sock?.profilePictureUrl(jid)
370
- }
371
- return contact.imgUrl
372
- },
373
- fetchGroupMetadata: async (jid, sock) => {
374
- if (!groupMetadata[jid]) {
375
- const metadata = await sock?.groupMetadata(jid)
376
- if (metadata) {
377
- groupMetadata[jid] = metadata
378
- }
379
- }
380
- return groupMetadata[jid]
381
- },
382
- // fetchBroadcastListInfo: async(jid: string, sock: WASocket | undefined) => {
383
- // if(!groupMetadata[jid]) {
384
- // const metadata = await sock?.getBroadcastListInfo(jid)
385
- // if(metadata) {
386
- // groupMetadata[jid] = metadata
387
- // }
388
- // }
389
- // return groupMetadata[jid]
390
- // },
391
- fetchMessageReceipts: async ({ remoteJid, id }) => {
392
- const list = messages[remoteJid]
393
- const msg = list?.get(id)
394
- return msg?.userReceipt
395
- },
396
- toJSON,
397
- fromJSON,
398
- writeToFile: (path) => {
399
- // require fs here so that in case "fs" is not available -- the app does not crash
400
- const { writeFileSync } = require('fs')
401
- writeFileSync(path, JSON.stringify(toJSON()))
402
- },
403
- readFromFile: (path) => {
404
- // require fs here so that in case "fs" is not available -- the app does not crash
405
- const { readFileSync, existsSync } = require('fs')
406
- if (existsSync(path)) {
407
- logger.debug({ path }, 'reading from file')
408
- const jsonStr = readFileSync(path, { encoding: 'utf-8' })
409
- const json = JSON.parse(jsonStr)
410
- fromJSON(json)
411
- }
412
- }
413
- }
414
- }
415
-
416
- export { waChatKey, waMessageID, waLabelAssociationKey, makeInMemoryStore };
1
+ import WAProto from '../../WAProto/index.js';
2
+ import * as Defaults from '../Defaults/index.js';
3
+ import { LabelAssociationType } from '../Types/LabelAssociation.js';
4
+ import { md5, toNumber } from '../Utils/index.js';
5
+ import { jidDecode, jidNormalizedUser } from '../WABinary/index.js';
6
+ import { makeOrderedDictionary } from './make-ordered-dictionary.js';
7
+ import { ObjectRepository } from './object-repository.js';
8
+
9
+ const waChatKey = (pin) => ({
10
+ key: (c) => (pin ? (c.pinned ? '1' : '0') : '') + (c.archived ? '0' : '1') + (c.conversationTimestamp ? c.conversationTimestamp.toString(16).padStart(8, '0') : '') + c.id,
11
+ compare: (k1, k2) => k2.localeCompare(k1)
12
+ });
13
+
14
+ const waMessageID = (m) => m.key.id || '';
15
+
16
+ const waLabelAssociationKey = {
17
+ key: (la) => (la.type === LabelAssociationType.Chat ? la.chatId + la.labelId : la.chatId + la.messageId + la.labelId),
18
+ compare: (k1, k2) => k2.localeCompare(k1)
19
+ };
20
+
21
+ const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID);
22
+
23
+ const makeInMemoryStore = (config) => {
24
+ const socket = config.socket
25
+ const chatKey = config.chatKey || waChatKey(true)
26
+ const labelAssociationKey = config.labelAssociationKey || waLabelAssociationKey
27
+ const logger = config.logger || Defaults_1.DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
28
+ const KeyedDB = require('@adiwajshing/keyed-db').default
29
+ const chats = new KeyedDB(chatKey, c => c.id)
30
+ const messages = {}
31
+ const contacts = {}
32
+ const groupMetadata = {}
33
+ const presences = {}
34
+ const state = { connection: 'close' }
35
+ const labels = new object_repository_1.ObjectRepository()
36
+ const labelAssociations = new KeyedDB(labelAssociationKey, labelAssociationKey.key)
37
+ const assertMessageList = (jid) => {
38
+ if (!messages[jid]) {
39
+ messages[jid] = makeMessagesDictionary()
40
+ }
41
+ return messages[jid]
42
+ }
43
+ const contactsUpsert = (newContacts) => {
44
+ const oldContacts = new Set(Object.keys(contacts))
45
+ for (const contact of newContacts) {
46
+ oldContacts.delete(contact.id)
47
+ contacts[contact.id] = Object.assign(contacts[contact.id] || {}, contact)
48
+ }
49
+ return oldContacts
50
+ }
51
+ const labelsUpsert = (newLabels) => {
52
+ for (const label of newLabels) {
53
+ labels.upsertById(label.id, label)
54
+ }
55
+ }
56
+ /**
57
+ * binds to a BaileysEventEmitter.
58
+ * It listens to all events and constructs a state that you can query accurate data from.
59
+ * Eg. can use the store to fetch chats, contacts, messages etc.
60
+ * @param ev typically the event emitter from the socket connection
61
+ */
62
+ const bind = (ev) => {
63
+ ev.on('connection.update', update => {
64
+ Object.assign(state, update)
65
+ })
66
+ ev.on('messaging-history.set', ({ chats: newChats, contacts: newContacts, messages: newMessages, isLatest, syncType }) => {
67
+ if (syncType === WAProto_1.proto.HistorySync.HistorySyncType.ON_DEMAND) {
68
+ return // FOR NOW,
69
+ //TODO: HANDLE
70
+ }
71
+ if (isLatest) {
72
+ chats.clear()
73
+ for (const id in messages) {
74
+ delete messages[id]
75
+ }
76
+ }
77
+ const chatsAdded = chats.insertIfAbsent(...newChats).length
78
+ logger.debug({ chatsAdded }, 'synced chats')
79
+ const oldContacts = contactsUpsert(newContacts)
80
+ if (isLatest) {
81
+ for (const jid of oldContacts) {
82
+ delete contacts[jid]
83
+ }
84
+ }
85
+ logger.debug({ deletedContacts: isLatest ? oldContacts.size : 0, newContacts }, 'synced contacts')
86
+ for (const msg of newMessages) {
87
+ const jid = msg.key.remoteJid
88
+ const list = assertMessageList(jid)
89
+ list.upsert(msg, 'prepend')
90
+ }
91
+ logger.debug({ messages: newMessages.length }, 'synced messages')
92
+ })
93
+ ev.on('contacts.upsert', contacts => {
94
+ contactsUpsert(contacts)
95
+ })
96
+ ev.on('contacts.update', async (updates) => {
97
+ for (const update of updates) {
98
+ let contact
99
+ if (contacts[update.id]) {
100
+ contact = contacts[update.id]
101
+ }
102
+ else {
103
+ const contactHashes = await Promise.all(Object.keys(contacts).map(async (contactId) => {
104
+ const { user } = WABinary_1.jidDecode(contactId)
105
+ return [contactId, (await Utils_1.md5(Buffer.from(user + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)]
106
+ }))
107
+ contact = contacts[contactHashes.find(([, b]) => b === update.id?.[0]) || ''] // find contact by attrs.hash, when user is not saved as a contact
108
+ }
109
+ if (contact) {
110
+ if (update.imgUrl === 'changed') {
111
+ contact.imgUrl = socket ? await socket.profilePictureUrl(contact.id) : undefined
112
+ }
113
+ else if (update.imgUrl === 'removed') {
114
+ delete contact.imgUrl
115
+ }
116
+ }
117
+ else {
118
+ return logger.debug({ update }, 'got update for non-existant contact')
119
+ }
120
+ Object.assign(contacts[contact.id], contact)
121
+ }
122
+ })
123
+ ev.on('chats.upsert', newChats => {
124
+ chats.upsert(...newChats)
125
+ })
126
+ ev.on('chats.update', updates => {
127
+ for (let update of updates) {
128
+ const result = chats.update(update.id, chat => {
129
+ if (update.unreadCount > 0) {
130
+ update = { ...update }
131
+ update.unreadCount = (chat.unreadCount || 0) + update.unreadCount
132
+ }
133
+ Object.assign(chat, update)
134
+ })
135
+ if (!result) {
136
+ logger.debug({ update }, 'got update for non-existant chat')
137
+ }
138
+ }
139
+ })
140
+ ev.on('labels.edit', (label) => {
141
+ if (label.deleted) {
142
+ return labels.deleteById(label.id)
143
+ }
144
+ // WhatsApp can store only up to 20 labels
145
+ if (labels.count() < 20) {
146
+ return labels.upsertById(label.id, label)
147
+ }
148
+ logger.error('Labels count exceed')
149
+ })
150
+ ev.on('labels.association', ({ type, association }) => {
151
+ switch (type) {
152
+ case 'add':
153
+ labelAssociations.upsert(association)
154
+ break
155
+ case 'remove':
156
+ labelAssociations.delete(association)
157
+ break
158
+ default:
159
+ console.error(`unknown operation type [${type}]`)
160
+ }
161
+ })
162
+ ev.on('presence.update', ({ id, presences: update }) => {
163
+ presences[id] = presences[id] || {}
164
+ Object.assign(presences[id], update)
165
+ })
166
+ ev.on('chats.delete', deletions => {
167
+ for (const item of deletions) {
168
+ if (chats.get(item)) {
169
+ chats.deleteById(item)
170
+ }
171
+ }
172
+ })
173
+ ev.on('messages.upsert', ({ messages: newMessages, type }) => {
174
+ switch (type) {
175
+ case 'append':
176
+ case 'notify':
177
+ for (const msg of newMessages) {
178
+ const jid = WABinary_1.jidNormalizedUser(msg.key.remoteJid)
179
+ const list = assertMessageList(jid)
180
+ list.upsert(msg, 'append')
181
+ if (type === 'notify' && !chats.get(jid)) {
182
+ ev.emit('chats.upsert', [
183
+ {
184
+ id: jid,
185
+ conversationTimestamp: Utils_1.toNumber(msg.messageTimestamp),
186
+ unreadCount: 1
187
+ }
188
+ ])
189
+ }
190
+ }
191
+ break
192
+ }
193
+ })
194
+ ev.on('messages.update', updates => {
195
+ for (const { update, key } of updates) {
196
+ const list = assertMessageList(WABinary_1.jidNormalizedUser(key.remoteJid))
197
+ if (update?.status) {
198
+ const listStatus = list.get(key.id)?.status
199
+ if (listStatus && update?.status <= listStatus) {
200
+ logger.debug({ update, storedStatus: listStatus }, 'status stored newer then update')
201
+ delete update.status
202
+ logger.debug({ update }, 'new update object')
203
+ }
204
+ }
205
+ const result = list.updateAssign(key.id, update)
206
+ if (!result) {
207
+ logger.debug({ update }, 'got update for non-existent message')
208
+ }
209
+ }
210
+ })
211
+ ev.on('messages.delete', item => {
212
+ if ('all' in item) {
213
+ const list = messages[item.jid]
214
+ list?.clear()
215
+ }
216
+ else {
217
+ const jid = item.keys[0].remoteJid
218
+ const list = messages[jid]
219
+ if (list) {
220
+ const idSet = new Set(item.keys.map(k => k.id))
221
+ list.filter(m => !idSet.has(m.key.id))
222
+ }
223
+ }
224
+ })
225
+ ev.on('groups.update', updates => {
226
+ for (const update of updates) {
227
+ const id = update.id
228
+ if (groupMetadata[id]) {
229
+ Object.assign(groupMetadata[id], update)
230
+ }
231
+ else {
232
+ logger.debug({ update }, 'got update for non-existant group metadata')
233
+ }
234
+ }
235
+ })
236
+ ev.on('group-participants.update', ({ id, participants, action }) => {
237
+ const metadata = groupMetadata[id]
238
+ if (metadata) {
239
+ switch (action) {
240
+ case 'add':
241
+ metadata.participants.push(...participants.map(id => ({ id, isAdmin: false, isSuperAdmin: false })))
242
+ break
243
+ case 'demote':
244
+ case 'promote':
245
+ for (const participant of metadata.participants) {
246
+ if (participants.includes(participant.id)) {
247
+ participant.isAdmin = action === 'promote'
248
+ }
249
+ }
250
+ break
251
+ case 'remove':
252
+ metadata.participants = metadata.participants.filter(p => !participants.includes(p.id))
253
+ break
254
+ }
255
+ }
256
+ })
257
+ ev.on('message-receipt.update', updates => {
258
+ for (const { key, receipt } of updates) {
259
+ const obj = messages[key.remoteJid]
260
+ const msg = obj?.get(key.id)
261
+ if (msg) {
262
+ Utils_1.updateMessageWithReceipt(msg, receipt)
263
+ }
264
+ }
265
+ })
266
+ ev.on('messages.reaction', (reactions) => {
267
+ for (const { key, reaction } of reactions) {
268
+ const obj = messages[key.remoteJid]
269
+ const msg = obj?.get(key.id)
270
+ if (msg) {
271
+ Utils_1.updateMessageWithReaction(msg, reaction)
272
+ }
273
+ }
274
+ })
275
+ }
276
+ const toJSON = () => ({
277
+ chats,
278
+ contacts,
279
+ messages,
280
+ labels,
281
+ labelAssociations
282
+ })
283
+ const fromJSON = (json) => {
284
+ chats.upsert(...json.chats)
285
+ labelAssociations.upsert(...json.labelAssociations || [])
286
+ contactsUpsert(Object.values(json.contacts))
287
+ labelsUpsert(Object.values(json.labels || {}))
288
+ for (const jid in json.messages) {
289
+ const list = assertMessageList(jid)
290
+ for (const msg of json.messages[jid]) {
291
+ list.upsert(WAProto_1.proto.WebMessageInfo.fromObject(msg), 'append')
292
+ }
293
+ }
294
+ }
295
+ return {
296
+ chats,
297
+ contacts,
298
+ messages,
299
+ groupMetadata,
300
+ state,
301
+ presences,
302
+ labels,
303
+ labelAssociations,
304
+ bind,
305
+ /** loads messages from the store, if not found -- uses the legacy connection */
306
+ loadMessages: async (jid, count, cursor) => {
307
+ const list = assertMessageList(jid)
308
+ const mode = !cursor || 'before' in cursor ? 'before' : 'after'
309
+ const cursorKey = !!cursor ? ('before' in cursor ? cursor.before : cursor.after) : undefined
310
+ const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined
311
+ let messages
312
+ if (list && mode === 'before' && (!cursorKey || cursorValue)) {
313
+ if (cursorValue) {
314
+ const msgIdx = list.array.findIndex(m => m.key.id === cursorKey?.id)
315
+ messages = list.array.slice(0, msgIdx)
316
+ }
317
+ else {
318
+ messages = list.array
319
+ }
320
+ const diff = count - messages.length
321
+ if (diff < 0) {
322
+ messages = messages.slice(-count) // get the last X messages
323
+ }
324
+ }
325
+ else {
326
+ messages = []
327
+ }
328
+ return messages
329
+ },
330
+ /**
331
+ * Get all available labels for profile
332
+ *
333
+ * Keep in mind that the list is formed from predefined tags and tags
334
+ * that were "caught" during their editing.
335
+ */
336
+ getLabels: () => {
337
+ return labels
338
+ },
339
+ /**
340
+ * Get labels for chat
341
+ *
342
+ * @returns Label IDs
343
+ **/
344
+ getChatLabels: (chatId) => {
345
+ return labelAssociations.filter((la) => la.chatId === chatId).all()
346
+ },
347
+ /**
348
+ * Get labels for message
349
+ *
350
+ * @returns Label IDs
351
+ **/
352
+ getMessageLabels: (messageId) => {
353
+ const associations = labelAssociations
354
+ .filter((la) => la.messageId === messageId)
355
+ .all()
356
+ return associations.map(({ labelId }) => labelId)
357
+ },
358
+ loadMessage: async (jid, id) => messages[jid]?.get(id),
359
+ mostRecentMessage: async (jid) => {
360
+ const message = messages[jid]?.array.slice(-1)[0]
361
+ return message
362
+ },
363
+ fetchImageUrl: async (jid, sock) => {
364
+ const contact = contacts[jid]
365
+ if (!contact) {
366
+ return sock?.profilePictureUrl(jid)
367
+ }
368
+ if (typeof contact.imgUrl === 'undefined') {
369
+ contact.imgUrl = await sock?.profilePictureUrl(jid)
370
+ }
371
+ return contact.imgUrl
372
+ },
373
+ fetchGroupMetadata: async (jid, sock) => {
374
+ if (!groupMetadata[jid]) {
375
+ const metadata = await sock?.groupMetadata(jid)
376
+ if (metadata) {
377
+ groupMetadata[jid] = metadata
378
+ }
379
+ }
380
+ return groupMetadata[jid]
381
+ },
382
+ // fetchBroadcastListInfo: async(jid: string, sock: WASocket | undefined) => {
383
+ // if(!groupMetadata[jid]) {
384
+ // const metadata = await sock?.getBroadcastListInfo(jid)
385
+ // if(metadata) {
386
+ // groupMetadata[jid] = metadata
387
+ // }
388
+ // }
389
+ // return groupMetadata[jid]
390
+ // },
391
+ fetchMessageReceipts: async ({ remoteJid, id }) => {
392
+ const list = messages[remoteJid]
393
+ const msg = list?.get(id)
394
+ return msg?.userReceipt
395
+ },
396
+ toJSON,
397
+ fromJSON,
398
+ writeToFile: (path) => {
399
+ // require fs here so that in case "fs" is not available -- the app does not crash
400
+ const { writeFileSync } = require('fs')
401
+ writeFileSync(path, JSON.stringify(toJSON()))
402
+ },
403
+ readFromFile: (path) => {
404
+ // require fs here so that in case "fs" is not available -- the app does not crash
405
+ const { readFileSync, existsSync } = require('fs')
406
+ if (existsSync(path)) {
407
+ logger.debug({ path }, 'reading from file')
408
+ const jsonStr = readFileSync(path, { encoding: 'utf-8' })
409
+ const json = JSON.parse(jsonStr)
410
+ fromJSON(json)
411
+ }
412
+ }
413
+ }
414
+ }
415
+
416
+ export { waChatKey, waMessageID, waLabelAssociationKey, makeInMemoryStore };