Skip to content

Commit 0987fda

Browse files
Merge pull request #970 from jesus-chacon/bugs/mark_as_read_fromMe_and_groups
Mark as read from me and groups
2 parents d50da79 + 5b91648 commit 0987fda

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import makeWASocket, {
101101
isJidUser,
102102
makeCacheableSignalKeyStore,
103103
MessageUpsertType,
104+
MessageUserReceiptUpdate,
104105
MiscMessageGenerationOptions,
105106
ParticipantAction,
106107
prepareWAMessageMedia,
@@ -1112,12 +1113,18 @@ export class BaileysStartupService extends ChannelStartupService {
11121113
if (received.key.fromMe === false) {
11131114
if (msg.status === status[3]) {
11141115
this.logger.log(`Update not read messages ${received.key.remoteJid}`);
1115-
// is received not read message
1116+
11161117
await this.updateChatUnreadMessages(received.key.remoteJid);
11171118
} else if (msg.status === status[4]) {
11181119
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
1119-
this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
1120+
1121+
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
11201122
}
1123+
} else {
1124+
// is send message by me
1125+
this.logger.log(`Update readed messages ${received.key.remoteJid} - ${msg.messageTimestamp}`);
1126+
1127+
await this.updateMessagesReadedByTimestamp(received.key.remoteJid, msg.messageTimestamp);
11211128
}
11221129

11231130
if (isMedia) {
@@ -1250,6 +1257,8 @@ export class BaileysStartupService extends ChannelStartupService {
12501257
},
12511258

12521259
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
1260+
this.logger.log(`Update messages ${JSON.stringify(args, undefined, 2)}`);
1261+
12531262
const readChatToUpdate: Record<string, true> = {}; // {remoteJid: true}
12541263

12551264
for await (const { key, update } of args) {
@@ -1514,12 +1523,30 @@ export class BaileysStartupService extends ChannelStartupService {
15141523
this.messageHandle['messages.update'](payload, settings);
15151524
}
15161525

1526+
if (events['message-receipt.update']) {
1527+
const payload = events['message-receipt.update'] as MessageUserReceiptUpdate[];
1528+
const remotesJidMap: Record<string, number> = {};
1529+
1530+
for (const event of payload) {
1531+
if (typeof event.key.remoteJid === 'string' && typeof event.receipt.readTimestamp === 'number') {
1532+
remotesJidMap[event.key.remoteJid] = event.receipt.readTimestamp;
1533+
}
1534+
}
1535+
1536+
await Promise.all(
1537+
Object.keys(remotesJidMap).map(async (remoteJid) =>
1538+
this.updateMessagesReadedByTimestamp(remoteJid, remotesJidMap[remoteJid]),
1539+
),
1540+
);
1541+
}
1542+
15171543
if (events['presence.update']) {
15181544
const payload = events['presence.update'];
15191545

15201546
if (settings?.groupsIgnore && payload.id.includes('@g.us')) {
15211547
return;
15221548
}
1549+
15231550
this.sendDataWebhook(Events.PRESENCE_UPDATE, payload);
15241551
}
15251552

@@ -3768,6 +3795,10 @@ export class BaileysStartupService extends ChannelStartupService {
37683795
});
37693796

37703797
if (result) {
3798+
if (result.count > 0) {
3799+
this.updateChatUnreadMessages(remoteJid);
3800+
}
3801+
37713802
return result.count;
37723803
}
37733804

0 commit comments

Comments
 (0)