diff --git a/CHANGELOG.md b/CHANGELOG.md index 52879eee..8d7a1be0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Added conversion of audios for sending recorded audio, now it is possible to send mp3 audios and not just ogg * Route to fetch all groups that the connection is part of * Route to fetch all privacy settings +* Route to update the privacy settings ### Fixed diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 70ee4287..c62b1f7f 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -408,6 +408,36 @@ export const readMessageSchema: JSONSchema7 = { required: ['readMessages'], }; +export const privacySettingsSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + privacySettings: { + type: 'object', + properties: { + readreceipts: { type: 'string', enum: ['all', 'none'] }, + profile: { + type: 'string', + enum: ['all', 'contacts', 'contact_blacklist', 'none'], + }, + status: { + type: 'string', + enum: ['all', 'contacts', 'contact_blacklist', 'none'], + }, + online: { type: 'string', enum: ['all', 'match_last_seen'] }, + last: { type: 'string', enum: ['all', 'contacts', 'contact_blacklist', 'none'] }, + groupadd: { + type: 'string', + enum: ['all', 'contacts', 'contact_blacklist', 'none'], + }, + }, + required: ['readreceipts', 'profile', 'status', 'online', 'last', 'groupadd'], + ...isNotEmpty('readreceipts', 'profile', 'status', 'online', 'last', 'groupadd'), + }, + }, + required: ['privacySettings'], +}; + export const archiveChatSchema: JSONSchema7 = { $id: v4(), type: 'object', diff --git a/src/whatsapp/controllers/chat.controller.ts b/src/whatsapp/controllers/chat.controller.ts index 78629565..a4d8d0d1 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -3,6 +3,7 @@ import { ArchiveChatDto, DeleteMessage, NumberDto, + PrivacySettingDto, ProfileNameDto, ProfilePictureDto, ProfileStatusDto, @@ -67,6 +68,13 @@ export class ChatController { return await this.waMonitor.waInstances[instanceName].fetchPrivacySettings(); } + public async updatePrivacySettings( + { instanceName }: InstanceDto, + data: PrivacySettingDto, + ) { + return await this.waMonitor.waInstances[instanceName].updatePrivacySettings(data); + } + public async getBusinessProfile( { instanceName }: InstanceDto, data: ProfilePictureDto, diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index ebc372b6..2f65216a 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -1,3 +1,5 @@ +import { WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from "@evolution/base"; + export class OnWhatsAppDto { constructor( public readonly jid: string, @@ -47,6 +49,19 @@ export class ArchiveChatDto { archive: boolean; } +class PrivacySetting { + readreceipts: WAReadReceiptsValue; + profile: WAPrivacyValue; + status: WAPrivacyValue; + online: WAPrivacyOnlineValue; + last: WAPrivacyValue; + groupadd: WAPrivacyValue; +} + +export class PrivacySettingDto { + privacySettings: PrivacySetting; +} + export class DeleteMessage { id: string; fromMe: boolean; diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index b07ebc0d..be74a41d 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -5,6 +5,7 @@ import { deleteMessageSchema, messageUpSchema, messageValidateSchema, + privacySettingsSchema, profileNameSchema, profilePictureSchema, profileStatusSchema, @@ -15,6 +16,7 @@ import { ArchiveChatDto, DeleteMessage, NumberDto, + PrivacySettingDto, ProfileNameDto, ProfilePictureDto, ProfileStatusDto, @@ -150,6 +152,17 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) + .put(this.routerPath('updatePrivacySettings'), ...guards, async (req, res) => { + const response = await this.dataValidate({ + request: req, + schema: privacySettingsSchema, + ClassRef: PrivacySettingDto, + execute: (instance, data) => + chatController.updatePrivacySettings(instance, data), + }); + + return res.status(HttpStatus.CREATED).json(response); + }) .post(this.routerPath('getBusinessProfile'), ...guards, async (req, res) => { const response = await this.dataValidate({ request: req, diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 5cdc36f0..bf5857bd 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -29,6 +29,9 @@ import makeWASocket, { WAMessage, WAMessageUpdate, WASocket, + WAReadReceiptsValue, + WAPrivacyValue, + WAPrivacyOnlineValue, } from '@evolution/base'; import { Auth, @@ -81,6 +84,7 @@ import { ArchiveChatDto, DeleteMessage, OnWhatsAppDto, + PrivacySettingDto, ReadMessageDto, WhatsAppNumberDto, } from '../dto/chat.dto'; @@ -1479,6 +1483,23 @@ export class WAStartupService { return await this.client.fetchPrivacySettings(); } + 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); + return { update: 'success', data: await this.client.fetchPrivacySettings() }; + } catch (error) { + throw new InternalServerErrorException( + 'Error updating privacy settings', + error.toString(), + ); + } + } + public async getBusinessProfile(number: string) { try { let jid;