@@ -661,6 +661,7 @@ export class BaileysStartupService extends ChannelStartupService {
661661 instanceId : this . instanceId ,
662662 remoteJid : chat . id ,
663663 name : chat . name ,
664+ unreadMessages : typeof chat . unreadCount === 'number' ? chat . unreadCount : 0 ,
664665 } ,
665666 data : { remoteJid : chat . id } ,
666667 } ) ;
@@ -1231,6 +1232,8 @@ export class BaileysStartupService extends ChannelStartupService {
12311232 } ,
12321233
12331234 'messages.update' : async ( args : WAMessageUpdate [ ] , settings : any ) => {
1235+ const unreadChatToUpdate : Record < string , number > = { } ; // {remoteJid: readedMessages}
1236+
12341237 for await ( const { key, update } of args ) {
12351238 if ( settings ?. groupsIgnore && key . remoteJid ?. includes ( '@g.us' ) ) {
12361239 return ;
@@ -1273,7 +1276,7 @@ export class BaileysStartupService extends ChannelStartupService {
12731276 return ;
12741277 }
12751278
1276- if ( status [ update . status ] === 'READ' && ! key . fromMe ) return ;
1279+ // if (status[update.status] === 'READ' && !key.fromMe) return;
12771280
12781281 if ( update . message === null && update . status === undefined ) {
12791282 this . sendDataWebhook ( Events . MESSAGES_DELETE , key ) ;
@@ -1302,6 +1305,17 @@ export class BaileysStartupService extends ChannelStartupService {
13021305 }
13031306
13041307 return ;
1308+ } else if ( update . message !== undefined && update . message !== findMessage . status ) {
1309+ if ( unreadChatToUpdate [ key . remoteJid ! ] === undefined ) {
1310+ unreadChatToUpdate [ key . remoteJid ! ] = 0 ;
1311+ }
1312+
1313+ unreadChatToUpdate [ key . remoteJid ! ] ++ ;
1314+
1315+ this . prismaRepository . message . update ( {
1316+ where : { id : findMessage . id } ,
1317+ data : { status : status [ update . status ] } ,
1318+ } ) ;
13051319 }
13061320
13071321 const message : any = {
@@ -1323,6 +1337,17 @@ export class BaileysStartupService extends ChannelStartupService {
13231337 } ) ;
13241338 }
13251339 }
1340+
1341+ for await ( const [ remoteJid , unreadMessages ] of Object . entries ( unreadChatToUpdate ) ) {
1342+ const chat = await this . prismaRepository . chat . findFirst ( { where : { remoteJid } } ) ;
1343+
1344+ if ( chat ) {
1345+ this . prismaRepository . chat . update ( {
1346+ where : { id : chat . id } ,
1347+ data : { unreadMessages : Math . max ( 0 , chat . unreadMessages - unreadMessages ) } ,
1348+ } ) ;
1349+ }
1350+ }
13261351 } ,
13271352 } ;
13281353
@@ -2006,7 +2031,13 @@ export class BaileysStartupService extends ChannelStartupService {
20062031
20072032 const mimetype = mime . getType ( fileName ) . toString ( ) ;
20082033
2009- const fullName = join ( `${ this . instance . id } ` , messageRaw . key . remoteJid , `${ messageRaw . key . id } ` , mediaType , fileName ) ;
2034+ const fullName = join (
2035+ `${ this . instance . id } ` ,
2036+ messageRaw . key . remoteJid ,
2037+ `${ messageRaw . key . id } ` ,
2038+ mediaType ,
2039+ fileName ,
2040+ ) ;
20102041
20112042 await s3Service . uploadFile ( fullName , buffer , size . fileLength ?. low , {
20122043 'Content-Type' : mimetype ,
0 commit comments