From 801fc3f9fd27e22651c52a47138aa8e1d7e0f073 Mon Sep 17 00:00:00 2001 From: devlikepro Date: Sat, 14 Dec 2024 19:06:34 +0700 Subject: [PATCH] [core] Join group via code fix #688 --- src/api/groups.controller.ts | 27 ++++++++++++++++++++ src/core/abc/session.abc.ts | 8 ++++++ src/core/engines/noweb/session.noweb.core.ts | 8 ++++++ src/core/engines/webjs/session.webjs.core.ts | 8 ++++++ src/structures/groups.dto.ts | 17 ++++++++++++ 5 files changed, 68 insertions(+) diff --git a/src/api/groups.controller.ts b/src/api/groups.controller.ts index d836ea3a..c23efba8 100644 --- a/src/api/groups.controller.ts +++ b/src/api/groups.controller.ts @@ -23,6 +23,8 @@ import { CreateGroupRequest, DescriptionRequest, GroupsPaginationParams, + JoinGroupRequest, + JoinGroupResponse, ParticipantsRequest, SettingsSecurityChangeInfo, SubjectRequest, @@ -44,6 +46,31 @@ export class GroupsController { return session.createGroup(request); } + @Get('join-info') + @SessionApiParam + @ApiOperation({ summary: 'Get info about the group before joining.' }) + async joinInfoGroup( + @WorkingSessionParam session: WhatsappSession, + @Query() query: JoinGroupRequest, + ): Promise { + // https://chat.whatsapp.com/123 => 123 + const code = query.code.split('/').pop(); + return session.joinInfoGroup(code); + } + + @Post('join') + @SessionApiParam + @ApiOperation({ summary: 'Join group via code' }) + async joinGroup( + @WorkingSessionParam session: WhatsappSession, + @Body() request: JoinGroupRequest, + ): Promise { + // https://chat.whatsapp.com/123 => 123 + const code = request.code.split('/').pop(); + const id = await session.joinGroup(code); + return { id: id }; + } + @Get('') @SessionApiParam @ApiOperation({ summary: 'Get all groups.' }) diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index af6d0199..be45d563 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -523,6 +523,14 @@ export abstract class WhatsappSession { throw new NotImplementedByEngineError(); } + public joinGroup(code: string): Promise { + throw new NotImplementedByEngineError(); + } + + public joinInfoGroup(code: string): Promise { + throw new NotImplementedByEngineError(); + } + public getGroups(pagination: PaginationParams) { throw new NotImplementedByEngineError(); } diff --git a/src/core/engines/noweb/session.noweb.core.ts b/src/core/engines/noweb/session.noweb.core.ts index e8f1a9b4..6aaa2b14 100644 --- a/src/core/engines/noweb/session.noweb.core.ts +++ b/src/core/engines/noweb/session.noweb.core.ts @@ -1084,6 +1084,14 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { return this.sock.groupCreate(request.name, participants); } + public joinGroup(code: string) { + return this.sock.groupAcceptInvite(code); + } + + public joinInfoGroup(code: string) { + return this.sock.groupGetInviteInfo(code); + } + public async getGroups(pagination: PaginationParams) { const groups = await this.store.getGroups(pagination); // return {id: group} mapping for backward compatability diff --git a/src/core/engines/webjs/session.webjs.core.ts b/src/core/engines/webjs/session.webjs.core.ts index ebaa9bdb..a363cad8 100644 --- a/src/core/engines/webjs/session.webjs.core.ts +++ b/src/core/engines/webjs/session.webjs.core.ts @@ -762,6 +762,14 @@ export class WhatsappSessionWebJSCore extends WhatsappSession { return this.whatsapp.createGroup(request.name, participantIds); } + public joinGroup(code: string) { + return this.whatsapp.acceptInvite(code); + } + + public joinInfoGroup(code: string) { + return this.whatsapp.getInviteInfo(code); + } + public async getInfoAdminsOnly(id): Promise { const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat; return { diff --git a/src/structures/groups.dto.ts b/src/structures/groups.dto.ts index 70ac2f79..6d645d20 100644 --- a/src/structures/groups.dto.ts +++ b/src/structures/groups.dto.ts @@ -57,6 +57,23 @@ export class CreateGroupRequest { participants: Array; } +export class JoinGroupRequest { + @ApiProperty({ + description: 'Group code (123) or url (https://chat.whatsapp.com/123)', + example: 'https://chat.whatsapp.com/1234567890abcdef', + }) + @IsString() + code: string; +} + +export class JoinGroupResponse { + @ApiProperty({ + description: 'Group ID', + example: '123@g.us', + }) + id: string; +} + export enum GroupSortField { ID = 'id', SUBJECT = 'subject',