Skip to content

Commit 6e1d027

Browse files
Anderson SilvaAnderson Silva
authored andcommitted
feat(chatwoot): comprehensive improvements to message handling, editing, deletion and i18n
- Fix bidirectional message deletion between Chatwoot and WhatsApp - Support deletion of multiple attachments sent together - Implement proper message editing with 'Edited Message:' prefix format - Enable deletion of edited messages by updating chatwootMessageId - Skip cache for deleted messages (messageStubType === 1) to prevent duplicates - Fix i18n translation path detection for production environment - Add automatic dev/prod path resolution for translation files - Improve error handling and logging for message operations Technical improvements: - Changed Chatwoot deletion query from findFirst to findMany for multiple attachments - Fixed instanceId override issue in message deletion payload - Added retry logic with Prisma MessageUpdate validation - Implemented cache bypass for revoked messages to ensure proper processing - Enhanced i18n to detect dist/ folder in production vs src/ in development Resolves issues with: - Message deletion not working from Chatwoot to WhatsApp - Multiple attachments causing incomplete deletion - Edited messages showing raw i18n keys instead of translated text - Cache collision preventing deletion of edited messages - Production environment not loading translation files correctly Note: Tested and validated with Chatwoot v4.1 in production environment
1 parent 78c7b96 commit 6e1d027

File tree

3 files changed

+407
-97
lines changed

3 files changed

+407
-97
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,11 @@ export class BaileysStartupService extends ChannelStartupService {
10651065
settings: any,
10661066
) => {
10671067
try {
1068+
// Garantir que localChatwoot está carregado antes de processar mensagens
1069+
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && !this.localChatwoot?.enabled) {
1070+
await this.loadChatwoot();
1071+
}
1072+
10681073
for (const received of messages) {
10691074
if (received.key.remoteJid?.includes('@lid') && (received.key as ExtendedMessageKey).senderPn) {
10701075
(received.key as ExtendedMessageKey).previousRemoteJid = received.key.remoteJid;
@@ -1445,12 +1450,17 @@ export class BaileysStartupService extends ChannelStartupService {
14451450

14461451
const cached = await this.baileysCache.get(updateKey);
14471452

1448-
if (cached) {
1453+
// Não ignorar mensagens deletadas (messageStubType === 1) mesmo que estejam em cache
1454+
const isDeletedMessage = update.messageStubType === 1;
1455+
1456+
if (cached && !isDeletedMessage) {
14491457
this.logger.info(`Message duplicated ignored [avoid deadlock]: ${updateKey}`);
14501458
continue;
14511459
}
14521460

1453-
await this.baileysCache.set(updateKey, true, 30 * 60);
1461+
if (!isDeletedMessage) {
1462+
await this.baileysCache.set(updateKey, true, 30 * 60);
1463+
}
14541464

14551465
if (status[update.status] === 'READ' && key.fromMe) {
14561466
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot?.enabled) {
@@ -1550,8 +1560,22 @@ export class BaileysStartupService extends ChannelStartupService {
15501560

15511561
this.sendDataWebhook(Events.MESSAGES_UPDATE, message);
15521562

1553-
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE)
1554-
await this.prismaRepository.messageUpdate.create({ data: message });
1563+
if (this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
1564+
// Verificar se a mensagem ainda existe antes de criar o update
1565+
const messageExists = await this.prismaRepository.message.findFirst({
1566+
where: {
1567+
instanceId: message.instanceId,
1568+
key: {
1569+
path: ['id'],
1570+
equals: message.keyId,
1571+
},
1572+
},
1573+
});
1574+
1575+
if (messageExists) {
1576+
await this.prismaRepository.messageUpdate.create({ data: message });
1577+
}
1578+
}
15551579

15561580
const existingChat = await this.prismaRepository.chat.findFirst({
15571581
where: { instanceId: this.instanceId, remoteJid: message.remoteJid },

0 commit comments

Comments
 (0)