Skip to content

Commit f2bb01a

Browse files
committed
Merge branch 'v2.0.0' of github.com:EvolutionAPI/evolution-api into v2.0.0
2 parents ac3b186 + 2f1ba83 commit f2bb01a

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
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: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/api/services/channel.service.ts

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,7 @@ export class ChannelStartupService {
614614
: this.createJid(query.where?.remoteJid)
615615
: null;
616616

617-
let result;
618-
if (remoteJid) {
619-
result = await this.prismaRepository.$queryRaw`
617+
const result = await this.prismaRepository.$queryRaw`
620618
SELECT
621619
"Chat"."id",
622620
"Chat"."remoteJid",
@@ -625,12 +623,13 @@ export class ChannelStartupService {
625623
"Chat"."createdAt",
626624
"Chat"."updatedAt",
627625
"Contact"."pushName",
628-
"Contact"."profilePicUrl"
626+
"Contact"."profilePicUrl",
627+
"Contact"."unreadMessages"
629628
FROM "Chat"
630629
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
631630
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
632631
WHERE "Chat"."instanceId" = ${this.instanceId}
633-
AND "Chat"."remoteJid" = ${remoteJid}
632+
${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
634633
GROUP BY
635634
"Chat"."id",
636635
"Chat"."remoteJid",
@@ -639,36 +638,10 @@ export class ChannelStartupService {
639638
"Chat"."createdAt",
640639
"Chat"."updatedAt",
641640
"Contact"."pushName",
642-
"Contact"."profilePicUrl"
641+
"Contact"."profilePicUrl",
642+
"Contact"."unreadMessages"
643643
ORDER BY "Chat"."updatedAt" DESC;
644644
`;
645-
} else {
646-
result = await this.prismaRepository.$queryRaw`
647-
SELECT
648-
"Chat"."id",
649-
"Chat"."remoteJid",
650-
"Chat"."name",
651-
"Chat"."labels",
652-
"Chat"."createdAt",
653-
"Chat"."updatedAt",
654-
"Contact"."pushName",
655-
"Contact"."profilePicUrl"
656-
FROM "Chat"
657-
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
658-
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
659-
WHERE "Chat"."instanceId" = ${this.instanceId}
660-
GROUP BY
661-
"Chat"."id",
662-
"Chat"."remoteJid",
663-
"Chat"."name",
664-
"Chat"."labels",
665-
"Chat"."createdAt",
666-
"Chat"."updatedAt",
667-
"Contact"."pushName",
668-
"Contact"."profilePicUrl"
669-
ORDER BY "Chat"."updatedAt" DESC;
670-
`;
671-
}
672645

673646
return result;
674647
}

0 commit comments

Comments
 (0)