From edb3875b9149984b9e6dac021d9ec1e019c13b3d Mon Sep 17 00:00:00 2001 From: Jesus Date: Mon, 7 Oct 2024 17:39:00 +0200 Subject: [PATCH 1/4] Added unreadMessages to chats --- prisma/postgresql-schema.prisma | 1 + .../whatsapp/whatsapp.baileys.service.ts | 35 +++++++++++++++++-- src/api/services/channel.service.ts | 6 +++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/prisma/postgresql-schema.prisma b/prisma/postgresql-schema.prisma index 857c4426..e904ff2e 100644 --- a/prisma/postgresql-schema.prisma +++ b/prisma/postgresql-schema.prisma @@ -123,6 +123,7 @@ model Chat { updatedAt DateTime? @updatedAt @db.Timestamp Instance Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade) instanceId String + unreadMessages Int @default(0) } model Contact { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 03a4cb67..cfe828fd 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -661,6 +661,7 @@ export class BaileysStartupService extends ChannelStartupService { instanceId: this.instanceId, remoteJid: chat.id, name: chat.name, + unreadMessages: typeof chat.unreadCount === 'number' ? chat.unreadCount : 0, }, data: { remoteJid: chat.id }, }); @@ -1231,6 +1232,8 @@ export class BaileysStartupService extends ChannelStartupService { }, 'messages.update': async (args: WAMessageUpdate[], settings: any) => { + const unreadChatToUpdate: Record = {}; // {remoteJid: readedMessages} + for await (const { key, update } of args) { if (settings?.groupsIgnore && key.remoteJid?.includes('@g.us')) { return; @@ -1273,7 +1276,7 @@ export class BaileysStartupService extends ChannelStartupService { return; } - if (status[update.status] === 'READ' && !key.fromMe) return; + //if (status[update.status] === 'READ' && !key.fromMe) return; if (update.message === null && update.status === undefined) { this.sendDataWebhook(Events.MESSAGES_DELETE, key); @@ -1302,6 +1305,17 @@ export class BaileysStartupService extends ChannelStartupService { } return; + } else if (update.message !== undefined && update.message !== findMessage.status) { + if (unreadChatToUpdate[key.remoteJid!] === undefined) { + unreadChatToUpdate[key.remoteJid!] = 0; + } + + unreadChatToUpdate[key.remoteJid!]++; + + this.prismaRepository.message.update({ + where: { id: findMessage.id }, + data: { status: status[update.status] }, + }); } const message: any = { @@ -1323,6 +1337,17 @@ export class BaileysStartupService extends ChannelStartupService { }); } } + + for await (const [remoteJid, unreadMessages] of Object.entries(unreadChatToUpdate)) { + const chat = await this.prismaRepository.chat.findFirst({ where: { remoteJid } }); + + if (chat) { + this.prismaRepository.chat.update({ + where: { id: chat.id }, + data: { unreadMessages: Math.max(0, chat.unreadMessages - unreadMessages) }, + }); + } + } }, }; @@ -2006,7 +2031,13 @@ export class BaileysStartupService extends ChannelStartupService { const mimetype = mime.getType(fileName).toString(); - const fullName = join(`${this.instance.id}`, messageRaw.key.remoteJid, `${messageRaw.key.id}`, mediaType, fileName); + const fullName = join( + `${this.instance.id}`, + messageRaw.key.remoteJid, + `${messageRaw.key.id}`, + mediaType, + fileName, + ); await s3Service.uploadFile(fullName, buffer, size.fileLength?.low, { 'Content-Type': mimetype, diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 2875e696..d4aa6a19 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -626,6 +626,7 @@ export class ChannelStartupService { "Chat"."updatedAt", "Contact"."pushName", "Contact"."profilePicUrl" + "Contact"."unreadMessages" FROM "Chat" INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" @@ -639,7 +640,8 @@ export class ChannelStartupService { "Chat"."createdAt", "Chat"."updatedAt", "Contact"."pushName", - "Contact"."profilePicUrl" + "Contact"."profilePicUrl", + "Contact"."unreadMessages" ORDER BY "Chat"."updatedAt" DESC; `; } else { @@ -653,6 +655,7 @@ export class ChannelStartupService { "Chat"."updatedAt", "Contact"."pushName", "Contact"."profilePicUrl" + "Contact"."unreadMessages" FROM "Chat" INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" @@ -666,6 +669,7 @@ export class ChannelStartupService { "Chat"."updatedAt", "Contact"."pushName", "Contact"."profilePicUrl" + "Contact"."unreadMessages" ORDER BY "Chat"."updatedAt" DESC; `; } From 27dc49f2c36d1c33b159c429a8210435eeaa2b3b Mon Sep 17 00:00:00 2001 From: Jesus Date: Mon, 7 Oct 2024 17:41:43 +0200 Subject: [PATCH 2/4] Fix unread for chat creation --- .../channel/whatsapp/whatsapp.baileys.service.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index cfe828fd..19aef748 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -627,7 +627,12 @@ export class BaileysStartupService extends ChannelStartupService { const chatsToInsert = chats .filter((chat) => !existingChatIdSet?.has(chat.id)) - .map((chat) => ({ remoteJid: chat.id, instanceId: this.instanceId, name: chat.name })); + .map((chat) => ({ + remoteJid: chat.id, + instanceId: this.instanceId, + name: chat.name, + unreadMessages: chat.unreadCount !== undefined ? chat.unreadCount : 0, + })); this.sendDataWebhook(Events.CHATS_UPSERT, chatsToInsert); From 96e18021481fefc393f748d130fa744683893009 Mon Sep 17 00:00:00 2001 From: Jesus Date: Mon, 7 Oct 2024 17:54:25 +0200 Subject: [PATCH 3/4] Fix update message condition --- .../channel/whatsapp/whatsapp.baileys.service.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 19aef748..d10fb748 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1281,8 +1281,6 @@ export class BaileysStartupService extends ChannelStartupService { return; } - //if (status[update.status] === 'READ' && !key.fromMe) return; - if (update.message === null && update.status === undefined) { this.sendDataWebhook(Events.MESSAGES_DELETE, key); @@ -1310,8 +1308,8 @@ export class BaileysStartupService extends ChannelStartupService { } return; - } else if (update.message !== undefined && update.message !== findMessage.status) { - if (unreadChatToUpdate[key.remoteJid!] === undefined) { + } else if (update.status !== undefined && status[update.status] !== findMessage.status) { + if (!unreadChatToUpdate[key.remoteJid!]) { unreadChatToUpdate[key.remoteJid!] = 0; } From 10aeeebb530cc1eacc8d915b13f49cf7f91d4f27 Mon Sep 17 00:00:00 2001 From: Jesus Date: Mon, 7 Oct 2024 18:03:27 +0200 Subject: [PATCH 4/4] Fix and improve fetch chats --- src/api/services/channel.service.ts | 37 +++-------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index d4aa6a19..5d079b69 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -614,9 +614,7 @@ export class ChannelStartupService { : this.createJid(query.where?.remoteJid) : null; - let result; - if (remoteJid) { - result = await this.prismaRepository.$queryRaw` + const result = await this.prismaRepository.$queryRaw` SELECT "Chat"."id", "Chat"."remoteJid", @@ -625,13 +623,13 @@ export class ChannelStartupService { "Chat"."createdAt", "Chat"."updatedAt", "Contact"."pushName", - "Contact"."profilePicUrl" + "Contact"."profilePicUrl", "Contact"."unreadMessages" FROM "Chat" INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" WHERE "Chat"."instanceId" = ${this.instanceId} - AND "Chat"."remoteJid" = ${remoteJid} + ${remoteJid ? 'AND "Chat"."remoteJid" = ${remoteJid}' : ''} GROUP BY "Chat"."id", "Chat"."remoteJid", @@ -644,35 +642,6 @@ export class ChannelStartupService { "Contact"."unreadMessages" ORDER BY "Chat"."updatedAt" DESC; `; - } else { - result = await this.prismaRepository.$queryRaw` - SELECT - "Chat"."id", - "Chat"."remoteJid", - "Chat"."name", - "Chat"."labels", - "Chat"."createdAt", - "Chat"."updatedAt", - "Contact"."pushName", - "Contact"."profilePicUrl" - "Contact"."unreadMessages" - FROM "Chat" - INNER JOIN "Message" ON "Chat"."remoteJid" = "Message"."key"->>'remoteJid' - LEFT JOIN "Contact" ON "Chat"."remoteJid" = "Contact"."remoteJid" - WHERE "Chat"."instanceId" = ${this.instanceId} - GROUP BY - "Chat"."id", - "Chat"."remoteJid", - "Chat"."name", - "Chat"."labels", - "Chat"."createdAt", - "Chat"."updatedAt", - "Contact"."pushName", - "Contact"."profilePicUrl" - "Contact"."unreadMessages" - ORDER BY "Chat"."updatedAt" DESC; - `; - } return result; }