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;
}
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;

View File

@ -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<ProfilePictureDto>({
request: req,
schema: profilePictureSchema,

View File

@ -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<Contact>) {
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 } } : {},
],
},

View File

@ -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) {