Spaces:
Sleeping
Sleeping
Download src/modules/group/group.controller.ts from jinksQspider/openWa: direct link, hf CLI and curl.
- Browser
- Download file 8.08 kB
-
https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/group/group.controller.ts
- Command line
-
hf download hf://spaces/jinksQspider/openWa/src/modules/group/group.controller.ts
-
curl -L -o group.controller.ts https://huggingface.co/spaces/jinksQspider/openWa/resolve/main/src/modules/group/group.controller.ts
8.08 kB
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Put, | |
| Delete, | |
| Param, | |
| Body, | |
| HttpCode, | |
| HttpStatus, | |
| BadRequestException, | |
| NotFoundException, | |
| } from '@nestjs/common'; | |
| import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@nestjs/swagger'; | |
| import { SessionService } from '../session/session.service'; | |
| import { CreateGroupDto, ParticipantsDto, GroupSubjectDto, GroupDescriptionDto } from './dto/group.dto'; | |
| ('groups') | |
| ('sessions/:sessionId/groups') | |
| export class GroupController { | |
| constructor(private readonly sessionService: SessionService) {} | |
| () | |
| ({ summary: 'Get all groups for a session' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ status: 200, description: 'List of groups' }) | |
| async findAll(('sessionId') sessionId: string) { | |
| const engine = this.getEngine(sessionId); | |
| return engine.getGroups(); | |
| } | |
| (':groupId') | |
| ({ summary: 'Get detailed group info' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID (e.g., 120363xxx@g.us)' }) | |
| ({ status: 200, description: 'Group details with participants' }) | |
| ({ status: 404, description: 'Group not found' }) | |
| async findOne(('sessionId') sessionId: string, ('groupId') groupId: string) { | |
| const engine = this.getEngine(sessionId); | |
| const group = await engine.getGroupInfo(groupId); | |
| if (!group) { | |
| throw new NotFoundException(`Group ${groupId} not found`); | |
| } | |
| return group; | |
| } | |
| () | |
| ({ summary: 'Create a new group' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ type: CreateGroupDto }) | |
| ({ status: 201, description: 'Group created' }) | |
| async create(('sessionId') sessionId: string, () dto: CreateGroupDto) { | |
| const engine = this.getEngine(sessionId); | |
| return engine.createGroup(dto.name, dto.participants); | |
| } | |
| (':groupId/participants') | |
| ({ summary: 'Add participants to a group' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: ParticipantsDto }) | |
| ({ status: 200, description: 'Participants added' }) | |
| (HttpStatus.OK) | |
| async addParticipants( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: ParticipantsDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.addParticipants(groupId, dto.participants); | |
| return { success: true, message: 'Participants added' }; | |
| } | |
| (':groupId/participants') | |
| ({ summary: 'Remove participants from a group' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: ParticipantsDto }) | |
| ({ status: 200, description: 'Participants removed' }) | |
| async removeParticipants( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: ParticipantsDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.removeParticipants(groupId, dto.participants); | |
| return { success: true, message: 'Participants removed' }; | |
| } | |
| (':groupId/participants/promote') | |
| ({ summary: 'Promote participants to admin' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: ParticipantsDto }) | |
| ({ status: 200, description: 'Participants promoted' }) | |
| (HttpStatus.OK) | |
| async promoteParticipants( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: ParticipantsDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.promoteParticipants(groupId, dto.participants); | |
| return { success: true, message: 'Participants promoted to admin' }; | |
| } | |
| (':groupId/participants/demote') | |
| ({ summary: 'Demote participants from admin' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: ParticipantsDto }) | |
| ({ status: 200, description: 'Participants demoted' }) | |
| (HttpStatus.OK) | |
| async demoteParticipants( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: ParticipantsDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.demoteParticipants(groupId, dto.participants); | |
| return { success: true, message: 'Participants demoted from admin' }; | |
| } | |
| (':groupId/subject') | |
| ({ summary: 'Change group name/subject' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: GroupSubjectDto }) | |
| ({ status: 200, description: 'Subject updated' }) | |
| async setSubject( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: GroupSubjectDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.setGroupSubject(groupId, dto.subject); | |
| return { success: true, message: 'Group subject updated' }; | |
| } | |
| (':groupId/description') | |
| ({ summary: 'Change group description' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ type: GroupDescriptionDto }) | |
| ({ status: 200, description: 'Description updated' }) | |
| async setDescription( | |
| ('sessionId') sessionId: string, | |
| ('groupId') groupId: string, | |
| () dto: GroupDescriptionDto, | |
| ) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.setGroupDescription(groupId, dto.description); | |
| return { success: true, message: 'Group description updated' }; | |
| } | |
| (':groupId/leave') | |
| ({ summary: 'Leave a group' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ status: 200, description: 'Left the group' }) | |
| (HttpStatus.OK) | |
| async leave(('sessionId') sessionId: string, ('groupId') groupId: string) { | |
| const engine = this.getEngine(sessionId); | |
| await engine.leaveGroup(groupId); | |
| return { success: true, message: 'Left the group' }; | |
| } | |
| // ========== Gap Quick Wins: Invite Link ========== | |
| (':groupId/invite-code') | |
| ({ summary: 'Get group invite code/link' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ status: 200, description: 'Group invite code' }) | |
| async getInviteCode(('sessionId') sessionId: string, ('groupId') groupId: string) { | |
| const engine = this.getEngine(sessionId); | |
| const inviteCode = await engine.getGroupInviteCode(groupId); | |
| return { | |
| inviteCode, | |
| inviteLink: `https://chat.whatsapp.com/${inviteCode}`, | |
| }; | |
| } | |
| (':groupId/invite-code/revoke') | |
| (HttpStatus.OK) | |
| ({ summary: 'Revoke group invite code and generate new one' }) | |
| ({ name: 'sessionId', description: 'Session ID' }) | |
| ({ name: 'groupId', description: 'Group ID' }) | |
| ({ status: 200, description: 'New invite code generated' }) | |
| async revokeInviteCode(('sessionId') sessionId: string, ('groupId') groupId: string) { | |
| const engine = this.getEngine(sessionId); | |
| const newCode = await engine.revokeGroupInviteCode(groupId); | |
| return { | |
| inviteCode: newCode, | |
| inviteLink: `https://chat.whatsapp.com/${newCode}`, | |
| message: 'Invite code revoked and new one generated', | |
| }; | |
| } | |
| private getEngine(sessionId: string) { | |
| const engine = this.sessionService.getEngine(sessionId); | |
| if (!engine) { | |
| throw new BadRequestException('Session is not started'); | |
| } | |
| return engine; | |
| } | |
| } | |