diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 01592b1e9..faaf2fa10 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -539,6 +539,17 @@ export const privacySettingsSchema: JSONSchema7 = { required: ['privacySettings'], }; +export const blockUserSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + number: { type: 'string' }, + status: { type: 'string', enum: ['block', 'unblock'] }, + }, + required: ['number', 'status'], + ...isNotEmpty('number', 'status'), +}; + 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 f9d77fcef..1c16260d9 100644 --- a/src/whatsapp/controllers/chat.controller.ts +++ b/src/whatsapp/controllers/chat.controller.ts @@ -1,6 +1,7 @@ import { Logger } from '../../config/logger.config'; import { ArchiveChatDto, + BlockUserDto, DeleteMessage, getBase64FromMediaMessageDto, NumberDto, @@ -123,4 +124,9 @@ export class ChatController { logger.verbose('requested updateMessage from ' + instanceName + ' instance'); return await this.waMonitor.waInstances[instanceName].updateMessage(data); } + + public async blockUser({ instanceName }: InstanceDto, data: BlockUserDto) { + logger.verbose('requested blockUser from ' + instanceName + ' instance'); + return await this.waMonitor.waInstances[instanceName].blockUser(data); + } } diff --git a/src/whatsapp/dto/chat.dto.ts b/src/whatsapp/dto/chat.dto.ts index 31f3dfe33..24f048473 100644 --- a/src/whatsapp/dto/chat.dto.ts +++ b/src/whatsapp/dto/chat.dto.ts @@ -115,3 +115,8 @@ export class UpdateMessageDto extends Metadata { key: proto.IMessageKey; text: string; } + +export class BlockUserDto { + number: string; + status: 'block' | 'unblock'; +} diff --git a/src/whatsapp/routers/chat.router.ts b/src/whatsapp/routers/chat.router.ts index e41614746..a0ce32166 100644 --- a/src/whatsapp/routers/chat.router.ts +++ b/src/whatsapp/routers/chat.router.ts @@ -3,6 +3,7 @@ import { RequestHandler, Router } from 'express'; import { Logger } from '../../config/logger.config'; import { archiveChatSchema, + blockUserSchema, contactValidateSchema, deleteMessageSchema, messageUpSchema, @@ -20,6 +21,7 @@ import { import { RouterBroker } from '../abstract/abstract.router'; import { ArchiveChatDto, + BlockUserDto, DeleteMessage, getBase64FromMediaMessageDto, NumberDto, @@ -384,6 +386,23 @@ export class ChatRouter extends RouterBroker { }); return res.status(HttpStatus.OK).json(response); + }) + .put(this.routerPath('updateBlockStatus'), ...guards, async (req, res) => { + logger.verbose('request received in updateBlockStatus'); + logger.verbose('request body: '); + logger.verbose(req.body); + + logger.verbose('request query: '); + logger.verbose(req.query); + + const response = await this.dataValidate({ + request: req, + schema: blockUserSchema, + ClassRef: BlockUserDto, + execute: (instance, data) => chatController.blockUser(instance, data), + }); + + return res.status(HttpStatus.CREATED).json(response); }); } diff --git a/src/whatsapp/services/whatsapp.baileys.service.ts b/src/whatsapp/services/whatsapp.baileys.service.ts index 0fc4f541c..915416049 100644 --- a/src/whatsapp/services/whatsapp.baileys.service.ts +++ b/src/whatsapp/services/whatsapp.baileys.service.ts @@ -63,6 +63,7 @@ import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-d import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db'; import { ArchiveChatDto, + BlockUserDto, DeleteMessage, getBase64FromMediaMessageDto, LastMessage, @@ -2796,6 +2797,29 @@ export class BaileysStartupService extends WAStartupService { } } + public async blockUser(data: BlockUserDto) { + this.logger.verbose('Blocking user: ' + data.number); + try { + const { number } = data; + + this.logger.verbose(`Check if number "${number}" is WhatsApp`); + const isWA = (await this.whatsappNumber({ numbers: [number] }))?.shift(); + + this.logger.verbose(`Exists: "${isWA.exists}" | jid: ${isWA.jid}`); + if (!isWA.exists && !isJidGroup(isWA.jid) && !isWA.jid.includes('@broadcast')) { + throw new BadRequestException(isWA); + } + + const sender = isWA.jid; + + await this.client.updateBlockStatus(sender, data.status); + + return { block: 'success' }; + } catch (error) { + throw new InternalServerErrorException('Error blocking user', error.toString()); + } + } + public async updateMessage(data: UpdateMessageDto) { try { const jid = this.createJid(data.number); diff --git a/src/whatsapp/services/whatsapp.business.service.ts b/src/whatsapp/services/whatsapp.business.service.ts index 2b37f3628..73b1c2998 100644 --- a/src/whatsapp/services/whatsapp.business.service.ts +++ b/src/whatsapp/services/whatsapp.business.service.ts @@ -1159,6 +1159,9 @@ export class BusinessStartupService extends WAStartupService { public async removeProfilePicture() { throw new BadRequestException('Method not available on WhatsApp Business API'); } + public async blockUser() { + throw new BadRequestException('Method not available on WhatsApp Business API'); + } public async updateMessage() { throw new BadRequestException('Method not available on WhatsApp Business API'); }