@prototypevip/baileys 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1112,6 +1112,7 @@ export const makeChatsSocket = (config) => {
1112
1112
  cleanDirtyBits: cleanDirtyBits,
1113
1113
  addOrEditContact: addOrEditContact,
1114
1114
  removeContact: removeContact,
1115
+ placeholderResendCache: placeholderResendCache,
1115
1116
  addLabel: addLabel,
1116
1117
  addChatLabel: addChatLabel,
1117
1118
  removeChatLabel: removeChatLabel,
@@ -39,10 +39,11 @@ import {
39
39
  buildMergedTcTokenIndexWrite,
40
40
  generateCsToken,
41
41
  unixTimestampSeconds,
42
+ DEF_MEDIA_HOST,
42
43
  } from "../Utils/index.js";
43
44
  import { AssociationType } from "../Types/index.js";
44
45
  import { getUrlInfo } from "../Utils/link-preview.js";
45
- import { makeKeyedMutex } from "../Utils/make-mutex.js";
46
+ import { makeKeyedMutex, makeMutex } from "../Utils/make-mutex.js";
46
47
  import {
47
48
  getMessageReportingToken,
48
49
  shouldIncludeReportingToken,
@@ -105,8 +106,12 @@ export const makeMessagesSocket = (config) => {
105
106
  ? new MessageRetryManager(logger, maxMsgRetryCount)
106
107
  : null;
107
108
  const encryptionMutex = makeKeyedMutex();
109
+ /** Serializes writes to userDevicesCache across USync refresh and device-notification handling. */
110
+ const devicesMutex = makeMutex();
108
111
  const inFlightTcTokenIssuance = new Set();
109
112
  let mediaConn;
113
+ /** Per-socket media host; updated whenever media_conn is fetched. Defaults to the public WhatsApp host. */
114
+ let mediaHost = DEF_MEDIA_HOST;
110
115
  const issuePrivacyTokens = async (jids, timestamp) => {
111
116
  const t = (timestamp ?? unixTimestampSeconds()).toString();
112
117
  const result = await query({
@@ -161,6 +166,9 @@ export const makeMessagesSocket = (config) => {
161
166
  fetchDate: new Date(),
162
167
  };
163
168
  logger.debug("fetched media conn");
169
+ if (node.hosts[0]) {
170
+ mediaHost = node.hosts[0].hostname;
171
+ }
164
172
  return node;
165
173
  })();
166
174
  }
@@ -363,18 +371,21 @@ export const makeMessagesSocket = (config) => {
363
371
  );
364
372
  }
365
373
  }
366
- if (userDevicesCache.mset) {
367
- await userDevicesCache.mset(
368
- Object.entries(deviceMap).map(([key, value]) => ({
369
- key: key,
370
- value: value,
371
- })),
372
- );
373
- } else {
374
- for (const key in deviceMap) {
375
- if (deviceMap[key]) await userDevicesCache.set(key, deviceMap[key]);
374
+ await devicesMutex.mutex(async () => {
375
+ if (userDevicesCache.mset) {
376
+ await userDevicesCache.mset(
377
+ Object.entries(deviceMap).map(([key, value]) => ({
378
+ key: key,
379
+ value: value,
380
+ })),
381
+ );
382
+ } else {
383
+ for (const key in deviceMap) {
384
+ if (deviceMap[key])
385
+ await userDevicesCache.set(key, deviceMap[key]);
386
+ }
376
387
  }
377
- }
388
+ });
378
389
  const userDeviceUpdates = {};
379
390
  for (const [userId, devices] of Object.entries(deviceMap)) {
380
391
  if (devices && devices.length > 0) {
@@ -1316,6 +1327,8 @@ export const makeMessagesSocket = (config) => {
1316
1327
  const waitForMsgMediaUpdate = bindWaitForEvent(ev, "messages.media-update");
1317
1328
  return {
1318
1329
  ...sock,
1330
+ userDevicesCache: userDevicesCache,
1331
+ devicesMutex: devicesMutex,
1319
1332
  getPrivacyTokens: getPrivacyTokens,
1320
1333
  issuePrivacyTokens: issuePrivacyTokens,
1321
1334
  assertSessions: assertSessions,
@@ -1324,6 +1337,8 @@ export const makeMessagesSocket = (config) => {
1324
1337
  sendReceipts: sendReceipts,
1325
1338
  readMessages: readMessages,
1326
1339
  refreshMediaConn: refreshMediaConn,
1340
+ // Function (not getter) so the spread in chats.js preserves the live closure binding.
1341
+ getMediaHost: () => mediaHost,
1327
1342
  waUploadToServer: waUploadToServer,
1328
1343
  fetchPrivacySettings: fetchPrivacySettings,
1329
1344
  sendPeerMessage: sendPeerMessage,
@@ -1368,7 +1383,7 @@ export const makeMessagesSocket = (config) => {
1368
1383
  );
1369
1384
  }
1370
1385
  content.directPath = media.directPath;
1371
- content.url = getUrlFromDirectPath(content.directPath);
1386
+ content.url = getUrlFromDirectPath(content.directPath, mediaHost);
1372
1387
  logger.debug(
1373
1388
  { directPath: media.directPath, key: result.key },
1374
1389
  "media update successful",
@@ -380,6 +380,33 @@ export const makePrototypeStore = (config = {}) => {
380
380
 
381
381
  const presencesCache = {};
382
382
 
383
+ const makeMessagesStore = () =>
384
+ new Proxy(
385
+ makeOrderedDictionary((m) => {
386
+ const participant = m.key.participant
387
+ ? resolveJID(m.key.participant, store.signalRepository?.lidMapping)
388
+ : m.key.fromMe
389
+ ? "me"
390
+ : "";
391
+ return participant ? `${participant}:${m.key.id}` : m.key.id;
392
+ }),
393
+ {
394
+ get(target, prop) {
395
+ if (prop in target) return target[prop];
396
+ return target.get(prop);
397
+ },
398
+ set(target, prop, value) {
399
+ target.upsert(value);
400
+ return true;
401
+ },
402
+ deleteProperty(target, prop) {
403
+ const item = target.get(prop);
404
+ if (item) return target.remove(item);
405
+ return false;
406
+ },
407
+ },
408
+ );
409
+
383
410
  const store = {
384
411
  signalRepository: config.signalRepository,
385
412
  chats: new Proxy(chatsCache, {
@@ -389,34 +416,7 @@ export const makePrototypeStore = (config = {}) => {
389
416
  if (!target[jid]) {
390
417
  target[jid] = {
391
418
  id: jid,
392
- messages: new Proxy(
393
- makeOrderedDictionary((m) => {
394
- const participant = m.key.participant
395
- ? resolveJID(
396
- m.key.participant,
397
- store.signalRepository?.lidMapping,
398
- )
399
- : m.key.fromMe
400
- ? "me"
401
- : "";
402
- return participant ? `${participant}:${m.key.id}` : m.key.id;
403
- }),
404
- {
405
- get(target, prop) {
406
- if (prop in target) return target[prop];
407
- return target.get(prop);
408
- },
409
- set(target, prop, value) {
410
- target.upsert(value);
411
- return true;
412
- },
413
- deleteProperty(target, prop) {
414
- const item = target.get(prop);
415
- if (item) return target.remove(item);
416
- return false;
417
- },
418
- },
419
- ),
419
+ messages: makeMessagesStore(),
420
420
  get isGroup() {
421
421
  return jid.endsWith("@g.us");
422
422
  },
@@ -788,7 +788,7 @@ export const makePrototypeStore = (config = {}) => {
788
788
  store.signalRepository?.lidMapping,
789
789
  );
790
790
  if (chatsCache[jid]) {
791
- chatsCache[jid].messages = {};
791
+ chatsCache[jid].messages = makeMessagesStore();
792
792
  if (db)
793
793
  pendingUpdates.set(`del:m:${jid}`, {
794
794
  type: "delete",
@@ -954,5 +954,22 @@ export const makePrototypeStore = (config = {}) => {
954
954
  },
955
955
  };
956
956
 
957
+ store.close = () => {
958
+ if (flushInterval) clearInterval(flushInterval);
959
+ if (cleanInterval) clearInterval(cleanInterval);
960
+ try {
961
+ _flush();
962
+ } catch {
963
+ // ignore flush errors on close
964
+ }
965
+ if (db) {
966
+ try {
967
+ db.close();
968
+ } catch {
969
+ // ignore close errors
970
+ }
971
+ }
972
+ };
973
+
957
974
  return store;
958
975
  };
@@ -580,7 +580,12 @@ const consolidateEvents = (data) => {
580
580
  Object.keys(data.historySets.messages).length ||
581
581
  Object.keys(data.historySets.contacts).length
582
582
  ) {
583
- map["messaging-history.set"] = data.historySets;
583
+ map["messaging-history.set"] = {
584
+ ...data.historySets,
585
+ chats: Object.values(data.historySets.chats),
586
+ contacts: Object.values(data.historySets.contacts),
587
+ messages: Object.values(data.historySets.messages),
588
+ };
584
589
  }
585
590
  if (Object.keys(data.chatUpserts).length)
586
591
  map["chats.upsert"] = Object.values(data.chatUpserts);
@@ -26,4 +26,5 @@ export * from "./cstoken-utils.js";
26
26
  export * from "./use-prototype-auth-state.js";
27
27
  export * from "./sticker-utils.js";
28
28
  export * from "./rich-message-utils.js";
29
+ export * from "./message-builder.js";
29
30
  export * from "./constants.js";