@@ -1122,7 +1122,9 @@ export class ChatwootService {
11221122
11231123 data . append ( 'message_type' , messageType ) ;
11241124
1125- data . append ( 'attachments[]' , fileStream , { filename : fileName } ) ;
1125+ if ( fileData && fileName ) {
1126+ data . append ( 'attachments[]' , fileData , { filename : fileName } ) ;
1127+ }
11261128
11271129 const sourceReplyId = quotedMsg ?. chatwootMessageId || null ;
11281130
@@ -1487,6 +1489,59 @@ export class ChatwootService {
14871489 } ) ;
14881490 }
14891491
1492+ /**
1493+ * Processa deleção de mensagem em background
1494+ * Método assíncrono chamado via setImmediate para não bloquear resposta do webhook
1495+ */
1496+ private async processDeletion ( instance : InstanceDto , body : any , deleteLockKey : string ) {
1497+ this . logger . warn ( `[DELETE] 🗑️ Processing deletion - messageId: ${ body . id } ` ) ;
1498+ const waInstance = this . waMonitor . waInstances [ instance . instanceName ] ;
1499+
1500+ // Buscar TODAS as mensagens com esse chatwootMessageId (pode ser múltiplos anexos)
1501+ const messages = await this . prismaRepository . message . findMany ( {
1502+ where : {
1503+ chatwootMessageId : body . id ,
1504+ instanceId : instance . instanceId ,
1505+ } ,
1506+ } ) ;
1507+
1508+ if ( messages && messages . length > 0 ) {
1509+ this . logger . warn ( `[DELETE] Found ${ messages . length } message(s) to delete from Chatwoot message ${ body . id } ` ) ;
1510+ this . logger . verbose ( `[DELETE] Messages keys: ${ messages . map ( ( m ) => ( m . key as any ) ?. id ) . join ( ', ' ) } ` ) ;
1511+
1512+ // Deletar cada mensagem no WhatsApp
1513+ for ( const message of messages ) {
1514+ const key = message . key as ExtendedMessageKey ;
1515+ this . logger . warn (
1516+ `[DELETE] Attempting to delete WhatsApp message - keyId: ${ key ?. id } , remoteJid: ${ key ?. remoteJid } ` ,
1517+ ) ;
1518+
1519+ try {
1520+ await waInstance ?. client . sendMessage ( key . remoteJid , { delete : key } ) ;
1521+ this . logger . warn ( `[DELETE] ✅ Message ${ key . id } deleted in WhatsApp successfully` ) ;
1522+ } catch ( error ) {
1523+ this . logger . error ( `[DELETE] ❌ Error deleting message ${ key . id } in WhatsApp: ${ error } ` ) ;
1524+ this . logger . error ( `[DELETE] Error details: ${ JSON . stringify ( error , null , 2 ) } ` ) ;
1525+ }
1526+ }
1527+
1528+ // Remover todas as mensagens do banco de dados
1529+ await this . prismaRepository . message . deleteMany ( {
1530+ where : {
1531+ instanceId : instance . instanceId ,
1532+ chatwootMessageId : body . id ,
1533+ } ,
1534+ } ) ;
1535+ this . logger . warn ( `[DELETE] ✅ SUCCESS: ${ messages . length } message(s) deleted from WhatsApp and database` ) ;
1536+ } else {
1537+ // Mensagem não encontrada - pode ser uma mensagem antiga que foi substituída por edição
1538+ this . logger . warn ( `[DELETE] ⚠️ WARNING: Message not found in DB - chatwootMessageId: ${ body . id } ` ) ;
1539+ }
1540+
1541+ // Liberar lock após processar
1542+ await this . cache . delete ( deleteLockKey ) ;
1543+ }
1544+
14901545 public async receiveWebhook ( instance : InstanceDto , body : any ) {
14911546 try {
14921547 // IMPORTANTE: Verificar lock de deleção ANTES do delay inicial
@@ -1545,55 +1600,25 @@ export class ChatwootService {
15451600 // Lock já foi adquirido no início do método (antes do delay)
15461601 const deleteLockKey = `${ instance . instanceName } :deleteMessage-${ body . id } ` ;
15471602
1548- this . logger . warn ( `[DELETE] 🗑️ Processing deletion - messageId: ${ body . id } ` ) ;
1549- const waInstance = this . waMonitor . waInstances [ instance . instanceName ] ;
1603+ // ESTRATÉGIA: Processar em background e responder IMEDIATAMENTE
1604+ // Isso evita timeout do Chatwoot (5s) quando há muitas imagens (> 5s de processamento)
1605+ this . logger . warn ( `[DELETE] 🚀 Starting background deletion - messageId: ${ body . id } ` ) ;
15501606
1551- // Buscar TODAS as mensagens com esse chatwootMessageId (pode ser múltiplos anexos)
1552- const messages = await this . prismaRepository . message . findMany ( {
1553- where : {
1554- chatwootMessageId : body . id ,
1555- instanceId : instance . instanceId ,
1556- } ,
1557- } ) ;
1558-
1559- if ( messages && messages . length > 0 ) {
1560- this . logger . warn ( `[DELETE] Found ${ messages . length } message(s) to delete from Chatwoot message ${ body . id } ` ) ;
1561- this . logger . verbose ( `[DELETE] Messages keys: ${ messages . map ( ( m ) => ( m . key as any ) ?. id ) . join ( ', ' ) } ` ) ;
1562-
1563- // Deletar cada mensagem no WhatsApp
1564- for ( const message of messages ) {
1565- const key = message . key as ExtendedMessageKey ;
1566- this . logger . warn (
1567- `[DELETE] Attempting to delete WhatsApp message - keyId: ${ key ?. id } , remoteJid: ${ key ?. remoteJid } ` ,
1568- ) ;
1569-
1570- try {
1571- await waInstance ?. client . sendMessage ( key . remoteJid , { delete : key } ) ;
1572- this . logger . warn ( `[DELETE] ✅ Message ${ key . id } deleted in WhatsApp successfully` ) ;
1573- } catch ( error ) {
1574- this . logger . error ( `[DELETE] ❌ Error deleting message ${ key . id } in WhatsApp: ${ error } ` ) ;
1575- this . logger . error ( `[DELETE] Error details: ${ JSON . stringify ( error , null , 2 ) } ` ) ;
1576- }
1607+ // Executar em background (sem await) - não bloqueia resposta do webhook
1608+ setImmediate ( async ( ) => {
1609+ try {
1610+ await this . processDeletion ( instance , body , deleteLockKey ) ;
1611+ } catch ( error ) {
1612+ this . logger . error ( `[DELETE] ❌ Background deletion failed for messageId ${ body . id } : ${ error } ` ) ;
15771613 }
1614+ } ) ;
15781615
1579- // Remover todas as mensagens do banco de dados
1580- await this . prismaRepository . message . deleteMany ( {
1581- where : {
1582- instanceId : instance . instanceId ,
1583- chatwootMessageId : body . id ,
1584- } ,
1585- } ) ;
1586- this . logger . warn ( `[DELETE] ✅ SUCCESS: ${ messages . length } message(s) deleted from WhatsApp and database` ) ;
1587- } else {
1588- // Mensagem não encontrada - pode ser uma mensagem antiga que foi substituída por edição
1589- // Nesse caso, ignoramos silenciosamente pois o ID já foi atualizado no banco
1590- this . logger . warn ( `[DELETE] ⚠️ WARNING: Message not found in DB - chatwootMessageId: ${ body . id } ` ) ;
1591- }
1592-
1593- // Liberar lock após processar
1594- await this . cache . delete ( deleteLockKey ) ;
1595-
1596- return { message : 'deleted' } ;
1616+ // RESPONDER IMEDIATAMENTE ao Chatwoot (< 50ms)
1617+ return {
1618+ message : 'deletion_accepted' ,
1619+ messageId : body . id ,
1620+ note : 'Deletion is being processed in background' ,
1621+ } ;
15971622 }
15981623
15991624 if (
0 commit comments