@prototypevip/baileys 0.0.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 (118) hide show
  1. package/LICENSE +23 -0
  2. package/WAProto/index.js +131118 -0
  3. package/engine-requirements.js +1 -0
  4. package/lib/Defaults/index.js +187 -0
  5. package/lib/Signal/Group/ciphertext-message.js +11 -0
  6. package/lib/Signal/Group/group-session-builder.js +41 -0
  7. package/lib/Signal/Group/group_cipher.js +108 -0
  8. package/lib/Signal/Group/index.js +11 -0
  9. package/lib/Signal/Group/keyhelper.js +14 -0
  10. package/lib/Signal/Group/sender-chain-key.js +31 -0
  11. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  12. package/lib/Signal/Group/sender-key-message.js +79 -0
  13. package/lib/Signal/Group/sender-key-name.js +49 -0
  14. package/lib/Signal/Group/sender-key-record.js +46 -0
  15. package/lib/Signal/Group/sender-key-state.js +104 -0
  16. package/lib/Signal/Group/sender-message-key.js +29 -0
  17. package/lib/Signal/libsignal.js +532 -0
  18. package/lib/Signal/lid-mapping.js +312 -0
  19. package/lib/Socket/Client/index.js +2 -0
  20. package/lib/Socket/Client/types.js +10 -0
  21. package/lib/Socket/Client/websocket.js +64 -0
  22. package/lib/Socket/business.js +293 -0
  23. package/lib/Socket/chats.js +1124 -0
  24. package/lib/Socket/communities.js +528 -0
  25. package/lib/Socket/groups.js +465 -0
  26. package/lib/Socket/index.js +14 -0
  27. package/lib/Socket/messages-recv.js +2325 -0
  28. package/lib/Socket/messages-send.js +1746 -0
  29. package/lib/Socket/mex.js +55 -0
  30. package/lib/Socket/newsletter.js +317 -0
  31. package/lib/Socket/socket.js +1185 -0
  32. package/lib/Store/cache-factory.js +20 -0
  33. package/lib/Store/index.js +5 -0
  34. package/lib/Store/make-in-memory-store.js +523 -0
  35. package/lib/Store/make-ordered-dictionary.js +203 -0
  36. package/lib/Store/object-repository.js +23 -0
  37. package/lib/Store/prototype-store.js +958 -0
  38. package/lib/Types/Auth.js +1 -0
  39. package/lib/Types/Bussines.js +1 -0
  40. package/lib/Types/Call.js +1 -0
  41. package/lib/Types/Chat.js +7 -0
  42. package/lib/Types/Contact.js +1 -0
  43. package/lib/Types/Events.js +1 -0
  44. package/lib/Types/GroupMetadata.js +1 -0
  45. package/lib/Types/Label.js +24 -0
  46. package/lib/Types/LabelAssociation.js +6 -0
  47. package/lib/Types/Message.js +18 -0
  48. package/lib/Types/Mex.js +37 -0
  49. package/lib/Types/Newsletter.js +36 -0
  50. package/lib/Types/Product.js +1 -0
  51. package/lib/Types/RichType.js +25 -0
  52. package/lib/Types/Signal.js +1 -0
  53. package/lib/Types/Socket.js +2 -0
  54. package/lib/Types/State.js +78 -0
  55. package/lib/Types/USync.js +1 -0
  56. package/lib/Types/index.js +33 -0
  57. package/lib/Utils/auth-utils.js +303 -0
  58. package/lib/Utils/browser-utils.js +32 -0
  59. package/lib/Utils/business.js +245 -0
  60. package/lib/Utils/chat-utils.js +971 -0
  61. package/lib/Utils/companion-reg-client-utils.js +54 -0
  62. package/lib/Utils/constants.js +24 -0
  63. package/lib/Utils/crypto.js +133 -0
  64. package/lib/Utils/cstoken-utils.js +41 -0
  65. package/lib/Utils/decode-wa-message.js +406 -0
  66. package/lib/Utils/event-buffer.js +616 -0
  67. package/lib/Utils/generics.js +438 -0
  68. package/lib/Utils/history.js +147 -0
  69. package/lib/Utils/identity-change-handler.js +63 -0
  70. package/lib/Utils/index.js +29 -0
  71. package/lib/Utils/interactive-message-handler.js +478 -0
  72. package/lib/Utils/link-preview.js +91 -0
  73. package/lib/Utils/logger.js +2 -0
  74. package/lib/Utils/lt-hash.js +6 -0
  75. package/lib/Utils/make-mutex.js +31 -0
  76. package/lib/Utils/message-retry-manager.js +291 -0
  77. package/lib/Utils/messages-media.js +881 -0
  78. package/lib/Utils/messages.js +1434 -0
  79. package/lib/Utils/noise-handler.js +229 -0
  80. package/lib/Utils/normalize-message-identity.js +73 -0
  81. package/lib/Utils/offline-node-processor.js +50 -0
  82. package/lib/Utils/pre-key-manager.js +119 -0
  83. package/lib/Utils/process-message.js +648 -0
  84. package/lib/Utils/reporting-utils.js +355 -0
  85. package/lib/Utils/rich-message-utils.js +264 -0
  86. package/lib/Utils/signal.js +233 -0
  87. package/lib/Utils/stanza-ack.js +33 -0
  88. package/lib/Utils/sticker-utils.js +221 -0
  89. package/lib/Utils/sync-action-utils.js +53 -0
  90. package/lib/Utils/tc-token-utils.js +182 -0
  91. package/lib/Utils/use-multi-file-auth-state.js +118 -0
  92. package/lib/Utils/use-prototype-auth-state.js +217 -0
  93. package/lib/Utils/use-single-file-auth-state.js +94 -0
  94. package/lib/Utils/validate-connection.js +235 -0
  95. package/lib/WABinary/constants.js +1468 -0
  96. package/lib/WABinary/decode.js +258 -0
  97. package/lib/WABinary/encode.js +219 -0
  98. package/lib/WABinary/generic-utils.js +202 -0
  99. package/lib/WABinary/index.js +5 -0
  100. package/lib/WABinary/jid-utils.js +94 -0
  101. package/lib/WABinary/types.js +1 -0
  102. package/lib/WAM/BinaryInfo.js +9 -0
  103. package/lib/WAM/constants.js +20669 -0
  104. package/lib/WAM/encode.js +151 -0
  105. package/lib/WAM/index.js +3 -0
  106. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
  107. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -0
  108. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
  109. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
  110. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  111. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
  112. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
  113. package/lib/WAUSync/Protocols/index.js +4 -0
  114. package/lib/WAUSync/USyncQuery.js +133 -0
  115. package/lib/WAUSync/USyncUser.js +22 -0
  116. package/lib/WAUSync/index.js +3 -0
  117. package/lib/index.js +12 -0
  118. package/package.json +78 -0
@@ -0,0 +1,20 @@
1
+ import NodeCache from "@cacheable/node-cache";
2
+ import { DEFAULT_CACHE_TTLS } from "../Defaults/index.js";
3
+
4
+ /**
5
+ * Default cache for group metadata.
6
+ * Exported so users can manually manage it:
7
+ * import { groupMetadataCache } from '@prototypevip/baileys'
8
+ */
9
+ export const groupMetadataCache = new NodeCache({
10
+ stdTTL: DEFAULT_CACHE_TTLS.GROUP_METADATA,
11
+ useClones: false,
12
+ });
13
+
14
+ /**
15
+ * Default cache for profile picture URLs.
16
+ */
17
+ export const profilePictureCache = new NodeCache({
18
+ stdTTL: 300,
19
+ useClones: false,
20
+ });
@@ -0,0 +1,5 @@
1
+ export * from "./prototype-store.js";
2
+ export * from "./make-in-memory-store.js";
3
+ export * from "./make-ordered-dictionary.js";
4
+ export * from "./object-repository.js";
5
+ export * from "./cache-factory.js";
@@ -0,0 +1,523 @@
1
+ /**
2
+
3
+ * --- [WIP]
4
+ * Manually converted to ESM and modified by me
5
+ * Originally based on @whiskeysockets/baileys v6.7.16
6
+ * Minor adjustments for compatibility with baileys v7
7
+ */
8
+ import keyedDB from "@adiwajshing/keyed-db";
9
+ import { writeFileSync, readFileSync, existsSync } from "fs";
10
+ import { WAProto } from "../Types/index.js";
11
+ import { LabelAssociationType } from "../Types/LabelAssociation.js";
12
+ import { DEFAULT_CONNECTION_CONFIG } from "../Defaults/index.js";
13
+ import {
14
+ md5,
15
+ toNumber,
16
+ updateMessageWithReceipt,
17
+ updateMessageWithReaction,
18
+ } from "../Utils/index.js";
19
+ import { jidDecode, jidNormalizedUser } from "../WABinary/index.js";
20
+ import { makeOrderedDictionary } from "./make-ordered-dictionary.js";
21
+ import { ObjectRepository } from "./object-repository.js";
22
+ export const waChatKey = (pin) => ({
23
+ key: (c) =>
24
+ (pin ? (c.pinned ? "1" : "0") : "") +
25
+ (c.archived ? "0" : "1") +
26
+ (c.conversationTimestamp
27
+ ? c.conversationTimestamp.toString(16).padStart(8, "0")
28
+ : "") +
29
+ c.id,
30
+ compare: (k1, k2) => k2.localeCompare(k1),
31
+ });
32
+ export const waMessageID = (m) => m.key.id || "";
33
+ export const waLabelAssociationKey = {
34
+ key: (la) =>
35
+ la.type === LabelAssociationType.Chat
36
+ ? la.chatId + la.labelId
37
+ : la.chatId + la.messageId + la.labelId,
38
+ compare: (k1, k2) => k2.localeCompare(k1),
39
+ };
40
+ const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID);
41
+ export const makeInMemoryStore = (config = {}) => {
42
+ const socket = config.socket;
43
+ const chatKey = config.chatKey || waChatKey(true);
44
+ const labelAssociationKey =
45
+ config.labelAssociationKey || waLabelAssociationKey;
46
+ const logger =
47
+ config.logger ||
48
+ DEFAULT_CONNECTION_CONFIG.logger.child({ stream: "in-mem-store" });
49
+ const KeyedDB = keyedDB?.default ?? keyedDB;
50
+ const chats = new KeyedDB(chatKey, (c) => c.id);
51
+ const messages = {};
52
+ const contacts = {};
53
+ const groupMetadata = {};
54
+ const presences = {};
55
+ const state = { connection: "close" };
56
+ const labels = new ObjectRepository();
57
+ const labelAssociations = new KeyedDB(
58
+ labelAssociationKey,
59
+ labelAssociationKey.key,
60
+ );
61
+ const assertMessageList = (jid) => {
62
+ if (!messages[jid]) {
63
+ messages[jid] = makeMessagesDictionary();
64
+ }
65
+ return messages[jid];
66
+ };
67
+ const contactsUpsert = (newContacts) => {
68
+ const oldContacts = new Set(Object.keys(contacts));
69
+ for (const contact of newContacts) {
70
+ oldContacts.delete(contact.id);
71
+ contacts[contact.id] = Object.assign(contacts[contact.id] || {}, contact);
72
+ }
73
+ return oldContacts;
74
+ };
75
+ const labelsUpsert = (newLabels) => {
76
+ for (const label of newLabels) {
77
+ labels.upsertById(label.id, label);
78
+ }
79
+ };
80
+ /**
81
+ * binds to a BaileysEventEmitter.
82
+ * It listens to all events and constructs a state that you can query accurate data from.
83
+ * Eg. can use the store to fetch chats, contacts, messages etc.
84
+ * @param ev typically the event emitter from the socket connection
85
+ */ const bind = (ev) => {
86
+ ev.on("connection.update", (update) => {
87
+ Object.assign(state, update);
88
+ });
89
+ ev.on(
90
+ "messaging-history.set",
91
+ ({
92
+ chats: newChats,
93
+ contacts: newContacts,
94
+ messages: newMessages,
95
+ isLatest,
96
+ syncType,
97
+ }) => {
98
+ if (syncType === WAProto.HistorySync.HistorySyncType.ON_DEMAND) {
99
+ chats.upsert(...newChats);
100
+ contactsUpsert(newContacts);
101
+ for (const msg of newMessages) {
102
+ const jid = msg.key.remoteJid;
103
+ const list = assertMessageList(jid);
104
+ list.upsert(msg, "append");
105
+ }
106
+ return;
107
+ }
108
+ if (isLatest) {
109
+ chats.clear();
110
+ for (const id in messages) {
111
+ delete messages[id];
112
+ }
113
+ }
114
+ const chatsAdded = chats.insertIfAbsent(...newChats).length;
115
+ logger.debug({ chatsAdded: chatsAdded }, "synced chats");
116
+ const oldContacts = contactsUpsert(newContacts);
117
+ if (isLatest) {
118
+ for (const jid of oldContacts) {
119
+ delete contacts[jid];
120
+ }
121
+ }
122
+ logger.debug(
123
+ {
124
+ deletedContacts: isLatest ? oldContacts.size : 0,
125
+ newContacts: newContacts,
126
+ },
127
+ "synced contacts",
128
+ );
129
+ for (const msg of newMessages) {
130
+ const jid = msg.key.remoteJidAlt || msg.key.remoteJid;
131
+ const list = assertMessageList(jid);
132
+ list.upsert(msg, "prepend");
133
+ }
134
+ logger.debug({ messages: newMessages.length }, "synced messages");
135
+ },
136
+ );
137
+ ev.on("contacts.upsert", (contacts) => {
138
+ contactsUpsert(contacts);
139
+ });
140
+ ev.on("contacts.update", async (updates) => {
141
+ var _a;
142
+ for (const update of updates) {
143
+ let contact;
144
+ if (contacts[update.id]) {
145
+ contact = contacts[update.id];
146
+ } else {
147
+ const contactHashes = await Promise.all(
148
+ Object.keys(contacts).map(async (contactId) => {
149
+ const { user } = jidDecode(contactId);
150
+ return [
151
+ contactId,
152
+ (await md5(Buffer.from(user + "WA_ADD_NOTIF", "utf8")))
153
+ .toString("base64")
154
+ .slice(0, 3),
155
+ ];
156
+ }),
157
+ );
158
+ contact =
159
+ contacts[
160
+ ((_a = contactHashes.find(([, b]) => b === update.id)) === null ||
161
+ _a === void 0
162
+ ? void 0
163
+ : _a[0]) || ""
164
+ ]; // find contact by attrs.hash, when user is not saved as a contact
165
+ }
166
+ if (contact) {
167
+ if (update.imgUrl === "changed") {
168
+ contact.imgUrl = socket
169
+ ? await (socket === null || socket === void 0
170
+ ? void 0
171
+ : socket.profilePictureUrl(contact.id))
172
+ : undefined;
173
+ } else if (update.imgUrl === "removed") {
174
+ delete contact.imgUrl;
175
+ }
176
+ } else {
177
+ return logger.debug(
178
+ { update: update },
179
+ "got update for non-existant contact",
180
+ );
181
+ }
182
+ Object.assign(contacts[contact.id], contact);
183
+ }
184
+ });
185
+ ev.on("chats.upsert", (newChats) => {
186
+ chats.upsert(...newChats);
187
+ });
188
+ ev.on("chats.update", (updates) => {
189
+ for (let update of updates) {
190
+ const result = chats.update(update.id, (chat) => {
191
+ if (update.unreadCount > 0) {
192
+ update = { ...update };
193
+ update.unreadCount = (chat.unreadCount || 0) + update.unreadCount;
194
+ }
195
+ Object.assign(chat, update);
196
+ });
197
+ if (!result) {
198
+ logger.debug({ update: update }, "got update for non-existant chat");
199
+ }
200
+ }
201
+ });
202
+ ev.on("labels.edit", (label) => {
203
+ if (label.deleted) {
204
+ return labels.deleteById(label.id);
205
+ }
206
+ // WhatsApp can store only up to 20 labels
207
+ if (labels.count() < 20) {
208
+ return labels.upsertById(label.id, label);
209
+ }
210
+ logger.error("Labels count exceed");
211
+ });
212
+ ev.on("labels.association", ({ type, association }) => {
213
+ switch (type) {
214
+ case "add":
215
+ labelAssociations.upsert(association);
216
+ break;
217
+ case "remove":
218
+ labelAssociations.delete(association);
219
+ break;
220
+ default:
221
+ console.error(`unknown operation type [${type}]`);
222
+ }
223
+ });
224
+ ev.on("presence.update", ({ id, presences: update }) => {
225
+ presences[id] = presences[id] || {};
226
+ Object.assign(presences[id], update);
227
+ });
228
+ ev.on("chats.delete", (deletions) => {
229
+ for (const item of deletions) {
230
+ if (chats.get(item)) {
231
+ chats.deleteById(item);
232
+ }
233
+ }
234
+ });
235
+ ev.on("messages.upsert", ({ messages: newMessages, type }) => {
236
+ switch (type) {
237
+ case "append":
238
+ case "notify":
239
+ for (const msg of newMessages) {
240
+ const jid = jidNormalizedUser(
241
+ msg.key.remoteJidAlt || msg.key.remoteJid,
242
+ );
243
+ const list = assertMessageList(jid);
244
+ list.upsert(msg, "append");
245
+ if (type === "notify" && !chats.get(jid)) {
246
+ ev.emit("chats.upsert", [
247
+ {
248
+ id: jid,
249
+ conversationTimestamp: toNumber(msg.messageTimestamp),
250
+ unreadCount: 1,
251
+ },
252
+ ]);
253
+ }
254
+ }
255
+ break;
256
+ }
257
+ });
258
+ ev.on("messages.update", (updates) => {
259
+ var _a;
260
+ for (const { update, key } of updates) {
261
+ const list = assertMessageList(jidNormalizedUser(key.remoteJid));
262
+ if (update === null || update === void 0 ? void 0 : update.status) {
263
+ const listStatus =
264
+ (_a = list.get(key.id)) === null || _a === void 0
265
+ ? void 0
266
+ : _a.status;
267
+ if (
268
+ listStatus &&
269
+ (update === null || update === void 0 ? void 0 : update.status) <=
270
+ listStatus
271
+ ) {
272
+ logger.debug(
273
+ { update: update, storedStatus: listStatus },
274
+ "status stored newer then update",
275
+ );
276
+ delete update.status;
277
+ logger.debug({ update: update }, "new update object");
278
+ }
279
+ }
280
+ const result = list.updateAssign(key.id, update);
281
+ if (!result) {
282
+ logger.debug(
283
+ { update: update },
284
+ "got update for non-existent message",
285
+ );
286
+ }
287
+ }
288
+ });
289
+ ev.on("messages.delete", (item) => {
290
+ if ("all" in item) {
291
+ const list = messages[item.jid];
292
+ list === null || list === void 0 ? void 0 : list.clear();
293
+ } else {
294
+ const jid = item.keys[0].remoteJidAlt || item.keys[0].remoteJid;
295
+ const list = messages[jid];
296
+ if (list) {
297
+ const idSet = new Set(item.keys.map((k) => k.id));
298
+ list.filter((m) => !idSet.has(m.key.id));
299
+ }
300
+ }
301
+ });
302
+ ev.on("groups.update", (updates) => {
303
+ for (const update of updates) {
304
+ const id = update.id;
305
+ if (groupMetadata[id]) {
306
+ Object.assign(groupMetadata[id], update);
307
+ } else {
308
+ logger.debug(
309
+ { update: update },
310
+ "got update for non-existant group metadata",
311
+ );
312
+ }
313
+ }
314
+ });
315
+ ev.on("group-participants.update", ({ id, participants, action }) => {
316
+ const metadata = groupMetadata[id];
317
+ if (metadata) {
318
+ switch (action) {
319
+ case "add":
320
+ metadata.participants.push(
321
+ ...participants.map((participant) => ({
322
+ id: participant.id,
323
+ phoneNumber: participant.phoneNumber,
324
+ admin: participant.admin,
325
+ })),
326
+ );
327
+ break;
328
+ case "demote":
329
+ case "promote":
330
+ for (const participant of metadata.participants) {
331
+ for (const participantData of participants) {
332
+ if (
333
+ participantData.id === participant.id ||
334
+ participantData.phoneNumber === participant.phoneNumber
335
+ ) {
336
+ participant.admin = action === "promote" && "admin";
337
+ }
338
+ }
339
+ }
340
+ break;
341
+ case "remove":
342
+ const removeSet = new Set();
343
+ for (const p of participants) {
344
+ if (p.id) removeSet.add(p.id);
345
+ if (p.phoneNumber) removeSet.add(p.phoneNumber);
346
+ }
347
+ metadata.participants = metadata.participants.reduce((acc, p) => {
348
+ if (!removeSet.has(p.id) && !removeSet.has(p.phoneNumber))
349
+ acc.push(p);
350
+ return acc;
351
+ }, []);
352
+ break;
353
+ }
354
+ }
355
+ });
356
+ ev.on("message-receipt.update", (updates) => {
357
+ for (const { key, receipt } of updates) {
358
+ const obj = messages[key.remoteJidAlt || key.remoteJid];
359
+ const msg = obj === null || obj === void 0 ? void 0 : obj.get(key.id);
360
+ if (msg) {
361
+ updateMessageWithReceipt(msg, receipt);
362
+ }
363
+ }
364
+ });
365
+ ev.on("messages.reaction", (reactions) => {
366
+ for (const { key, reaction } of reactions) {
367
+ const obj = messages[key.remoteJidAlt || key.remoteJid];
368
+ const msg = obj === null || obj === void 0 ? void 0 : obj.get(key.id);
369
+ if (msg) {
370
+ updateMessageWithReaction(msg, reaction);
371
+ }
372
+ }
373
+ });
374
+ };
375
+ const toJSON = () => ({
376
+ chats: chats,
377
+ contacts: contacts,
378
+ messages: messages,
379
+ labels: labels,
380
+ labelAssociations: labelAssociations,
381
+ });
382
+ const fromJSON = (json) => {
383
+ chats.upsert(...json.chats);
384
+ labelAssociations.upsert(...(json.labelAssociations || []));
385
+ contactsUpsert(Object.values(json.contacts));
386
+ labelsUpsert(Object.values(json.labels || {}));
387
+ for (const jid in json.messages) {
388
+ const list = assertMessageList(jid);
389
+ for (const msg of json.messages[jid]) {
390
+ list.upsert(WAProto.WebMessageInfo.fromObject(msg), "append");
391
+ }
392
+ }
393
+ };
394
+ return {
395
+ chats: chats,
396
+ contacts: contacts,
397
+ messages: messages,
398
+ groupMetadata: groupMetadata,
399
+ state: state,
400
+ presences: presences,
401
+ labels: labels,
402
+ labelAssociations: labelAssociations,
403
+ bind: bind,
404
+ /** loads messages from the store, if not found -- uses the legacy connection */
405
+ loadMessages: async (jid, count, cursor) => {
406
+ const list = assertMessageList(jid);
407
+ const mode = !cursor || "before" in cursor ? "before" : "after";
408
+ const cursorKey = !!cursor
409
+ ? "before" in cursor
410
+ ? cursor.before
411
+ : cursor.after
412
+ : undefined;
413
+ const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined;
414
+ let messages;
415
+ if (list && mode === "before" && (!cursorKey || cursorValue)) {
416
+ if (cursorValue) {
417
+ const msgIdx = list.array.findIndex(
418
+ (m) =>
419
+ m.key.id ===
420
+ (cursorKey === null || cursorKey === void 0
421
+ ? void 0
422
+ : cursorKey.id),
423
+ );
424
+ messages = list.array.slice(0, msgIdx);
425
+ } else {
426
+ messages = list.array;
427
+ }
428
+ const diff = count - messages.length;
429
+ if (diff < 0) {
430
+ messages = messages.slice(-count); // get the last X messages
431
+ }
432
+ } else {
433
+ messages = [];
434
+ }
435
+ return messages;
436
+ },
437
+ /**
438
+ * Get all available labels for profile
439
+ *
440
+ * Keep in mind that the list is formed from predefined tags and tags
441
+ * that were "caught" during their editing.
442
+ */
443
+ getLabels: () => {
444
+ return labels;
445
+ },
446
+ /**
447
+ * Get labels for chat
448
+ *
449
+ * @returns Label IDs
450
+ **/
451
+ getChatLabels: (chatId) => {
452
+ return labelAssociations.filter((la) => la.chatId === chatId).all();
453
+ },
454
+ /**
455
+ * Get labels for message
456
+ *
457
+ * @returns Label IDs
458
+ **/
459
+ getMessageLabels: (messageId) => {
460
+ const associations = labelAssociations
461
+ .filter((la) => la.messageId === messageId)
462
+ .all();
463
+ return associations.map(({ labelId }) => labelId);
464
+ },
465
+ loadMessage: async (jid, id) => {
466
+ var _a;
467
+ return (_a = messages[jid]) === null || _a === void 0
468
+ ? void 0
469
+ : _a.get(id);
470
+ },
471
+ mostRecentMessage: async (jid) => {
472
+ var _a;
473
+ const message =
474
+ (_a = messages[jid]) === null || _a === void 0
475
+ ? void 0
476
+ : _a.array.slice(-1)[0];
477
+ return message;
478
+ },
479
+ fetchImageUrl: async (jid, sock) => {
480
+ const contact = contacts[jid];
481
+ if (!contact) {
482
+ return sock === null || sock === void 0
483
+ ? void 0
484
+ : sock.profilePictureUrl(jid);
485
+ }
486
+ if (typeof contact.imgUrl === "undefined") {
487
+ contact.imgUrl = await (sock === null || sock === void 0
488
+ ? void 0
489
+ : sock.profilePictureUrl(jid));
490
+ }
491
+ return contact.imgUrl;
492
+ },
493
+ fetchGroupMetadata: async (jid, sock) => {
494
+ if (!groupMetadata[jid]) {
495
+ const metadata = await (sock === null || sock === void 0
496
+ ? void 0
497
+ : sock.groupMetadata(jid));
498
+ if (metadata) {
499
+ groupMetadata[jid] = metadata;
500
+ }
501
+ }
502
+ return groupMetadata[jid];
503
+ },
504
+ fetchMessageReceipts: async ({ remoteJid, id }) => {
505
+ const list = messages[remoteJid];
506
+ const msg = list === null || list === void 0 ? void 0 : list.get(id);
507
+ return msg === null || msg === void 0 ? void 0 : msg.userReceipt;
508
+ },
509
+ toJSON: toJSON,
510
+ fromJSON: fromJSON,
511
+ writeToFile: (path) => {
512
+ writeFileSync(path, JSON.stringify(toJSON()));
513
+ },
514
+ readFromFile: (path) => {
515
+ if (existsSync(path)) {
516
+ logger.debug({ path: path }, "reading from file");
517
+ const jsonStr = readFileSync(path, { encoding: "utf-8" });
518
+ const json = JSON.parse(jsonStr);
519
+ fromJSON(json);
520
+ }
521
+ },
522
+ };
523
+ };