whatsapp-store-db 1.3.45 → 1.3.47
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 +56 -16
- package/dist/handlers/contact.js +4 -14
- package/package.json +1 -1
package/dist/handlers/chat.js
CHANGED
|
@@ -8,6 +8,45 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
8
8
|
const prisma = (0, shared_1.usePrisma)();
|
|
9
9
|
const logger = (0, shared_1.useLogger)();
|
|
10
10
|
let listening = false;
|
|
11
|
+
/**
|
|
12
|
+
* Updates Contact records that have LID as their ID when we discover the phone number mapping.
|
|
13
|
+
* This handles the case where Contact events arrive before Chat events.
|
|
14
|
+
*/
|
|
15
|
+
const updateContactWithLidMapping = async (lidJid, phoneNumberId) => {
|
|
16
|
+
try {
|
|
17
|
+
// Check if there's a Contact with the LID as ID
|
|
18
|
+
const contactWithLid = await prisma.contact.findFirst({
|
|
19
|
+
select: { pkId: true },
|
|
20
|
+
where: { sessionId, id: lidJid },
|
|
21
|
+
});
|
|
22
|
+
if (contactWithLid) {
|
|
23
|
+
// Check if a contact with the phone number already exists
|
|
24
|
+
const existingContact = await prisma.contact.findFirst({
|
|
25
|
+
select: { pkId: true },
|
|
26
|
+
where: { sessionId, id: phoneNumberId },
|
|
27
|
+
});
|
|
28
|
+
if (existingContact) {
|
|
29
|
+
// Phone number contact exists, delete the LID contact (it's a duplicate)
|
|
30
|
+
await prisma.contact.delete({
|
|
31
|
+
where: { pkId: contactWithLid.pkId },
|
|
32
|
+
});
|
|
33
|
+
logger.info({ lidJid, phoneNumberId }, 'Removed duplicate LID contact (phone number contact exists)');
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Update the LID contact to use the phone number
|
|
37
|
+
await prisma.contact.update({
|
|
38
|
+
where: { pkId: contactWithLid.pkId },
|
|
39
|
+
data: { id: phoneNumberId },
|
|
40
|
+
});
|
|
41
|
+
logger.info({ lidJid, phoneNumberId }, 'Updated Contact LID to phone number');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
// Silently ignore errors - this is a best-effort cleanup
|
|
47
|
+
logger.debug({ lidJid, phoneNumberId, error: e }, 'Failed to update Contact with LID mapping');
|
|
48
|
+
}
|
|
49
|
+
};
|
|
11
50
|
/**
|
|
12
51
|
* Resolves a chat ID, converting LID format to phone number format when possible.
|
|
13
52
|
* Returns an object with both the resolved ID and the original LID (if conversion happened).
|
|
@@ -72,11 +111,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
72
111
|
}
|
|
73
112
|
logger.error({ id, sessionId }, 'Failed to persist chat record after repeated retries');
|
|
74
113
|
};
|
|
75
|
-
const set = async ({ chats
|
|
114
|
+
const set = async ({ chats }) => {
|
|
115
|
+
const lidMappings = [];
|
|
76
116
|
try {
|
|
77
117
|
await prisma.$transaction(async (tx) => {
|
|
78
|
-
if (isLatest)
|
|
79
|
-
await tx.chat.deleteMany({ where: { sessionId } });
|
|
80
118
|
// Process chats in batches to avoid timeout
|
|
81
119
|
const BATCH_SIZE = 100;
|
|
82
120
|
const normalizedChats = chats.map((c) => {
|
|
@@ -86,6 +124,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
86
124
|
// Set lidJid if we resolved from LID to phone number
|
|
87
125
|
if (lidJid) {
|
|
88
126
|
validatedData.lidJid = lidJid;
|
|
127
|
+
lidMappings.push({ lidJid, phoneNumberId: resolvedId });
|
|
89
128
|
}
|
|
90
129
|
return Object.assign(Object.assign({}, validatedData), { id: resolvedId });
|
|
91
130
|
});
|
|
@@ -106,6 +145,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
106
145
|
}, {
|
|
107
146
|
timeout: 30000, // 30 seconds for this specific transaction
|
|
108
147
|
});
|
|
148
|
+
// Update any Contact records that have LID as their ID (outside transaction)
|
|
149
|
+
for (const { lidJid, phoneNumberId } of lidMappings) {
|
|
150
|
+
await updateContactWithLidMapping(lidJid, phoneNumberId);
|
|
151
|
+
}
|
|
109
152
|
}
|
|
110
153
|
catch (e) {
|
|
111
154
|
logger.error(e, 'An error occured during chats set');
|
|
@@ -115,6 +158,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
115
158
|
try {
|
|
116
159
|
// Normalize and de-duplicate by resolved id (keep the last occurrence)
|
|
117
160
|
const dedupedById = new Map();
|
|
161
|
+
const lidMappings = [];
|
|
118
162
|
for (const c of chats) {
|
|
119
163
|
const { resolvedId, lidJid } = resolveChatId(c.id, c);
|
|
120
164
|
const transformedData = (0, utils_1.transformPrisma)(c);
|
|
@@ -122,6 +166,7 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
122
166
|
// Set lidJid if we resolved from LID to phone number
|
|
123
167
|
if (lidJid) {
|
|
124
168
|
validatedData.lidJid = lidJid;
|
|
169
|
+
lidMappings.push({ lidJid, phoneNumberId: resolvedId });
|
|
125
170
|
}
|
|
126
171
|
dedupedById.set(resolvedId, Object.assign(Object.assign({}, validatedData), { id: resolvedId }));
|
|
127
172
|
}
|
|
@@ -131,6 +176,10 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
131
176
|
createData: Object.assign({}, data),
|
|
132
177
|
updateData: Object.assign(Object.assign({}, data), { id: undefined }),
|
|
133
178
|
})));
|
|
179
|
+
// Update any Contact records that have LID as their ID
|
|
180
|
+
for (const { lidJid, phoneNumberId } of lidMappings) {
|
|
181
|
+
await updateContactWithLidMapping(lidJid, phoneNumberId);
|
|
182
|
+
}
|
|
134
183
|
}
|
|
135
184
|
catch (e) {
|
|
136
185
|
logger.error(e, 'An error occured during chats upsert');
|
|
@@ -159,30 +208,22 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
159
208
|
: { set: validatedData.unreadCount }
|
|
160
209
|
: undefined }),
|
|
161
210
|
});
|
|
211
|
+
// Update any Contact records that have LID as their ID
|
|
212
|
+
if (lidJid) {
|
|
213
|
+
await updateContactWithLidMapping(lidJid, chatId);
|
|
214
|
+
}
|
|
162
215
|
}
|
|
163
216
|
catch (e) {
|
|
164
217
|
logger.error(e, 'An error occured during chat update');
|
|
165
218
|
}
|
|
166
219
|
}
|
|
167
220
|
};
|
|
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
221
|
const listen = () => {
|
|
180
222
|
if (listening)
|
|
181
223
|
return;
|
|
182
224
|
event.on('messaging-history.set', set);
|
|
183
225
|
event.on('chats.upsert', upsert);
|
|
184
226
|
event.on('chats.update', update);
|
|
185
|
-
// event.on('chats.delete', del);
|
|
186
227
|
listening = true;
|
|
187
228
|
};
|
|
188
229
|
const unlisten = () => {
|
|
@@ -191,7 +232,6 @@ function chatHandler(sessionId, event, getJid = undefined) {
|
|
|
191
232
|
event.off('messaging-history.set', set);
|
|
192
233
|
event.off('chats.upsert', upsert);
|
|
193
234
|
event.off('chats.update', update);
|
|
194
|
-
// event.off('chats.delete', del);
|
|
195
235
|
listening = false;
|
|
196
236
|
};
|
|
197
237
|
return { listen, unlisten };
|
package/dist/handlers/contact.js
CHANGED
|
@@ -63,23 +63,13 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
63
63
|
const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
|
|
64
64
|
return Object.assign(Object.assign({}, data), { id });
|
|
65
65
|
}));
|
|
66
|
-
|
|
67
|
-
const deletedOldContactIds = (await prisma.contact.findMany({
|
|
68
|
-
select: { id: true },
|
|
69
|
-
where: { id: { notIn: contactIds }, sessionId },
|
|
70
|
-
})).map((c) => c.id);
|
|
71
|
-
const upsertPromises = normalizedContacts
|
|
72
|
-
.map((data) => prisma.contact.upsert({
|
|
66
|
+
await Promise.all(normalizedContacts.map((data) => prisma.contact.upsert({
|
|
73
67
|
select: { pkId: true },
|
|
74
68
|
create: Object.assign(Object.assign({}, data), { sessionId }),
|
|
75
69
|
update: data,
|
|
76
70
|
where: { sessionId_id: { id: data.id, sessionId } },
|
|
77
|
-
}));
|
|
78
|
-
|
|
79
|
-
...upsertPromises,
|
|
80
|
-
prisma.contact.deleteMany({ where: { id: { in: deletedOldContactIds }, sessionId } }),
|
|
81
|
-
]);
|
|
82
|
-
logger.info({ deletedContacts: deletedOldContactIds.length, newContacts: contacts.length }, 'Synced contacts');
|
|
71
|
+
})));
|
|
72
|
+
logger.info({ contactsProcessed: contacts.length }, 'Synced contacts');
|
|
83
73
|
}
|
|
84
74
|
catch (e) {
|
|
85
75
|
logger.error(e, 'An error occured during contacts set');
|
|
@@ -92,7 +82,7 @@ function contactHandler(sessionId, event, getJid = undefined) {
|
|
|
92
82
|
const data = sanitizeContactData((0, utils_1.transformPrisma)(c));
|
|
93
83
|
return Object.assign(Object.assign({}, data), { id });
|
|
94
84
|
}));
|
|
95
|
-
await Promise.
|
|
85
|
+
await Promise.all(normalizedContacts.map((data) => prisma.contact.upsert({
|
|
96
86
|
select: { pkId: true },
|
|
97
87
|
create: Object.assign(Object.assign({}, data), { sessionId }),
|
|
98
88
|
update: data,
|