@@ -561,53 +561,44 @@ export class ChatwootService {
561561
562562 const picture_url = await this . waMonitor . waInstances [ instance . instanceName ] . profilePicture ( chatId ) ;
563563
564- const findContact = await this . findContact ( instance , chatId ) ;
565-
566- let contact : any ;
567- if ( body . key . fromMe ) {
568- if ( findContact ) {
569- contact = await this . updateContact ( instance , findContact . id , {
570- avatar_url : picture_url . profilePictureUrl || null ,
571- } ) ;
572- } else {
573- const jid = isGroup ? null : body . key . remoteJid ;
574- contact = await this . createContact (
575- instance ,
576- chatId ,
577- filterInbox . id ,
578- isGroup ,
579- nameContact ,
580- picture_url . profilePictureUrl || null ,
581- jid ,
582- ) ;
583- }
584- } else {
585- if ( findContact ) {
586- if ( ! findContact . name || findContact . name === chatId ) {
587- contact = await this . updateContact ( instance , findContact . id , {
588- name : nameContact ,
589- avatar_url : picture_url . profilePictureUrl || null ,
590- } ) ;
591- } else {
592- contact = await this . updateContact ( instance , findContact . id , {
593- avatar_url : picture_url . profilePictureUrl || null ,
564+ let contact = await this . findContact ( instance , chatId ) ;
565+
566+ if ( contact ) {
567+ if ( ! body . key . fromMe ) {
568+ const waProfilePictureFile =
569+ picture_url ?. profilePictureUrl ?. split ( '#' ) [ 0 ] . split ( '?' ) [ 0 ] . split ( '/' ) . pop ( ) || '' ;
570+ const chatwootProfilePictureFile = contact ?. thumbnail ?. split ( '#' ) [ 0 ] . split ( '?' ) [ 0 ] . split ( '/' ) . pop ( ) || '' ;
571+ const pictureNeedsUpdate = waProfilePictureFile !== chatwootProfilePictureFile ;
572+ const nameNeedsUpdate =
573+ ! contact . name ||
574+ contact . name === chatId ||
575+ ( `+${ chatId } ` . startsWith ( '+55' )
576+ ? this . getNumbers ( `+${ chatId } ` ) . some (
577+ ( v ) => contact . name === v || contact . name === v . substring ( 3 ) || contact . name === v . substring ( 1 ) ,
578+ )
579+ : false ) ;
580+
581+ const contactNeedsUpdate = pictureNeedsUpdate || nameNeedsUpdate ;
582+ if ( contactNeedsUpdate ) {
583+ this . logger . verbose ( 'update contact in chatwoot' ) ;
584+ contact = await this . updateContact ( instance , contact . id , {
585+ ...( nameNeedsUpdate && { name : nameContact } ) ,
586+ ...( waProfilePictureFile === '' && { avatar : null } ) ,
587+ ...( pictureNeedsUpdate && { avatar_url : picture_url ?. profilePictureUrl } ) ,
594588 } ) ;
595589 }
596- if ( ! contact ) {
597- contact = await this . findContact ( instance , chatId ) ;
598- }
599- } else {
600- const jid = isGroup ? null : body . key . remoteJid ;
601- contact = await this . createContact (
602- instance ,
603- chatId ,
604- filterInbox . id ,
605- isGroup ,
606- nameContact ,
607- picture_url . profilePictureUrl || null ,
608- jid ,
609- ) ;
610590 }
591+ } else {
592+ const jid = isGroup ? null : body . key . remoteJid ;
593+ contact = await this . createContact (
594+ instance ,
595+ chatId ,
596+ filterInbox . id ,
597+ isGroup ,
598+ nameContact ,
599+ picture_url . profilePictureUrl || null ,
600+ jid ,
601+ ) ;
611602 }
612603
613604 if ( ! contact ) {
@@ -617,13 +608,6 @@ export class ChatwootService {
617608
618609 const contactId = contact ?. payload ?. id || contact ?. payload ?. contact ?. id || contact ?. id ;
619610
620- if ( ! body . key . fromMe && contact . name === chatId && nameContact !== chatId ) {
621- this . logger . verbose ( 'update contact name in chatwoot' ) ;
622- await this . updateContact ( instance , contactId , {
623- name : nameContact ,
624- } ) ;
625- }
626-
627611 this . logger . verbose ( 'get contact conversations in chatwoot' ) ;
628612 const contactConversations = ( await client . contacts . listConversations ( {
629613 accountId : this . provider . account_id ,
0 commit comments