Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blockUser functionality #433

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions src/whatsapp/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Logger } from '../../config/logger.config';
import {
ArchiveChatDto,
BlockUserDto,
DeleteMessage,
getBase64FromMediaMessageDto,
NumberDto,
Expand Down Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions src/whatsapp/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ export class UpdateMessageDto extends Metadata {
key: proto.IMessageKey;
text: string;
}

export class BlockUserDto {
number: string;
status: 'block' | 'unblock';
}
19 changes: 19 additions & 0 deletions src/whatsapp/routers/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RequestHandler, Router } from 'express';
import { Logger } from '../../config/logger.config';
import {
archiveChatSchema,
blockUserSchema,
contactValidateSchema,
deleteMessageSchema,
messageUpSchema,
Expand All @@ -20,6 +21,7 @@ import {
import { RouterBroker } from '../abstract/abstract.router';
import {
ArchiveChatDto,
BlockUserDto,
DeleteMessage,
getBase64FromMediaMessageDto,
NumberDto,
Expand Down Expand Up @@ -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<BlockUserDto>({
request: req,
schema: blockUserSchema,
ClassRef: BlockUserDto,
execute: (instance, data) => chatController.blockUser(instance, data),
});

return res.status(HttpStatus.CREATED).json(response);
});
}

Expand Down
24 changes: 24 additions & 0 deletions src/whatsapp/services/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/services/whatsapp.business.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down