@@ -627,7 +627,12 @@ export class BaileysStartupService extends ChannelStartupService {
627627
628628 const chatsToInsert = chats
629629 . filter ( ( chat ) => ! existingChatIdSet ?. has ( chat . id ) )
630- . map ( ( chat ) => ( { remoteJid : chat . id , instanceId : this . instanceId , name : chat . name } ) ) ;
630+ . map ( ( chat ) => ( {
631+ remoteJid : chat . id ,
632+ instanceId : this . instanceId ,
633+ name : chat . name ,
634+ unreadMessages : chat . unreadCount !== undefined ? chat . unreadCount : 0 ,
635+ } ) ) ;
631636
632637 this . sendDataWebhook ( Events . CHATS_UPSERT , chatsToInsert ) ;
633638
@@ -661,6 +666,7 @@ export class BaileysStartupService extends ChannelStartupService {
661666 instanceId : this . instanceId ,
662667 remoteJid : chat . id ,
663668 name : chat . name ,
669+ unreadMessages : typeof chat . unreadCount === 'number' ? chat . unreadCount : 0 ,
664670 } ,
665671 data : { remoteJid : chat . id } ,
666672 } ) ;
@@ -1231,6 +1237,8 @@ export class BaileysStartupService extends ChannelStartupService {
12311237 } ,
12321238
12331239 'messages.update' : async ( args : WAMessageUpdate [ ] , settings : any ) => {
1240+ const unreadChatToUpdate : Record < string , number > = { } ; // {remoteJid: readedMessages}
1241+
12341242 for await ( const { key, update } of args ) {
12351243 if ( settings ?. groupsIgnore && key . remoteJid ?. includes ( '@g.us' ) ) {
12361244 return ;
@@ -1273,8 +1281,6 @@ export class BaileysStartupService extends ChannelStartupService {
12731281 return ;
12741282 }
12751283
1276- if ( status [ update . status ] === 'READ' && ! key . fromMe ) return ;
1277-
12781284 if ( update . message === null && update . status === undefined ) {
12791285 this . sendDataWebhook ( Events . MESSAGES_DELETE , key ) ;
12801286
@@ -1302,6 +1308,17 @@ export class BaileysStartupService extends ChannelStartupService {
13021308 }
13031309
13041310 return ;
1311+ } else if ( update . status !== undefined && status [ update . status ] !== findMessage . status ) {
1312+ if ( ! unreadChatToUpdate [ key . remoteJid ! ] ) {
1313+ unreadChatToUpdate [ key . remoteJid ! ] = 0 ;
1314+ }
1315+
1316+ unreadChatToUpdate [ key . remoteJid ! ] ++ ;
1317+
1318+ this . prismaRepository . message . update ( {
1319+ where : { id : findMessage . id } ,
1320+ data : { status : status [ update . status ] } ,
1321+ } ) ;
13051322 }
13061323
13071324 const message : any = {
@@ -1323,6 +1340,17 @@ export class BaileysStartupService extends ChannelStartupService {
13231340 } ) ;
13241341 }
13251342 }
1343+
1344+ for await ( const [ remoteJid , unreadMessages ] of Object . entries ( unreadChatToUpdate ) ) {
1345+ const chat = await this . prismaRepository . chat . findFirst ( { where : { remoteJid } } ) ;
1346+
1347+ if ( chat ) {
1348+ this . prismaRepository . chat . update ( {
1349+ where : { id : chat . id } ,
1350+ data : { unreadMessages : Math . max ( 0 , chat . unreadMessages - unreadMessages ) } ,
1351+ } ) ;
1352+ }
1353+ }
13261354 } ,
13271355 } ;
13281356
@@ -2006,7 +2034,13 @@ export class BaileysStartupService extends ChannelStartupService {
20062034
20072035 const mimetype = mime . getType ( fileName ) . toString ( ) ;
20082036
2009- const fullName = join ( `${ this . instance . id } ` , messageRaw . key . remoteJid , `${ messageRaw . key . id } ` , mediaType , fileName ) ;
2037+ const fullName = join (
2038+ `${ this . instance . id } ` ,
2039+ messageRaw . key . remoteJid ,
2040+ `${ messageRaw . key . id } ` ,
2041+ mediaType ,
2042+ fileName ,
2043+ ) ;
20102044
20112045 await s3Service . uploadFile ( fullName , buffer , size . fileLength ?. low , {
20122046 'Content-Type' : mimetype ,
0 commit comments