From e19e37eef48aa78bb14f1369f9a1b3007771dacb Mon Sep 17 00:00:00 2001 From: Leandro Rocha Date: Sun, 21 Jan 2024 02:13:24 -0300 Subject: [PATCH] Join in Group by Invite Code --- package.json | 2 +- src/validate/validate.schema.ts | 10 ++++++++++ src/whatsapp/controllers/group.controller.ts | 6 ++++++ src/whatsapp/dto/group.dto.ts | 4 ++++ src/whatsapp/routers/group.router.ts | 18 ++++++++++++++++++ src/whatsapp/services/whatsapp.service.ts | 11 +++++++++++ 6 files changed, 50 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f0c56235f..828e0d1b7 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "@figuro/chatwoot-sdk": "^1.1.16", "@hapi/boom": "^10.0.1", "@sentry/node": "^7.59.2", - "@whiskeysockets/baileys": "^6.5.0", + "@whiskeysockets/baileys": "6.5.0", "amqplib": "^0.10.3", "aws-sdk": "^2.1499.0", "axios": "^1.3.5", diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index efc6f685a..498d9982e 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -771,6 +771,16 @@ export const groupInviteSchema: JSONSchema7 = { ...isNotEmpty('inviteCode'), }; +export const AcceptGroupInviteSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + inviteCode: { type: 'string', pattern: '^[a-zA-Z0-9]{22}$' }, + }, + required: ['inviteCode'], + ...isNotEmpty('inviteCode'), +}; + export const updateParticipantsSchema: JSONSchema7 = { $id: v4(), type: 'object', diff --git a/src/whatsapp/controllers/group.controller.ts b/src/whatsapp/controllers/group.controller.ts index 0cf093ca2..e452bc0c0 100644 --- a/src/whatsapp/controllers/group.controller.ts +++ b/src/whatsapp/controllers/group.controller.ts @@ -1,5 +1,6 @@ import { Logger } from '../../config/logger.config'; import { + AcceptGroupInvite, CreateGroupDto, GetParticipant, GroupDescriptionDto, @@ -65,6 +66,11 @@ export class GroupController { return await this.waMonitor.waInstances[instance.instanceName].sendInvite(data); } + public async acceptInviteCode(instance: InstanceDto, inviteCode: AcceptGroupInvite) { + logger.verbose('requested acceptInviteCode from ' + instance.instanceName + ' instance'); + return await this.waMonitor.waInstances[instance.instanceName].acceptInviteCode(inviteCode); + } + public async revokeInviteCode(instance: InstanceDto, groupJid: GroupJid) { logger.verbose('requested revokeInviteCode from ' + instance.instanceName + ' instance'); return await this.waMonitor.waInstances[instance.instanceName].revokeInviteCode(groupJid); diff --git a/src/whatsapp/dto/group.dto.ts b/src/whatsapp/dto/group.dto.ts index 6dfdc45c9..293329d2e 100644 --- a/src/whatsapp/dto/group.dto.ts +++ b/src/whatsapp/dto/group.dto.ts @@ -32,6 +32,10 @@ export class GroupInvite { inviteCode: string; } +export class AcceptGroupInvite { + inviteCode: string; +} + export class GroupSendInvite { groupJid: string; description: string; diff --git a/src/whatsapp/routers/group.router.ts b/src/whatsapp/routers/group.router.ts index f59e82a41..bf0881294 100644 --- a/src/whatsapp/routers/group.router.ts +++ b/src/whatsapp/routers/group.router.ts @@ -2,6 +2,7 @@ import { RequestHandler, Router } from 'express'; import { Logger } from '../../config/logger.config'; import { + AcceptGroupInviteSchema, createGroupSchema, getParticipantsSchema, groupInviteSchema, @@ -16,6 +17,7 @@ import { } from '../../validate/validate.schema'; import { RouterBroker } from '../abstract/abstract.router'; import { + AcceptGroupInvite, CreateGroupDto, GetParticipant, GroupDescriptionDto, @@ -182,6 +184,22 @@ export class GroupRouter extends RouterBroker { res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('acceptInviteCode'), ...guards, async (req, res) => { + logger.verbose('request received in acceptInviteCode'); + logger.verbose('request body: '); + logger.verbose(req.body); + + logger.verbose('request query: '); + logger.verbose(req.query); + const response = await this.inviteCodeValidate({ + request: req, + schema: AcceptGroupInviteSchema, + ClassRef: AcceptGroupInvite, + execute: (instance, data) => groupController.acceptInviteCode(instance, data), + }); + + res.status(HttpStatus.OK).json(response); + }) .post(this.routerPath('sendInvite'), ...guards, async (req, res) => { logger.verbose('request received in sendInvite'); logger.verbose('request body: '); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index c49df6bf8..024b88bd5 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -89,6 +89,7 @@ import { WhatsAppNumberDto, } from '../dto/chat.dto'; import { + AcceptGroupInvite, CreateGroupDto, GetParticipant, GroupDescriptionDto, @@ -3744,6 +3745,16 @@ export class WAStartupService { } } + public async acceptInviteCode(id: AcceptGroupInvite) { + this.logger.verbose('Joining the group by invitation code: ' + id.inviteCode); + try { + const groupJid = await this.client.groupAcceptInvite(id.inviteCode); + return { accepted: true, groupJid: groupJid }; + } catch (error) { + throw new NotFoundException('Accept invite error', error.toString()); + } + } + public async revokeInviteCode(id: GroupJid) { this.logger.verbose('Revoking invite code for group: ' + id.groupJid); try {