mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-09 01:49:37 -06:00
fix: enhance message content sanitization in Baileys service and improve message retrieval logic in Chatwoot service
This commit is contained in:
parent
d9c04fc866
commit
21502b996d
@ -4302,27 +4302,44 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
private sanitizeMessageContent(messageContent: any): any {
|
||||
if (!messageContent) return messageContent;
|
||||
|
||||
// Deep clone to avoid modifying original
|
||||
const sanitized = JSON.parse(JSON.stringify(messageContent, (key, value) => {
|
||||
// Convert Long objects to numbers
|
||||
if (Long.isLong(value)) {
|
||||
return value.toNumber();
|
||||
}
|
||||
|
||||
// Convert Uint8Array to regular arrays or remove them
|
||||
if (value instanceof Uint8Array) {
|
||||
return Array.from(value);
|
||||
}
|
||||
|
||||
// Remove functions and other non-serializable objects
|
||||
if (typeof value === 'function') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return value;
|
||||
}));
|
||||
// Deep clone and sanitize to avoid modifying original
|
||||
return JSON.parse(
|
||||
JSON.stringify(messageContent, (key, value) => {
|
||||
// Convert Long objects to numbers
|
||||
if (Long.isLong(value)) {
|
||||
return value.toNumber();
|
||||
}
|
||||
|
||||
return sanitized;
|
||||
// Convert Uint8Array to regular arrays
|
||||
if (value instanceof Uint8Array) {
|
||||
return Array.from(value);
|
||||
}
|
||||
|
||||
// Remove functions and other non-serializable objects
|
||||
if (typeof value === 'function') {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Handle objects with toJSON method
|
||||
if (value && typeof value === 'object' && typeof value.toJSON === 'function') {
|
||||
return value.toJSON();
|
||||
}
|
||||
|
||||
// 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 {
|
||||
@ -4330,7 +4347,7 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
const contentMsg = message?.message[contentType] as any;
|
||||
|
||||
const messageRaw = {
|
||||
key: message.key,
|
||||
key: message.key, // Save key exactly as it comes from Baileys
|
||||
pushName:
|
||||
message.pushName ||
|
||||
(message.key.fromMe
|
||||
@ -4338,10 +4355,10 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
: message?.participant || (message.key?.participant ? message.key.participant.split('@')[0] : null)),
|
||||
status: status[message.status],
|
||||
message: this.sanitizeMessageContent({ ...message.message }),
|
||||
contextInfo: contentMsg?.contextInfo,
|
||||
contextInfo: this.sanitizeMessageContent(contentMsg?.contextInfo),
|
||||
messageType: contentType || 'unknown',
|
||||
messageTimestamp: Long.isLong(message.messageTimestamp)
|
||||
? (message.messageTimestamp as Long).toNumber()
|
||||
messageTimestamp: Long.isLong(message.messageTimestamp)
|
||||
? (message.messageTimestamp as Long).toNumber()
|
||||
: (message.messageTimestamp as number),
|
||||
instanceId: this.instanceId,
|
||||
source: getDevice(message.key.id),
|
||||
|
||||
@ -1561,13 +1561,24 @@ export class ChatwootService {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use the message ID directly instead of JSON path query
|
||||
await this.prismaRepository.message.updateMany({
|
||||
where: {
|
||||
key: {
|
||||
path: ['id'],
|
||||
equals: key.id,
|
||||
},
|
||||
instanceId: instance.instanceId,
|
||||
AND: [
|
||||
{ instanceId: instance.instanceId },
|
||||
{
|
||||
OR: [
|
||||
{ id: message.id }, // Use the actual message ID if available
|
||||
// Fallback to raw query if needed
|
||||
{
|
||||
key: {
|
||||
path: ['id'],
|
||||
equals: key.id,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
data: {
|
||||
chatwootMessageId: chatwootMessageIds.messageId,
|
||||
@ -1584,13 +1595,15 @@ export class ChatwootService {
|
||||
}
|
||||
|
||||
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({
|
||||
where: {
|
||||
instanceId: instance.instanceId,
|
||||
// Use raw query to avoid JSON path issues
|
||||
key: {
|
||||
path: ['id'],
|
||||
equals: keyId,
|
||||
},
|
||||
instanceId: instance.instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user