Skip to content

Commit 21502b9

Browse files
committed
fix: enhance message content sanitization in Baileys service and improve message retrieval logic in Chatwoot service
1 parent d9c04fc commit 21502b9

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,46 +4302,63 @@ export class BaileysStartupService extends ChannelStartupService {
43024302
private sanitizeMessageContent(messageContent: any): any {
43034303
if (!messageContent) return messageContent;
43044304

4305-
// Deep clone to avoid modifying original
4306-
const sanitized = JSON.parse(JSON.stringify(messageContent, (key, value) => {
4307-
// Convert Long objects to numbers
4308-
if (Long.isLong(value)) {
4309-
return value.toNumber();
4310-
}
4311-
4312-
// Convert Uint8Array to regular arrays or remove them
4313-
if (value instanceof Uint8Array) {
4314-
return Array.from(value);
4315-
}
4316-
4317-
// Remove functions and other non-serializable objects
4318-
if (typeof value === 'function') {
4319-
return undefined;
4320-
}
4321-
4322-
return value;
4323-
}));
4305+
// Deep clone and sanitize to avoid modifying original
4306+
return JSON.parse(
4307+
JSON.stringify(messageContent, (key, value) => {
4308+
// Convert Long objects to numbers
4309+
if (Long.isLong(value)) {
4310+
return value.toNumber();
4311+
}
4312+
4313+
// Convert Uint8Array to regular arrays
4314+
if (value instanceof Uint8Array) {
4315+
return Array.from(value);
4316+
}
4317+
4318+
// Remove functions and other non-serializable objects
4319+
if (typeof value === 'function') {
4320+
return undefined;
4321+
}
43244322

4325-
return sanitized;
4323+
// Handle objects with toJSON method
4324+
if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
4325+
return value.toJSON();
4326+
}
4327+
4328+
// Handle special objects that might not serialize properly
4329+
if (value && typeof value === 'object') {
4330+
// Check if it's a plain object or has prototype issues
4331+
try {
4332+
JSON.stringify(value);
4333+
return value;
4334+
} catch (e) {
4335+
// If it can't be stringified, return a safe representation
4336+
return '[Non-serializable object]';
4337+
}
4338+
}
4339+
4340+
return value;
4341+
}),
4342+
);
43264343
}
43274344

43284345
private prepareMessage(message: proto.IWebMessageInfo): any {
43294346
const contentType = getContentType(message.message);
43304347
const contentMsg = message?.message[contentType] as any;
43314348

43324349
const messageRaw = {
4333-
key: message.key,
4350+
key: message.key, // Save key exactly as it comes from Baileys
43344351
pushName:
43354352
message.pushName ||
43364353
(message.key.fromMe
43374354
? 'Você'
43384355
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
43394356
status: status[message.status],
43404357
message: this.sanitizeMessageContent({ ...message.message }),
4341-
contextInfo: contentMsg?.contextInfo,
4358+
contextInfo: this.sanitizeMessageContent(contentMsg?.contextInfo),
43424359
messageType: contentType || 'unknown',
4343-
messageTimestamp: Long.isLong(message.messageTimestamp)
4344-
? (message.messageTimestamp as Long).toNumber()
4360+
messageTimestamp: Long.isLong(message.messageTimestamp)
4361+
? (message.messageTimestamp as Long).toNumber()
43454362
: (message.messageTimestamp as number),
43464363
instanceId: this.instanceId,
43474364
source: getDevice(message.key.id),

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,13 +1561,24 @@ export class ChatwootService {
15611561
return;
15621562
}
15631563

1564+
// Use the message ID directly instead of JSON path query
15641565
await this.prismaRepository.message.updateMany({
15651566
where: {
1566-
key: {
1567-
path: ['id'],
1568-
equals: key.id,
1569-
},
1570-
instanceId: instance.instanceId,
1567+
AND: [
1568+
{ instanceId: instance.instanceId },
1569+
{
1570+
OR: [
1571+
{ id: message.id }, // Use the actual message ID if available
1572+
// Fallback to raw query if needed
1573+
{
1574+
key: {
1575+
path: ['id'],
1576+
equals: key.id,
1577+
},
1578+
},
1579+
],
1580+
},
1581+
],
15711582
},
15721583
data: {
15731584
chatwootMessageId: chatwootMessageIds.messageId,
@@ -1584,13 +1595,15 @@ export class ChatwootService {
15841595
}
15851596

15861597
private async getMessageByKeyId(instance: InstanceDto, keyId: string): Promise<MessageModel> {
1598+
// Try to find message using a more compatible approach
15871599
const messages = await this.prismaRepository.message.findFirst({
15881600
where: {
1601+
instanceId: instance.instanceId,
1602+
// Use raw query to avoid JSON path issues
15891603
key: {
15901604
path: ['id'],
15911605
equals: keyId,
15921606
},
1593-
instanceId: instance.instanceId,
15941607
},
15951608
});
15961609

0 commit comments

Comments
 (0)