Skip to content

Commit

Permalink
[core] Join group via code
Browse files Browse the repository at this point in the history
fix #688
  • Loading branch information
devlikepro committed Dec 14, 2024
1 parent 703b0b4 commit 801fc3f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/api/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
CreateGroupRequest,
DescriptionRequest,
GroupsPaginationParams,
JoinGroupRequest,
JoinGroupResponse,
ParticipantsRequest,
SettingsSecurityChangeInfo,
SubjectRequest,
Expand All @@ -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<any> {
// 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<JoinGroupResponse> {
// 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.' })
Expand Down
8 changes: 8 additions & 0 deletions src/core/abc/session.abc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,14 @@ export abstract class WhatsappSession {
throw new NotImplementedByEngineError();
}

public joinGroup(code: string): Promise<string> {
throw new NotImplementedByEngineError();
}

public joinInfoGroup(code: string): Promise<any> {
throw new NotImplementedByEngineError();
}

public getGroups(pagination: PaginationParams) {
throw new NotImplementedByEngineError();
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/engines/noweb/session.noweb.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/core/engines/webjs/session.webjs.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingsSecurityChangeInfo> {
const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat;
return {
Expand Down
17 changes: 17 additions & 0 deletions src/structures/groups.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ export class CreateGroupRequest {
participants: Array<Participant>;
}

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: '[email protected]',
})
id: string;
}

export enum GroupSortField {
ID = 'id',
SUBJECT = 'subject',
Expand Down

0 comments on commit 801fc3f

Please sign in to comment.