whatsapp-store-db 1.3.44 → 1.3.46
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/dist/handlers/chat.js +1 -16
- package/dist/handlers/contact.js +41 -24
- package/package.json +1 -1
package/dist/handlers/chat.js
CHANGED
|
@@ -72,11 +72,9 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
72
72
|
}
|
|
73
73
|
logger.error({ id, sessionId }, 'Failed to persist chat record after repeated retries');
|
|
74
74
|
};
|
|
75
|
-
const set = async ({ chats
|
|
75
|
+
const set = async ({ chats }) => {
|
|
76
76
|
try {
|
|
77
77
|
await prisma.$transaction(async (tx) => {
|
|
78
|
-
if (isLatest)
|
|
79
|
-
await tx.chat.deleteMany({ where: { sessionId } });
|
|
80
78
|
// Process chats in batches to avoid timeout
|
|
81
79
|
const BATCH_SIZE = 100;
|
|
82
80
|
const normalizedChats = chats.map((c) => {
|
|
@@ -165,24 +163,12 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
165
163
|
}
|
|
166
164
|
}
|
|
167
165
|
};
|
|
168
|
-
const del = async (ids) => {
|
|
169
|
-
try {
|
|
170
|
-
const normalizedIds = ids.map((id) => resolveChatId(id).resolvedId);
|
|
171
|
-
await prisma.chat.deleteMany({
|
|
172
|
-
where: { id: { in: normalizedIds } },
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
catch (e) {
|
|
176
|
-
logger.error(e, 'An error occured during chats delete');
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
166
|
const listen = () => {
|
|
180
167
|
if (listening)
|
|
181
168
|
return;
|
|
182
169
|
event.on('messaging-history.set', set);
|
|
183
170
|
event.on('chats.upsert', upsert);
|
|
184
171
|
event.on('chats.update', update);
|
|
185
|
-
// event.on('chats.delete', del);
|
|
186
172
|
listening = true;
|
|
187
173
|
};
|
|
188
174
|
const unlisten = () => {
|
|
@@ -191,7 +177,6 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
191
177
|
event.off('messaging-history.set', set);
|
|
192
178
|
event.off('chats.upsert', upsert);
|
|
193
179
|
event.off('chats.update', update);
|
|
194
|
-
// event.off('chats.delete', del);
|
|
195
180
|
listening = false;
|
|
196
181
|
};
|
|
197
182
|
return { listen, unlisten };
|
package/dist/handlers/contact.js
CHANGED
|
@@ -7,14 +7,41 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
7
7
|
const prisma = (0, shared_1.usePrisma)();
|
|
8
8
|
const logger = (0, shared_1.useLogger)();
|
|
9
9
|
let listening = false;
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Resolves a contact ID, converting LID format to phone number format when possible.
|
|
12
|
+
* Checks multiple sources: contact fields, getJid function, and Chat table lookup.
|
|
13
|
+
*/
|
|
14
|
+
const resolveContactId = async (id, contact) => {
|
|
11
15
|
// console.log("contactHandler:contact:", contact);
|
|
12
|
-
// Prefer primary number when we get a LID id
|
|
16
|
+
// Prefer primary number when we get a LID id
|
|
13
17
|
if (id === null || id === void 0 ? void 0 : id.endsWith('@lid')) {
|
|
18
|
+
// First, check contact-level fields
|
|
14
19
|
const candidate = ((contact === null || contact === void 0 ? void 0 : contact.senderPn) || (contact === null || contact === void 0 ? void 0 : contact.pnJid) || (contact === null || contact === void 0 ? void 0 : contact.jid));
|
|
15
|
-
if (candidate) {
|
|
20
|
+
if (candidate && candidate.includes('@s.whatsapp.net')) {
|
|
16
21
|
return (0, baileys_1.jidNormalizedUser)(candidate);
|
|
17
22
|
}
|
|
23
|
+
// Second, try getJid function
|
|
24
|
+
const jidByLid = typeof getJid === 'function' ? getJid(id || '') : undefined;
|
|
25
|
+
if (jidByLid && jidByLid.includes('@s.whatsapp.net')) {
|
|
26
|
+
return (0, baileys_1.jidNormalizedUser)(jidByLid);
|
|
27
|
+
}
|
|
28
|
+
// Third, look up the Chat table for a mapping (lidJid -> id)
|
|
29
|
+
try {
|
|
30
|
+
const chatWithLid = await prisma.chat.findFirst({
|
|
31
|
+
select: { id: true },
|
|
32
|
+
where: {
|
|
33
|
+
sessionId,
|
|
34
|
+
lidJid: id,
|
|
35
|
+
id: { endsWith: '@s.whatsapp.net' }
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
if (chatWithLid === null || chatWithLid === void 0 ? void 0 : chatWithLid.id) {
|
|
39
|
+
return (0, baileys_1.jidNormalizedUser)(chatWithLid.id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
// Silently ignore lookup errors, fall through to default
|
|
44
|
+
}
|
|
18
45
|
}
|
|
19
46
|
const jidByLid = typeof getJid === 'function' ? getJid(id || '') : undefined;
|
|
20
47
|
return (0, baileys_1.jidNormalizedUser)(jidByLid !== null && jidByLid !== void 0 ? jidByLid : id);
|
|
@@ -31,28 +58,18 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
31
58
|
};
|
|
32
59
|
const set = async ({ contacts }) => {
|
|
33
60
|
try {
|
|
34
|
-
const normalizedContacts = contacts.map((c) => {
|
|
35
|
-
const id = resolveContactId(c.id, c);
|
|
61
|
+
const normalizedContacts = await Promise.all(contacts.map(async (c) => {
|
|
62
|
+
const id = await resolveContactId(c.id, c);
|
|
36
63
|
const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
|
|
37
64
|
return Object.assign(Object.assign({}, data), { id });
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const deletedOldContactIds = (await prisma.contact.findMany({
|
|
41
|
-
select: { id: true },
|
|
42
|
-
where: { id: { notIn: contactIds }, sessionId },
|
|
43
|
-
})).map((c) => c.id);
|
|
44
|
-
const upsertPromises = normalizedContacts
|
|
45
|
-
.map((data) => prisma.contact.upsert({
|
|
65
|
+
}));
|
|
66
|
+
await Promise.all(normalizedContacts.map((data) => prisma.contact.upsert({
|
|
46
67
|
select: { pkId: true },
|
|
47
68
|
create: Object.assign(Object.assign({}, data), { sessionId }),
|
|
48
69
|
update: data,
|
|
49
70
|
where: { sessionId_id: { id: data.id, sessionId } },
|
|
50
|
-
}));
|
|
51
|
-
|
|
52
|
-
...upsertPromises,
|
|
53
|
-
prisma.contact.deleteMany({ where: { id: { in: deletedOldContactIds }, sessionId } }),
|
|
54
|
-
]);
|
|
55
|
-
logger.info({ deletedContacts: deletedOldContactIds.length, newContacts: contacts.length }, 'Synced contacts');
|
|
71
|
+
})));
|
|
72
|
+
logger.info({ contactsProcessed: contacts.length }, 'Synced contacts');
|
|
56
73
|
}
|
|
57
74
|
catch (e) {
|
|
58
75
|
logger.error(e, 'An error occured during contacts set');
|
|
@@ -60,12 +77,12 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
60
77
|
};
|
|
61
78
|
const upsert = async (contacts) => {
|
|
62
79
|
try {
|
|
63
|
-
const normalizedContacts = contacts.map((c) => {
|
|
64
|
-
const id = resolveContactId(c.id, c);
|
|
80
|
+
const normalizedContacts = await Promise.all(contacts.map(async (c) => {
|
|
81
|
+
const id = await resolveContactId(c.id, c);
|
|
65
82
|
const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
|
|
66
83
|
return Object.assign(Object.assign({}, data), { id });
|
|
67
|
-
});
|
|
68
|
-
await Promise.
|
|
84
|
+
}));
|
|
85
|
+
await Promise.all(normalizedContacts.map((data) => prisma.contact.upsert({
|
|
69
86
|
select: { pkId: true },
|
|
70
87
|
create: Object.assign(Object.assign({}, data), { sessionId }),
|
|
71
88
|
update: data,
|
|
@@ -83,7 +100,7 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
83
100
|
continue;
|
|
84
101
|
}
|
|
85
102
|
try {
|
|
86
|
-
const contactId = resolveContactId(update.id, update);
|
|
103
|
+
const contactId = await resolveContactId(update.id, update);
|
|
87
104
|
const transformedData = sanitizeContactData((0, utils_1.transformPrisma)(update));
|
|
88
105
|
await prisma.contact.upsert({
|
|
89
106
|
select: { pkId: true },
|