Skip to content

Commit edb3875

Browse files
author
Jesus
committed
Added unreadMessages to chats
1 parent 49edd17 commit edb3875

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

prisma/postgresql-schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ model Chat {
123123
updatedAt DateTime? @updatedAt @db.Timestamp
124124
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
125125
instanceId String
126+
unreadMessages Int @default(0)
126127
}
127128

128129
model Contact {

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/api/services/channel.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ export class ChannelStartupService {
626626
"Chat"."updatedAt",
627627
"Contact"."pushName",
628628
"Contact"."profilePicUrl"
629+
"Contact"."unreadMessages"
629630
FROM "Chat"
630631
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
631632
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
@@ -639,7 +640,8 @@ export class ChannelStartupService {
639640
"Chat"."createdAt",
640641
"Chat"."updatedAt",
641642
"Contact"."pushName",
642-
"Contact"."profilePicUrl"
643+
"Contact"."profilePicUrl",
644+
"Contact"."unreadMessages"
643645
ORDER BY "Chat"."updatedAt" DESC;
644646
`;
645647
} else {
@@ -653,6 +655,7 @@ export class ChannelStartupService {
653655
"Chat"."updatedAt",
654656
"Contact"."pushName",
655657
"Contact"."profilePicUrl"
658+
"Contact"."unreadMessages"
656659
FROM "Chat"
657660
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
658661
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
@@ -666,6 +669,7 @@ export class ChannelStartupService {
666669
"Chat"."updatedAt",
667670
"Contact"."pushName",
668671
"Contact"."profilePicUrl"
672+
"Contact"."unreadMessages"
669673
ORDER BY "Chat"."updatedAt" DESC;
670674
`;
671675
}

0 commit comments

Comments
 (0)