@prototypevip/baileys 0.0.2 → 0.0.4
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.
- package/lib/Store/prototype-store.js +88 -34
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/message-builder.js +2281 -0
- package/lib/Utils/messages.js +45 -0
- package/package.json +5 -1
|
@@ -285,7 +285,12 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
287
|
} catch (err) {
|
|
288
|
-
|
|
288
|
+
const code = err?.code || err?.message || "";
|
|
289
|
+
if (String(code).includes("READONLY") || String(code).includes("SQLITE_READONLY")) {
|
|
290
|
+
console.error("PrototypeStore Transaction Step Error (readonly DB - check file path/permissions/volume):", err.message || err);
|
|
291
|
+
} else {
|
|
292
|
+
console.error("PrototypeStore Transaction Step Error:", err);
|
|
293
|
+
}
|
|
289
294
|
}
|
|
290
295
|
}
|
|
291
296
|
})();
|
|
@@ -380,6 +385,49 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
380
385
|
|
|
381
386
|
const presencesCache = {};
|
|
382
387
|
|
|
388
|
+
|
|
389
|
+
const safeAssignChat = (existing, incoming) => {
|
|
390
|
+
if (!existing || !incoming) return existing;
|
|
391
|
+
const savedMessages = existing.messages;
|
|
392
|
+
Object.assign(existing, incoming, { isChats: true });
|
|
393
|
+
// Never let chat events wipe the ordered messages store
|
|
394
|
+
if (
|
|
395
|
+
!existing.messages ||
|
|
396
|
+
typeof existing.messages.upsert !== "function"
|
|
397
|
+
) {
|
|
398
|
+
existing.messages = savedMessages || makeMessagesStore();
|
|
399
|
+
}
|
|
400
|
+
if (!existing.id) existing.id = incoming.id || existing.id;
|
|
401
|
+
return existing;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const makeMessagesStore = () =>
|
|
405
|
+
new Proxy(
|
|
406
|
+
makeOrderedDictionary((m) => {
|
|
407
|
+
const participant = m.key.participant
|
|
408
|
+
? resolveJID(m.key.participant, store.signalRepository?.lidMapping)
|
|
409
|
+
: m.key.fromMe
|
|
410
|
+
? "me"
|
|
411
|
+
: "";
|
|
412
|
+
return participant ? `${participant}:${m.key.id}` : m.key.id;
|
|
413
|
+
}),
|
|
414
|
+
{
|
|
415
|
+
get(target, prop) {
|
|
416
|
+
if (prop in target) return target[prop];
|
|
417
|
+
return target.get(prop);
|
|
418
|
+
},
|
|
419
|
+
set(target, prop, value) {
|
|
420
|
+
target.upsert(value);
|
|
421
|
+
return true;
|
|
422
|
+
},
|
|
423
|
+
deleteProperty(target, prop) {
|
|
424
|
+
const item = target.get(prop);
|
|
425
|
+
if (item) return target.remove(item);
|
|
426
|
+
return false;
|
|
427
|
+
},
|
|
428
|
+
},
|
|
429
|
+
);
|
|
430
|
+
|
|
383
431
|
const store = {
|
|
384
432
|
signalRepository: config.signalRepository,
|
|
385
433
|
chats: new Proxy(chatsCache, {
|
|
@@ -389,38 +437,17 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
389
437
|
if (!target[jid]) {
|
|
390
438
|
target[jid] = {
|
|
391
439
|
id: jid,
|
|
392
|
-
messages:
|
|
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
|
-
),
|
|
440
|
+
messages: makeMessagesStore(),
|
|
420
441
|
get isGroup() {
|
|
421
442
|
return jid.endsWith("@g.us");
|
|
422
443
|
},
|
|
423
444
|
};
|
|
445
|
+
} else if (
|
|
446
|
+
!target[jid].messages ||
|
|
447
|
+
typeof target[jid].messages.upsert !== "function"
|
|
448
|
+
) {
|
|
449
|
+
// Recover if messages was overwritten by Object.assign / chat updates
|
|
450
|
+
target[jid].messages = makeMessagesStore();
|
|
424
451
|
}
|
|
425
452
|
return target[jid];
|
|
426
453
|
},
|
|
@@ -606,7 +633,7 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
606
633
|
if (!id || id === "status@broadcast") continue;
|
|
607
634
|
|
|
608
635
|
const existingChat = store.chats[id];
|
|
609
|
-
|
|
636
|
+
safeAssignChat(existingChat, chat);
|
|
610
637
|
|
|
611
638
|
if (id.endsWith("@g.us")) {
|
|
612
639
|
const current = groupsCache[id] || {};
|
|
@@ -731,16 +758,26 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
731
758
|
store.signalRepository?.lidMapping,
|
|
732
759
|
);
|
|
733
760
|
const existingChat = store.chats[id];
|
|
734
|
-
|
|
761
|
+
safeAssignChat(existingChat, chat);
|
|
735
762
|
}
|
|
736
763
|
},
|
|
737
764
|
);
|
|
738
765
|
|
|
739
766
|
listen("messages.upsert", async ({ messages }) => {
|
|
740
767
|
for (let msg of messages) {
|
|
741
|
-
const
|
|
768
|
+
const rawChatId = msg?.key?.remoteJid;
|
|
769
|
+
if (!rawChatId) continue;
|
|
770
|
+
const chatId = await resolveJIDAsync(
|
|
771
|
+
rawChatId,
|
|
772
|
+
store.signalRepository?.lidMapping,
|
|
773
|
+
);
|
|
742
774
|
if (!chatId) continue;
|
|
743
|
-
store.chats[chatId]
|
|
775
|
+
const chat = store.chats[chatId];
|
|
776
|
+
if (!chat) continue;
|
|
777
|
+
if (!chat.messages || typeof chat.messages.upsert !== "function") {
|
|
778
|
+
chat.messages = makeMessagesStore();
|
|
779
|
+
}
|
|
780
|
+
chat.messages.upsert(cloneM(msg));
|
|
744
781
|
|
|
745
782
|
if (db) {
|
|
746
783
|
const msgKey = getMessageKey(
|
|
@@ -788,7 +825,7 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
788
825
|
store.signalRepository?.lidMapping,
|
|
789
826
|
);
|
|
790
827
|
if (chatsCache[jid]) {
|
|
791
|
-
chatsCache[jid].messages =
|
|
828
|
+
chatsCache[jid].messages = makeMessagesStore();
|
|
792
829
|
if (db)
|
|
793
830
|
pendingUpdates.set(`del:m:${jid}`, {
|
|
794
831
|
type: "delete",
|
|
@@ -954,5 +991,22 @@ export const makePrototypeStore = (config = {}) => {
|
|
|
954
991
|
},
|
|
955
992
|
};
|
|
956
993
|
|
|
994
|
+
store.close = () => {
|
|
995
|
+
if (flushInterval) clearInterval(flushInterval);
|
|
996
|
+
if (cleanInterval) clearInterval(cleanInterval);
|
|
997
|
+
try {
|
|
998
|
+
_flush();
|
|
999
|
+
} catch {
|
|
1000
|
+
// ignore flush errors on close
|
|
1001
|
+
}
|
|
1002
|
+
if (db) {
|
|
1003
|
+
try {
|
|
1004
|
+
db.close();
|
|
1005
|
+
} catch {
|
|
1006
|
+
// ignore close errors
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
|
|
957
1011
|
return store;
|
|
958
1012
|
};
|
package/lib/Utils/index.js
CHANGED