feat: Route to update the privacy settings

This commit is contained in:
Davidson Gomes 2023-06-11 11:38:26 -03:00
parent 55e36f24a5
commit ab28b4c0c5
6 changed files with 88 additions and 0 deletions

View File

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

View File

@ -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',

View File

@ -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,

View File

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

View File

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

View File

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