From 47d6fd5d31269dccb2d34570b97a83da20078f8a Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Wed, 26 Jun 2024 08:08:12 -0300 Subject: [PATCH] chore: Update privacy settings, refactor code and improve performance In this commit, several changes were made to improve the codebase and update privacy settings. Specifically, the following changes were made: - The `PrivacySetting` class was refactored to `PrivacySettingDto` in `chat.dto.ts`. - The `put` method was changed to `post` in `chat.router.ts` for updating the profile picture. - Several methods in `channel.service.ts` were refactored to improve performance and readability. Specifically, the `setWebhook`, `setRabbitmq`, `setSqs`, `fetchContacts`, and `fetchMessages` methods were improved. - The `updatePrivacySettings` method in `whatsapp.baileys.service.ts` was refactored to reduce complexity and improve readability. These changes were made to improve the overall performance and maintainability of the codebase. Additionally, the privacy settings for the WhatsApp client were updated to provide better control over user data. --- src/api/dto/chat.dto.ts | 6 +- src/api/routes/chat.router.ts | 2 +- src/api/services/channel.service.ts | 86 ++++++++++++++++++- .../channels/whatsapp.baileys.service.ts | 24 +++--- 4 files changed, 97 insertions(+), 21 deletions(-) diff --git a/src/api/dto/chat.dto.ts b/src/api/dto/chat.dto.ts index b2f47a627..fc2ff5d37 100644 --- a/src/api/dto/chat.dto.ts +++ b/src/api/dto/chat.dto.ts @@ -78,7 +78,7 @@ export class MarkChatUnreadDto { chat?: string; } -class PrivacySetting { +export class PrivacySettingDto { readreceipts: WAReadReceiptsValue; profile: WAPrivacyValue; status: WAPrivacyValue; @@ -87,10 +87,6 @@ class PrivacySetting { groupadd: WAPrivacyValue; } -export class PrivacySettingDto { - privacySettings: PrivacySetting; -} - export class DeleteMessage { id: string; fromMe: boolean; diff --git a/src/api/routes/chat.router.ts b/src/api/routes/chat.router.ts index f5d49af64..5985685bf 100644 --- a/src/api/routes/chat.router.ts +++ b/src/api/routes/chat.router.ts @@ -228,7 +228,7 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) - .put(this.routerPath('updateProfilePicture'), ...guards, async (req, res) => { + .post(this.routerPath('updateProfilePicture'), ...guards, async (req, res) => { const response = await this.dataValidate({ request: req, schema: profilePictureSchema, diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 07e157864..ca995cf21 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -229,6 +229,29 @@ export class ChannelStartupService { } public async setWebhook(data: WebhookDto) { + const findWebhook = await this.prismaRepository.webhook.findUnique({ + where: { + instanceId: this.instanceId, + }, + }); + + if (findWebhook) { + await this.prismaRepository.webhook.update({ + where: { + instanceId: this.instanceId, + }, + data: { + url: data.url, + enabled: data.enabled, + events: data.events, + webhookByEvents: data.webhookByEvents, + webhookBase64: data.webhookBase64, + }, + }); + + Object.assign(this.localWebhook, data); + return; + } await this.prismaRepository.webhook.create({ data: { url: data.url, @@ -241,6 +264,7 @@ export class ChannelStartupService { }); Object.assign(this.localWebhook, data); + return; } public async findWebhook() { @@ -437,6 +461,27 @@ export class ChannelStartupService { } public async setRabbitmq(data: RabbitmqDto) { + const findRabbitmq = await this.prismaRepository.rabbitmq.findUnique({ + where: { + instanceId: this.instanceId, + }, + }); + + if (findRabbitmq) { + await this.prismaRepository.rabbitmq.update({ + where: { + instanceId: this.instanceId, + }, + data: { + enabled: data.enabled, + events: data.events, + }, + }); + + Object.assign(this.localRabbitmq, data); + return; + } + await this.prismaRepository.rabbitmq.create({ data: { enabled: data.enabled, @@ -446,6 +491,7 @@ export class ChannelStartupService { }); Object.assign(this.localRabbitmq, data); + return; } public async findRabbitmq() { @@ -480,6 +526,27 @@ export class ChannelStartupService { } public async setSqs(data: SqsDto) { + const findSqs = await this.prismaRepository.sqs.findUnique({ + where: { + instanceId: this.instanceId, + }, + }); + + if (findSqs) { + await this.prismaRepository.sqs.update({ + where: { + instanceId: this.instanceId, + }, + data: { + enabled: data.enabled, + events: data.events, + }, + }); + + Object.assign(this.localSqs, data); + return; + } + await this.prismaRepository.sqs.create({ data: { enabled: data.enabled, @@ -489,6 +556,7 @@ export class ChannelStartupService { }); Object.assign(this.localSqs, data); + return; } public async findSqs() { @@ -1059,10 +1127,14 @@ export class ChannelStartupService { } public async fetchContacts(query: Query) { + const remoteJid = query.where?.remoteJid.includes('@') + ? query.where?.remoteJid + : this.createJid(query.where?.remoteJid); + return await this.prismaRepository.contact.findMany({ where: { instanceId: this.instanceId, - remoteJid: query.where?.remoteJid, + remoteJid, }, }); } @@ -1075,6 +1147,14 @@ export class ChannelStartupService { participants?: string; }; + const remoteJid = keyFilters?.remoteJid + ? keyFilters?.remoteJid.includes('@') + ? keyFilters?.remoteJid + : this.createJid(keyFilters?.remoteJid) + : null; + + console.log(remoteJid); + const count = await this.prismaRepository.message.count({ where: { instanceId: this.instanceId, @@ -1084,7 +1164,7 @@ export class ChannelStartupService { AND: [ keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {}, keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {}, - keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {}, + remoteJid ? { key: { path: ['remoteJid'], equals: remoteJid } } : {}, keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {}, ], }, @@ -1107,7 +1187,7 @@ export class ChannelStartupService { AND: [ keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {}, keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {}, - keyFilters?.remoteJid ? { key: { path: ['remoteJid'], equals: keyFilters?.remoteJid } } : {}, + remoteJid ? { key: { path: ['remoteJid'], equals: remoteJid } } : {}, keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {}, ], }, diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 673e24034..3041a6bd7 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -2928,24 +2928,24 @@ export class BaileysStartupService extends ChannelStartupService { public async updatePrivacySettings(settings: PrivacySettingDto) { try { - await this.client.updateReadReceiptsPrivacy(settings.privacySettings.readreceipts); - await this.client.updateProfilePicturePrivacy(settings.privacySettings.profile); - await this.client.updateStatusPrivacy(settings.privacySettings.status); - await this.client.updateOnlinePrivacy(settings.privacySettings.online); - await this.client.updateLastSeenPrivacy(settings.privacySettings.last); - await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd); + await this.client.updateReadReceiptsPrivacy(settings.readreceipts); + await this.client.updateProfilePicturePrivacy(settings.profile); + await this.client.updateStatusPrivacy(settings.status); + await this.client.updateOnlinePrivacy(settings.online); + await this.client.updateLastSeenPrivacy(settings.last); + await this.client.updateGroupsAddPrivacy(settings.groupadd); this.reloadConnection(); return { update: 'success', data: { - readreceipts: settings.privacySettings.readreceipts, - profile: settings.privacySettings.profile, - status: settings.privacySettings.status, - online: settings.privacySettings.online, - last: settings.privacySettings.last, - groupadd: settings.privacySettings.groupadd, + readreceipts: settings.readreceipts, + profile: settings.profile, + status: settings.status, + online: settings.online, + last: settings.last, + groupadd: settings.groupadd, }, }; } catch (error) {