@queenanya/baileys 6.7.0 → 6.8.1

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