Skip to content

Commit

Permalink
chore: Update privacy settings, refactor code and improve performance
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgcode-tec committed Jun 26, 2024
1 parent 4120318 commit 47d6fd5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/api/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class MarkChatUnreadDto {
chat?: string;
}

class PrivacySetting {
export class PrivacySettingDto {
readreceipts: WAReadReceiptsValue;
profile: WAPrivacyValue;
status: WAPrivacyValue;
Expand All @@ -87,10 +87,6 @@ class PrivacySetting {
groupadd: WAPrivacyValue;
}

export class PrivacySettingDto {
privacySettings: PrivacySetting;
}

export class DeleteMessage {
id: string;
fromMe: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
86 changes: 83 additions & 3 deletions src/api/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -241,6 +264,7 @@ export class ChannelStartupService {
});

Object.assign(this.localWebhook, data);
return;
}

public async findWebhook() {
Expand Down Expand Up @@ -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,
Expand All @@ -446,6 +491,7 @@ export class ChannelStartupService {
});

Object.assign(this.localRabbitmq, data);
return;
}

public async findRabbitmq() {
Expand Down Expand Up @@ -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,
Expand All @@ -489,6 +556,7 @@ export class ChannelStartupService {
});

Object.assign(this.localSqs, data);
return;
}

public async findSqs() {
Expand Down Expand Up @@ -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,
},
});
}
Expand All @@ -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,
Expand All @@ -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 } } : {},
],
},
Expand All @@ -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 } } : {},
],
},
Expand Down
24 changes: 12 additions & 12 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 47d6fd5

Please sign in to comment.