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.
This commit is contained in:
Davidson Gomes 2024-06-26 08:08:12 -03:00
parent 4120318ec4
commit 47d6fd5d31
4 changed files with 97 additions and 21 deletions

View File

@ -78,7 +78,7 @@ export class MarkChatUnreadDto {
chat?: string; chat?: string;
} }
class PrivacySetting { export class PrivacySettingDto {
readreceipts: WAReadReceiptsValue; readreceipts: WAReadReceiptsValue;
profile: WAPrivacyValue; profile: WAPrivacyValue;
status: WAPrivacyValue; status: WAPrivacyValue;
@ -87,10 +87,6 @@ class PrivacySetting {
groupadd: WAPrivacyValue; groupadd: WAPrivacyValue;
} }
export class PrivacySettingDto {
privacySettings: PrivacySetting;
}
export class DeleteMessage { export class DeleteMessage {
id: string; id: string;
fromMe: boolean; fromMe: boolean;

View File

@ -228,7 +228,7 @@ export class ChatRouter extends RouterBroker {
return res.status(HttpStatus.OK).json(response); 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<ProfilePictureDto>({ const response = await this.dataValidate<ProfilePictureDto>({
request: req, request: req,
schema: profilePictureSchema, schema: profilePictureSchema,

View File

@ -229,6 +229,29 @@ export class ChannelStartupService {
} }
public async setWebhook(data: WebhookDto) { 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({ await this.prismaRepository.webhook.create({
data: { data: {
url: data.url, url: data.url,
@ -241,6 +264,7 @@ export class ChannelStartupService {
}); });
Object.assign(this.localWebhook, data); Object.assign(this.localWebhook, data);
return;
} }
public async findWebhook() { public async findWebhook() {
@ -437,6 +461,27 @@ export class ChannelStartupService {
} }
public async setRabbitmq(data: RabbitmqDto) { 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({ await this.prismaRepository.rabbitmq.create({
data: { data: {
enabled: data.enabled, enabled: data.enabled,
@ -446,6 +491,7 @@ export class ChannelStartupService {
}); });
Object.assign(this.localRabbitmq, data); Object.assign(this.localRabbitmq, data);
return;
} }
public async findRabbitmq() { public async findRabbitmq() {
@ -480,6 +526,27 @@ export class ChannelStartupService {
} }
public async setSqs(data: SqsDto) { 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({ await this.prismaRepository.sqs.create({
data: { data: {
enabled: data.enabled, enabled: data.enabled,
@ -489,6 +556,7 @@ export class ChannelStartupService {
}); });
Object.assign(this.localSqs, data); Object.assign(this.localSqs, data);
return;
} }
public async findSqs() { public async findSqs() {
@ -1059,10 +1127,14 @@ export class ChannelStartupService {
} }
public async fetchContacts(query: Query<Contact>) { public async fetchContacts(query: Query<Contact>) {
const remoteJid = query.where?.remoteJid.includes('@')
? query.where?.remoteJid
: this.createJid(query.where?.remoteJid);
return await this.prismaRepository.contact.findMany({ return await this.prismaRepository.contact.findMany({
where: { where: {
instanceId: this.instanceId, instanceId: this.instanceId,
remoteJid: query.where?.remoteJid, remoteJid,
}, },
}); });
} }
@ -1075,6 +1147,14 @@ export class ChannelStartupService {
participants?: string; 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({ const count = await this.prismaRepository.message.count({
where: { where: {
instanceId: this.instanceId, instanceId: this.instanceId,
@ -1084,7 +1164,7 @@ export class ChannelStartupService {
AND: [ AND: [
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {}, keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {}, 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 } } : {}, keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
], ],
}, },
@ -1107,7 +1187,7 @@ export class ChannelStartupService {
AND: [ AND: [
keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {}, keyFilters?.id ? { key: { path: ['id'], equals: keyFilters?.id } } : {},
keyFilters?.fromMe ? { key: { path: ['fromMe'], equals: keyFilters?.fromMe } } : {}, 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 } } : {}, keyFilters?.participants ? { key: { path: ['participants'], equals: keyFilters?.participants } } : {},
], ],
}, },

View File

@ -2928,24 +2928,24 @@ export class BaileysStartupService extends ChannelStartupService {
public async updatePrivacySettings(settings: PrivacySettingDto) { public async updatePrivacySettings(settings: PrivacySettingDto) {
try { try {
await this.client.updateReadReceiptsPrivacy(settings.privacySettings.readreceipts); await this.client.updateReadReceiptsPrivacy(settings.readreceipts);
await this.client.updateProfilePicturePrivacy(settings.privacySettings.profile); await this.client.updateProfilePicturePrivacy(settings.profile);
await this.client.updateStatusPrivacy(settings.privacySettings.status); await this.client.updateStatusPrivacy(settings.status);
await this.client.updateOnlinePrivacy(settings.privacySettings.online); await this.client.updateOnlinePrivacy(settings.online);
await this.client.updateLastSeenPrivacy(settings.privacySettings.last); await this.client.updateLastSeenPrivacy(settings.last);
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd); await this.client.updateGroupsAddPrivacy(settings.groupadd);
this.reloadConnection(); this.reloadConnection();
return { return {
update: 'success', update: 'success',
data: { data: {
readreceipts: settings.privacySettings.readreceipts, readreceipts: settings.readreceipts,
profile: settings.privacySettings.profile, profile: settings.profile,
status: settings.privacySettings.status, status: settings.status,
online: settings.privacySettings.online, online: settings.online,
last: settings.privacySettings.last, last: settings.last,
groupadd: settings.privacySettings.groupadd, groupadd: settings.groupadd,
}, },
}; };
} catch (error) { } catch (error) {