@@ -152,7 +152,13 @@ import { v4 } from 'uuid';
152152import { BaileysMessageProcessor } from './baileysMessage.processor' ;
153153import { useVoiceCallsBaileys } from './voiceCalls/useVoiceCallsBaileys' ;
154154
155+ export interface ExtendedMessageKey extends WAMessageKey {
156+ senderPn ?: string ;
157+ previousRemoteJid ?: string | null ;
158+ }
159+
155160export interface ExtendedIMessageKey extends proto . IMessageKey {
161+ senderPn ?: string ;
156162 remoteJidAlt ?: string ;
157163 participantAlt ?: string ;
158164 server_id ?: string ;
@@ -998,6 +1004,10 @@ export class BaileysStartupService extends ChannelStartupService {
9981004 continue ;
9991005 }
10001006
1007+ if ( m . key . remoteJid ?. includes ( '@lid' ) && ( m . key as ExtendedIMessageKey ) . senderPn ) {
1008+ m . key . remoteJid = ( m . key as ExtendedIMessageKey ) . senderPn ;
1009+ }
1010+
10011011 if ( Long . isLong ( m ?. messageTimestamp ) ) {
10021012 m . messageTimestamp = m . messageTimestamp ?. toNumber ( ) ;
10031013 }
@@ -1059,7 +1069,16 @@ export class BaileysStartupService extends ChannelStartupService {
10591069 settings : any ,
10601070 ) => {
10611071 try {
1072+ // Garantir que localChatwoot está carregado antes de processar mensagens
1073+ if ( this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED && ! this . localChatwoot ?. enabled ) {
1074+ await this . loadChatwoot ( ) ;
1075+ }
1076+
10621077 for ( const received of messages ) {
1078+ if ( received . key . remoteJid ?. includes ( '@lid' ) && ( received . key as ExtendedMessageKey ) . senderPn ) {
1079+ ( received . key as ExtendedMessageKey ) . previousRemoteJid = received . key . remoteJid ;
1080+ received . key . remoteJid = ( received . key as ExtendedMessageKey ) . senderPn ;
1081+ }
10631082 if (
10641083 received ?. messageStubParameters ?. some ?.( ( param ) =>
10651084 [
@@ -1107,9 +1126,9 @@ export class BaileysStartupService extends ChannelStartupService {
11071126 await this . sendDataWebhook ( Events . MESSAGES_EDITED , editedMessage ) ;
11081127 const oldMessage = await this . getMessage ( editedMessage . key , true ) ;
11091128 if ( ( oldMessage as any ) ?. id ) {
1110- const editedMessageTimestamp = Long . isLong ( received ?. messageTimestamp )
1111- ? Math . floor ( received ?. messageTimestamp . toNumber ( ) )
1112- : Math . floor ( received ?. messageTimestamp as number ) ;
1129+ const editedMessageTimestamp = Long . isLong ( editedMessage ?. timestampMs )
1130+ ? Math . floor ( editedMessage . timestampMs . toNumber ( ) / 1000 )
1131+ : Math . floor ( ( editedMessage . timestampMs as number ) / 1000 ) ;
11131132
11141133 await this . prismaRepository . message . update ( {
11151134 where : { id : ( oldMessage as any ) . id } ,
@@ -1348,6 +1367,10 @@ export class BaileysStartupService extends ChannelStartupService {
13481367 }
13491368 }
13501369
1370+ if ( messageRaw . key . remoteJid ?. includes ( '@lid' ) && messageRaw . key . remoteJidAlt ) {
1371+ messageRaw . key . remoteJid = messageRaw . key . remoteJidAlt ;
1372+ }
1373+
13511374 this . logger . log ( messageRaw ) ;
13521375
13531376 this . sendDataWebhook ( Events . MESSAGES_UPSERT , messageRaw ) ;
@@ -1423,18 +1446,25 @@ export class BaileysStartupService extends ChannelStartupService {
14231446 continue ;
14241447 }
14251448
1426- if ( update . message !== null && update . status === undefined ) continue ;
1449+ if ( key . remoteJid ?. includes ( '@lid' ) && key . remoteJidAlt ) {
1450+ key . remoteJid = key . remoteJidAlt ;
1451+ }
14271452
14281453 const updateKey = `${ this . instance . id } _${ key . id } _${ update . status } ` ;
14291454
14301455 const cached = await this . baileysCache . get ( updateKey ) ;
14311456
1432- if ( cached ) {
1457+ // Não ignorar mensagens deletadas (messageStubType === 1) mesmo que estejam em cache
1458+ const isDeletedMessage = update . messageStubType === 1 ;
1459+
1460+ if ( cached && ! isDeletedMessage ) {
14331461 this . logger . info ( `Message duplicated ignored [avoid deadlock]: ${ updateKey } ` ) ;
14341462 continue ;
14351463 }
14361464
1437- await this . baileysCache . set ( updateKey , true , 30 * 60 ) ;
1465+ if ( ! isDeletedMessage ) {
1466+ await this . baileysCache . set ( updateKey , true , this . UPDATE_CACHE_TTL_SECONDS ) ;
1467+ }
14381468
14391469 if ( status [ update . status ] === 'READ' && key . fromMe ) {
14401470 if ( this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED && this . localChatwoot ?. enabled ) {
@@ -1464,7 +1494,7 @@ export class BaileysStartupService extends ChannelStartupService {
14641494 keyId : key . id ,
14651495 remoteJid : key ?. remoteJid ,
14661496 fromMe : key . fromMe ,
1467- participant : key ?. participant ,
1497+ participant : key ?. remoteJid ,
14681498 status : status [ update . status ] ?? 'DELETED' ,
14691499 pollUpdates,
14701500 instanceId : this . instanceId ,
@@ -1538,8 +1568,22 @@ export class BaileysStartupService extends ChannelStartupService {
15381568
15391569 this . sendDataWebhook ( Events . MESSAGES_UPDATE , message ) ;
15401570
1541- if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . MESSAGE_UPDATE )
1542- await this . prismaRepository . messageUpdate . create ( { data : message } ) ;
1571+ if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . MESSAGE_UPDATE ) {
1572+ // Verificar se a mensagem ainda existe antes de criar o update
1573+ const messageExists = await this . prismaRepository . message . findFirst ( {
1574+ where : {
1575+ instanceId : message . instanceId ,
1576+ key : {
1577+ path : [ 'id' ] ,
1578+ equals : message . keyId ,
1579+ } ,
1580+ } ,
1581+ } ) ;
1582+
1583+ if ( messageExists ) {
1584+ await this . prismaRepository . messageUpdate . create ( { data : message } ) ;
1585+ }
1586+ }
15431587
15441588 const existingChat = await this . prismaRepository . chat . findFirst ( {
15451589 where : { instanceId : this . instanceId , remoteJid : message . remoteJid } ,
@@ -3409,13 +3453,18 @@ export class BaileysStartupService extends ChannelStartupService {
34093453 }
34103454
34113455 const numberJid = numberVerified ?. jid || user . jid ;
3412-
3456+ // const lid =
3457+ // typeof numberVerified?.lid === 'string'
3458+ // ? numberVerified.lid
3459+ // : numberJid.includes('@lid')
3460+ // ? numberJid.split('@')[1]
3461+ // : undefined;
34133462 return new OnWhatsAppDto (
34143463 numberJid ,
34153464 ! ! numberVerified ?. exists ,
34163465 user . number ,
34173466 contacts . find ( ( c ) => c . remoteJid === numberJid ) ?. pushName ,
3418- undefined ,
3467+ // lid ,
34193468 ) ;
34203469 } ) ,
34213470 ) ;
@@ -3567,7 +3616,7 @@ export class BaileysStartupService extends ChannelStartupService {
35673616 keyId : messageId ,
35683617 remoteJid : response . key . remoteJid ,
35693618 fromMe : response . key . fromMe ,
3570- participant : response . key ?. participant ,
3619+ participant : response . key ?. remoteJid ,
35713620 status : 'DELETED' ,
35723621 instanceId : this . instanceId ,
35733622 } ;
@@ -3627,10 +3676,7 @@ export class BaileysStartupService extends ChannelStartupService {
36273676 }
36283677 }
36293678
3630- if (
3631- Object . keys ( msg . message ) . length === 1 &&
3632- Object . prototype . hasOwnProperty . call ( msg . message , 'messageContextInfo' )
3633- ) {
3679+ if ( 'messageContextInfo' in msg . message && Object . keys ( msg . message ) . length === 1 ) {
36343680 throw 'The message is messageContextInfo' ;
36353681 }
36363682
@@ -4005,7 +4051,7 @@ export class BaileysStartupService extends ChannelStartupService {
40054051 keyId : messageId ,
40064052 remoteJid : messageSent . key . remoteJid ,
40074053 fromMe : messageSent . key . fromMe ,
4008- participant : messageSent . key ?. participant ,
4054+ participant : messageSent . key ?. remoteJid ,
40094055 status : 'EDITED' ,
40104056 instanceId : this . instanceId ,
40114057 } ;
@@ -4601,7 +4647,9 @@ export class BaileysStartupService extends ChannelStartupService {
46014647 return response ;
46024648 }
46034649
4604- public async baileysAssertSessions ( jids : string [ ] ) {
4650+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
4651+ public async baileysAssertSessions ( jids : string [ ] , _force ?: boolean ) {
4652+ // Note: _force parameter kept for API compatibility but not used in Baileys 7.0.0-rc.5+
46054653 const response = await this . client . assertSessions ( jids ) ;
46064654
46074655 return response ;
@@ -4806,7 +4854,7 @@ export class BaileysStartupService extends ChannelStartupService {
48064854 {
48074855 OR : [
48084856 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
4809- keyFilters ?. remoteJidAlt ? { key : { path : [ 'remoteJidAlt ' ] , equals : keyFilters ?. remoteJidAlt } } : { } ,
4857+ keyFilters ?. senderPn ? { key : { path : [ 'senderPn ' ] , equals : keyFilters ?. senderPn } } : { } ,
48104858 ] ,
48114859 } ,
48124860 ] ,
@@ -4836,7 +4884,7 @@ export class BaileysStartupService extends ChannelStartupService {
48364884 {
48374885 OR : [
48384886 keyFilters ?. remoteJid ? { key : { path : [ 'remoteJid' ] , equals : keyFilters ?. remoteJid } } : { } ,
4839- keyFilters ?. remoteJidAlt ? { key : { path : [ 'remoteJidAlt ' ] , equals : keyFilters ?. remoteJidAlt } } : { } ,
4887+ keyFilters ?. senderPn ? { key : { path : [ 'senderPn ' ] , equals : keyFilters ?. senderPn } } : { } ,
48404888 ] ,
48414889 } ,
48424890 ] ,
0 commit comments