@@ -666,7 +666,6 @@ export class BaileysStartupService extends ChannelStartupService {
666666 instanceId : this . instanceId ,
667667 remoteJid : chat . id ,
668668 name : chat . name ,
669- unreadMessages : typeof chat . unreadCount === 'number' ? chat . unreadCount : 0 ,
670669 } ,
671670 data : { remoteJid : chat . id } ,
672671 } ) ;
@@ -1106,6 +1105,11 @@ export class BaileysStartupService extends ChannelStartupService {
11061105 data : messageRaw ,
11071106 } ) ;
11081107
1108+ if ( received . key . fromMe === false && msg . status === status [ 3 ] ) {
1109+ // is received not read message
1110+ await this . updateChatUnreadMessages ( received . key . remoteJid ) ;
1111+ }
1112+
11091113 if ( isMedia ) {
11101114 if ( this . configService . get < S3 > ( 'S3' ) . ENABLE ) {
11111115 try {
@@ -1237,7 +1241,7 @@ export class BaileysStartupService extends ChannelStartupService {
12371241 } ,
12381242
12391243 'messages.update' : async ( args : WAMessageUpdate [ ] , settings : any ) => {
1240- const unreadChatToUpdate : Record < string , number > = { } ; // { remoteJid: readedMessages}
1244+ const readChatToUpdate : Record < string , true > = { } ; // remoteJid[]
12411245
12421246 for await ( const { key, update } of args ) {
12431247 if ( settings ?. groupsIgnore && key . remoteJid ?. includes ( '@g.us' ) ) {
@@ -1309,13 +1313,28 @@ export class BaileysStartupService extends ChannelStartupService {
13091313
13101314 return ;
13111315 } else if ( update . status !== undefined && status [ update . status ] !== findMessage . status ) {
1312- if ( ! unreadChatToUpdate [ key . remoteJid ! ] ) {
1313- unreadChatToUpdate [ key . remoteJid ! ] = 0 ;
1316+ if ( ! key . fromMe && key . remoteJid ) {
1317+ readChatToUpdate [ key . remoteJid ] = true ;
1318+
1319+ if ( status [ update . status ] === status [ 4 ] ) {
1320+ // Mark to read old messages
1321+ await this . prismaRepository . message . updateMany ( {
1322+ where : {
1323+ AND : [
1324+ { key : { path : [ 'remoteJid' ] , equals : key . remoteJid } } ,
1325+ { key : { path : [ 'fromMe' ] , equals : false } } ,
1326+ { messageTimestamp : { lt : findMessage . messageTimestamp } } , // Delivered messages
1327+ {
1328+ OR : [ { status : null } , { status : status [ 3 ] } ] ,
1329+ } ,
1330+ ] ,
1331+ } ,
1332+ data : { status : status [ 4 ] } ,
1333+ } ) ;
1334+ }
13141335 }
13151336
1316- unreadChatToUpdate [ key . remoteJid ! ] ++ ;
1317-
1318- this . prismaRepository . message . update ( {
1337+ await this . prismaRepository . message . update ( {
13191338 where : { id : findMessage . id } ,
13201339 data : { status : status [ update . status ] } ,
13211340 } ) ;
@@ -1341,16 +1360,7 @@ export class BaileysStartupService extends ChannelStartupService {
13411360 }
13421361 }
13431362
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- }
1363+ await Promise . all ( Object . keys ( readChatToUpdate ) . map ( ( remoteJid ) => this . updateChatUnreadMessages ( remoteJid ) ) ) ;
13541364 } ,
13551365 } ;
13561366
@@ -3708,6 +3718,10 @@ export class BaileysStartupService extends ChannelStartupService {
37083718 source : getDevice ( message . key . id ) ,
37093719 } ;
37103720
3721+ if ( ! messageRaw . status && message . key . fromMe === false ) {
3722+ messageRaw . status = status [ 3 ] ; // DELIVERED MESSAGE
3723+ }
3724+
37113725 if ( messageRaw . message . extendedTextMessage ) {
37123726 messageRaw . messageType = 'conversation' ;
37133727 messageRaw . message . conversation = messageRaw . message . extendedTextMessage . text ;
@@ -3735,4 +3749,28 @@ export class BaileysStartupService extends ChannelStartupService {
37353749 task . start ( ) ;
37363750 }
37373751 }
3752+
3753+ private async updateChatUnreadMessages ( remoteJid : string ) : Promise < number > {
3754+ const [ chat , unreadMessages ] = await Promise . all ( [
3755+ this . prismaRepository . chat . findFirst ( { where : { remoteJid } } ) ,
3756+ this . prismaRepository . message . count ( {
3757+ where : {
3758+ AND : [
3759+ { key : { path : [ 'remoteJid' ] , equals : remoteJid } } ,
3760+ { key : { path : [ 'fromMe' ] , equals : false } } ,
3761+ { status : { equals : status [ 3 ] } } ,
3762+ ] ,
3763+ } ,
3764+ } ) ,
3765+ ] ) ;
3766+
3767+ if ( chat && chat . unreadMessages !== unreadMessages ) {
3768+ await this . prismaRepository . chat . update ( {
3769+ where : { id : chat . id } ,
3770+ data : { unreadMessages } ,
3771+ } ) ;
3772+ }
3773+
3774+ return unreadMessages ;
3775+ }
37383776}
0 commit comments