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

Join in Group by Invite Code #373

Merged
merged 1 commit into from
Jan 21, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions src/whatsapp/controllers/group.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger } from '../../config/logger.config';
import {
AcceptGroupInvite,
CreateGroupDto,
GetParticipant,
GroupDescriptionDto,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/whatsapp/dto/group.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class GroupInvite {
inviteCode: string;
}

export class AcceptGroupInvite {
inviteCode: string;
}

export class GroupSendInvite {
groupJid: string;
description: string;
Expand Down
18 changes: 18 additions & 0 deletions src/whatsapp/routers/group.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RequestHandler, Router } from 'express';

import { Logger } from '../../config/logger.config';
import {
AcceptGroupInviteSchema,
createGroupSchema,
getParticipantsSchema,
groupInviteSchema,
Expand All @@ -16,6 +17,7 @@ import {
} from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router';
import {
AcceptGroupInvite,
CreateGroupDto,
GetParticipant,
GroupDescriptionDto,
Expand Down Expand Up @@ -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<AcceptGroupInvite>({
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: ');
Expand Down
11 changes: 11 additions & 0 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import {
WhatsAppNumberDto,
} from '../dto/chat.dto';
import {
AcceptGroupInvite,
CreateGroupDto,
GetParticipant,
GroupDescriptionDto,
Expand Down Expand Up @@ -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 {
Expand Down