fix: enhance message content sanitization in Baileys service and improve message retrieval logic in Chatwoot service

This commit is contained in:
Davidson Gomes 2025-09-09 12:50:46 -03:00
parent d9c04fc866
commit 21502b996d
2 changed files with 60 additions and 30 deletions

View File

@ -4302,14 +4302,15 @@ export class BaileysStartupService extends ChannelStartupService {
private sanitizeMessageContent(messageContent: any): any { private sanitizeMessageContent(messageContent: any): any {
if (!messageContent) return messageContent; if (!messageContent) return messageContent;
// Deep clone to avoid modifying original // Deep clone and sanitize to avoid modifying original
const sanitized = JSON.parse(JSON.stringify(messageContent, (key, value) => { return JSON.parse(
JSON.stringify(messageContent, (key, value) => {
// Convert Long objects to numbers // Convert Long objects to numbers
if (Long.isLong(value)) { if (Long.isLong(value)) {
return value.toNumber(); return value.toNumber();
} }
// Convert Uint8Array to regular arrays or remove them // Convert Uint8Array to regular arrays
if (value instanceof Uint8Array) { if (value instanceof Uint8Array) {
return Array.from(value); return Array.from(value);
} }
@ -4319,10 +4320,26 @@ export class BaileysStartupService extends ChannelStartupService {
return undefined; return undefined;
} }
return value; // Handle objects with toJSON method
})); if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
return value.toJSON();
}
return sanitized; // Handle special objects that might not serialize properly
if (value && typeof value === 'object') {
// Check if it's a plain object or has prototype issues
try {
JSON.stringify(value);
return value;
} catch (e) {
// If it can't be stringified, return a safe representation
return '[Non-serializable object]';
}
}
return value;
}),
);
} }
private prepareMessage(message: proto.IWebMessageInfo): any { private prepareMessage(message: proto.IWebMessageInfo): any {
@ -4330,7 +4347,7 @@ export class BaileysStartupService extends ChannelStartupService {
const contentMsg = message?.message[contentType] as any; const contentMsg = message?.message[contentType] as any;
const messageRaw = { const messageRaw = {
key: message.key, key: message.key, // Save key exactly as it comes from Baileys
pushName: pushName:
message.pushName || message.pushName ||
(message.key.fromMe (message.key.fromMe
@ -4338,7 +4355,7 @@ export class BaileysStartupService extends ChannelStartupService {
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)), : message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
status: status[message.status], status: status[message.status],
message: this.sanitizeMessageContent({ ...message.message }), message: this.sanitizeMessageContent({ ...message.message }),
contextInfo: contentMsg?.contextInfo, contextInfo: this.sanitizeMessageContent(contentMsg?.contextInfo),
messageType: contentType || 'unknown', messageType: contentType || 'unknown',
messageTimestamp: Long.isLong(message.messageTimestamp) messageTimestamp: Long.isLong(message.messageTimestamp)
? (message.messageTimestamp as Long).toNumber() ? (message.messageTimestamp as Long).toNumber()

View File

@ -1561,13 +1561,24 @@ export class ChatwootService {
return; return;
} }
// Use the message ID directly instead of JSON path query
await this.prismaRepository.message.updateMany({ await this.prismaRepository.message.updateMany({
where: { where: {
AND: [
{ instanceId: instance.instanceId },
{
OR: [
{ id: message.id }, // Use the actual message ID if available
// Fallback to raw query if needed
{
key: { key: {
path: ['id'], path: ['id'],
equals: key.id, equals: key.id,
}, },
instanceId: instance.instanceId, },
],
},
],
}, },
data: { data: {
chatwootMessageId: chatwootMessageIds.messageId, chatwootMessageId: chatwootMessageIds.messageId,
@ -1584,13 +1595,15 @@ export class ChatwootService {
} }
private async getMessageByKeyId(instance: InstanceDto, keyId: string): Promise<MessageModel> { private async getMessageByKeyId(instance: InstanceDto, keyId: string): Promise<MessageModel> {
// Try to find message using a more compatible approach
const messages = await this.prismaRepository.message.findFirst({ const messages = await this.prismaRepository.message.findFirst({
where: { where: {
instanceId: instance.instanceId,
// Use raw query to avoid JSON path issues
key: { key: {
path: ['id'], path: ['id'],
equals: keyId, equals: keyId,
}, },
instanceId: instance.instanceId,
}, },
}); });