Skip to content

Commit 3a2fe3e

Browse files
committed
feat: Added unreadMessages to chats
1 parent 27e344e commit 3a2fe3e

File tree

4 files changed

+48
-43
lines changed

4 files changed

+48
-43
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Chat" ADD COLUMN "unreadMessages" INTEGER NOT NULL DEFAULT 0;

prisma/postgresql-schema.prisma

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ model Session {
115115
}
116116

117117
model Chat {
118-
id String @id @default(cuid())
119-
remoteJid String @db.VarChar(100)
120-
name String? @db.VarChar(100)
121-
labels Json? @db.JsonB
122-
createdAt DateTime? @default(now()) @db.Timestamp
123-
updatedAt DateTime? @updatedAt @db.Timestamp
124-
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
125-
instanceId String
118+
id String @id @default(cuid())
119+
remoteJid String @db.VarChar(100)
120+
name String? @db.VarChar(100)
121+
labels Json? @db.JsonB
122+
createdAt DateTime? @default(now()) @db.Timestamp
123+
updatedAt DateTime? @updatedAt @db.Timestamp
124+
Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
125+
instanceId String
126+
unreadMessages Int @default(0)
126127
}
127128

128129
model Contact {

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,12 @@ export class BaileysStartupService extends ChannelStartupService {
564564

565565
const chatsToInsert = chats
566566
.filter((chat) => !existingChatIdSet?.has(chat.id))
567-
.map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name }));
567+
.map((chat) => ({
568+
remoteJid: chat.id,
569+
instanceId: this.instanceId,
570+
name: chat.name,
571+
unreadMessages: chat.unreadCount !== undefined ? chat.unreadCount : 0,
572+
}));
568573

569574
this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert);
570575

@@ -598,6 +603,7 @@ export class BaileysStartupService extends ChannelStartupService {
598603
instanceId: this.instanceId,
599604
remoteJid: chat.id,
600605
name: chat.name,
606+
unreadMessages: typeof chat.unreadCount === 'number' ? chat.unreadCount : 0,
601607
},
602608
data: { remoteJid: chat.id },
603609
});
@@ -969,6 +975,8 @@ export class BaileysStartupService extends ChannelStartupService {
969975
},
970976

971977
'messages.update': async (args: WAMessageUpdate[], settings: any) => {
978+
const unreadChatToUpdate: Record<string, number> = {};
979+
972980
for await (const { key, update } of args) {
973981
if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) {
974982
return;
@@ -1001,8 +1009,6 @@ export class BaileysStartupService extends ChannelStartupService {
10011009
return;
10021010
}
10031011

1004-
if (status[update.status] === 'READ' && !key.fromMe) return;
1005-
10061012
if (update.message === null && update.status === undefined) {
10071013
this.sendDataWebhook(Events.MESSAGES_DELETE, key);
10081014

@@ -1022,6 +1028,17 @@ export class BaileysStartupService extends ChannelStartupService {
10221028
});
10231029

10241030
return;
1031+
} else if (update.status !== undefined && status[update.status] !== findMessage.status) {
1032+
if (!unreadChatToUpdate[key.remoteJid!]) {
1033+
unreadChatToUpdate[key.remoteJid!] = 0;
1034+
}
1035+
1036+
unreadChatToUpdate[key.remoteJid!]++;
1037+
1038+
this.prismaRepository.message.update({
1039+
where: { id: findMessage.id },
1040+
data: { status: status[update.status] },
1041+
});
10251042
}
10261043

10271044
const message: any = {
@@ -1043,6 +1060,17 @@ export class BaileysStartupService extends ChannelStartupService {
10431060
});
10441061
}
10451062
}
1063+
1064+
for await (const [remoteJid, unreadMessages] of Object.entries(unreadChatToUpdate)) {
1065+
const chat = await this.prismaRepository.chat.findFirst({ where: { remoteJid } });
1066+
1067+
if (chat) {
1068+
this.prismaRepository.chat.update({
1069+
where: { id: chat.id },
1070+
data: { unreadMessages: Math.max(0, chat.unreadMessages - unreadMessages) },
1071+
});
1072+
}
1073+
}
10461074
},
10471075
};
10481076

src/api/services/channel.service.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,7 @@ export class ChannelStartupService {
442442
: this.createJid(query.where?.remoteJid)
443443
: null;
444444

445-
let result;
446-
if (remoteJid) {
447-
result = await this.prismaRepository.$queryRaw`
445+
const result = await this.prismaRepository.$queryRaw`
448446
SELECT
449447
"Chat"."id",
450448
"Chat"."remoteJid",
@@ -453,11 +451,13 @@ export class ChannelStartupService {
453451
"Chat"."createdAt",
454452
"Chat"."updatedAt",
455453
"Contact"."pushName",
456-
"Contact"."profilePicUrl"
454+
"Contact"."profilePicUrl",
455+
"Contact"."unreadMessages"
457456
FROM "Chat"
458457
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
459458
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
460459
WHERE "Chat"."instanceId" = ${this.instanceId}
460+
${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''}
461461
AND "Chat"."remoteJid" = ${remoteJid}
462462
GROUP BY
463463
"Chat"."id",
@@ -467,36 +467,10 @@ export class ChannelStartupService {
467467
"Chat"."createdAt",
468468
"Chat"."updatedAt",
469469
"Contact"."pushName",
470-
"Contact"."profilePicUrl"
470+
"Contact"."profilePicUrl",
471+
"Contact"."unreadMessages"
471472
ORDER BY "Chat"."updatedAt" DESC;
472473
`;
473-
} else {
474-
result = await this.prismaRepository.$queryRaw`
475-
SELECT
476-
"Chat"."id",
477-
"Chat"."remoteJid",
478-
"Chat"."name",
479-
"Chat"."labels",
480-
"Chat"."createdAt",
481-
"Chat"."updatedAt",
482-
"Contact"."pushName",
483-
"Contact"."profilePicUrl"
484-
FROM "Chat"
485-
INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid'
486-
LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid"
487-
WHERE "Chat"."instanceId" = ${this.instanceId}
488-
GROUP BY
489-
"Chat"."id",
490-
"Chat"."remoteJid",
491-
"Chat"."name",
492-
"Chat"."labels",
493-
"Chat"."createdAt",
494-
"Chat"."updatedAt",
495-
"Contact"."pushName",
496-
"Contact"."profilePicUrl"
497-
ORDER BY "Chat"."updatedAt" DESC;
498-
`;
499-
}
500474

501475
return result;
502476
}

0 commit comments

Comments
 (0)