Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Delete, | |
| Get, | |
| HttpCode, | |
| HttpStatus, | |
| Param, | |
| Post, | |
| Put, | |
| Query, | |
| UsePipes, | |
| ValidationPipe, | |
| } from '@nestjs/common'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { GroupIdApiParam } from '@waha/nestjs/params/ChatIdApiParam'; | |
| import { | |
| SessionApiParam, | |
| WorkingSessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { CountResponse, Result } from '@waha/structures/base.dto'; | |
| import { | |
| ChatPictureQuery, | |
| ChatPictureResponse, | |
| } from '@waha/structures/chats.dto'; | |
| import { ProfilePictureRequest } from '@waha/structures/profile.dto'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| import { parseGroupInviteLink, WhatsappSession } from '../core/abc/session.abc'; | |
| import { | |
| CreateGroupRequest, | |
| DescriptionRequest, | |
| GroupField, | |
| GroupsListFields, | |
| GroupsPaginationParams, | |
| JoinGroupRequest, | |
| JoinGroupResponse, | |
| ParticipantsRequest, | |
| SettingsSecurityChangeInfo, | |
| SubjectRequest, | |
| } from '../structures/groups.dto'; | |
| ('api_key') | |
| ('api/:session/groups') | |
| ('👥 Groups') | |
| export class GroupsController { | |
| constructor(private manager: SessionManager) {} | |
| ('') | |
| ({ summary: 'Create a new group.' }) | |
| createGroup( | |
| session: WhatsappSession, | |
| () request: CreateGroupRequest, | |
| ) { | |
| return session.createGroup(request); | |
| } | |
| ('join-info') | |
| ({ summary: 'Get info about the group before joining.' }) | |
| async joinInfoGroup( | |
| session: WhatsappSession, | |
| () query: JoinGroupRequest, | |
| ): Promise<any> { | |
| const code = parseGroupInviteLink(query.code); | |
| return session.joinInfoGroup(code); | |
| } | |
| ('join') | |
| (HttpStatus.OK) | |
| ({ summary: 'Join group via code' }) | |
| async joinGroup( | |
| session: WhatsappSession, | |
| () request: JoinGroupRequest, | |
| ): Promise<JoinGroupResponse> { | |
| const code = parseGroupInviteLink(request.code); | |
| const id = await session.joinGroup(code); | |
| return { id: id }; | |
| } | |
| ('') | |
| ({ summary: 'Get all groups.' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getGroups( | |
| session: WhatsappSession, | |
| () pagination: GroupsPaginationParams, | |
| () fields: GroupsListFields, | |
| ) { | |
| let groups: any = await session.getGroups(pagination); | |
| groups = session.filterGroupsFields(groups, fields); | |
| return groups; | |
| } | |
| ('/count') | |
| ({ summary: 'Get the number of groups.' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getGroupsCount( | |
| session: WhatsappSession, | |
| ): Promise<CountResponse> { | |
| const data: any = await session.getGroups({}); | |
| const groups: any[] = Array.isArray(data) ? data : Object.values(data); | |
| return { | |
| count: groups.length, | |
| }; | |
| } | |
| ('refresh') | |
| (HttpStatus.OK) | |
| ({ summary: 'Refresh groups from the server.' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async refreshGroups( session: WhatsappSession) { | |
| return { success: await session.refreshGroups() }; | |
| } | |
| (':id') | |
| ({ summary: 'Get the group.' }) | |
| getGroup( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ) { | |
| return session.getGroup(id); | |
| } | |
| (':id') | |
| ({ summary: 'Delete the group.' }) | |
| deleteGroup( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ) { | |
| return session.deleteGroup(id); | |
| } | |
| (':id/leave') | |
| (HttpStatus.OK) | |
| ({ summary: 'Leave the group.' }) | |
| leaveGroup( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ) { | |
| return session.leaveGroup(id); | |
| } | |
| (':id/picture') | |
| ({ summary: 'Get group picture' }) | |
| (new ValidationPipe({ transform: true, whitelist: true })) | |
| async getChatPicture( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () query: ChatPictureQuery, | |
| ): Promise<ChatPictureResponse> { | |
| const url = await session.getContactProfilePicture(id, query.refresh); | |
| return { url: url }; | |
| } | |
| (':id/picture') | |
| ({ summary: 'Set group picture' }) | |
| async setPicture( | |
| ('id') id: string, | |
| session: WhatsappSession, | |
| () request: ProfilePictureRequest, | |
| ): Promise<Result> { | |
| const success = await session.updateGroupPicture(id, request.file); | |
| return { success: success ?? true }; | |
| } | |
| (':id/picture') | |
| ({ summary: 'Delete group picture' }) | |
| async deletePicture( | |
| ('id') id: string, | |
| session: WhatsappSession, | |
| ): Promise<Result> { | |
| const success = await session.updateGroupPicture(id, null); | |
| return { success: success || true }; | |
| } | |
| (':id/description') | |
| ({ | |
| summary: 'Updates the group description.', | |
| description: | |
| 'Returns "true" if the subject was properly updated. This can return "false" if the user does not have the necessary permissions.', | |
| }) | |
| setDescription( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: DescriptionRequest, | |
| ) { | |
| return session.setDescription(id, request.description); | |
| } | |
| (':id/subject') | |
| ({ | |
| summary: 'Updates the group subject', | |
| description: | |
| 'Returns "true" if the subject was properly updated. This can return "false" if the user does not have the necessary permissions.', | |
| }) | |
| setSubject( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: SubjectRequest, | |
| ) { | |
| return session.setSubject(id, request.subject); | |
| } | |
| (':id/settings/security/info-admin-only') | |
| ({ | |
| summary: 'Updates the group "info admin only" settings.', | |
| description: | |
| 'You can allow only admins to edit group info (title, description, photo).', | |
| }) | |
| setInfoAdminOnly( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: SettingsSecurityChangeInfo, | |
| ) { | |
| return session.setInfoAdminsOnly(id, request.adminsOnly); | |
| } | |
| (':id/settings/security/info-admin-only') | |
| ({ | |
| summary: "Get the group's 'info admin only' settings.", | |
| description: | |
| 'You can allow only admins to edit group info (title, description, photo).', | |
| }) | |
| getInfoAdminOnly( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<SettingsSecurityChangeInfo> { | |
| return session.getInfoAdminsOnly(id); | |
| } | |
| (':id/settings/security/messages-admin-only') | |
| ({ | |
| summary: 'Update settings - who can send messages', | |
| description: | |
| 'Updates the group settings to only allow admins to send messages.', | |
| }) | |
| setMessagesAdminOnly( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: SettingsSecurityChangeInfo, | |
| ) { | |
| return session.setMessagesAdminsOnly(id, request.adminsOnly); | |
| } | |
| (':id/settings/security/messages-admin-only') | |
| ({ | |
| summary: 'Get settings - who can send messages', | |
| description: 'The group settings to only allow admins to send messages.', | |
| }) | |
| getMessagesAdminOnly( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<SettingsSecurityChangeInfo> { | |
| return session.getMessagesAdminsOnly(id); | |
| } | |
| (':id/invite-code') | |
| ({ summary: 'Gets the invite code for the group.' }) | |
| getInviteCode( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<string> { | |
| return session.getInviteCode(id); | |
| } | |
| (':id/invite-code/revoke') | |
| (HttpStatus.OK) | |
| ({ | |
| summary: | |
| 'Invalidates the current group invite code and generates a new one.', | |
| }) | |
| revokeInviteCode( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ): Promise<string> { | |
| return session.revokeInviteCode(id); | |
| } | |
| (':id/participants/') | |
| ({ summary: 'Get participants' }) | |
| getParticipants( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| ) { | |
| return session.getParticipants(id); | |
| } | |
| (':id/participants/add') | |
| (HttpStatus.OK) | |
| ({ summary: 'Add participants' }) | |
| addParticipants( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: ParticipantsRequest, | |
| ) { | |
| return session.addParticipants(id, request); | |
| } | |
| (':id/participants/remove') | |
| (HttpStatus.OK) | |
| ({ | |
| summary: 'Remove participants', | |
| }) | |
| removeParticipants( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: ParticipantsRequest, | |
| ) { | |
| return session.removeParticipants(id, request); | |
| } | |
| (':id/admin/promote') | |
| (HttpStatus.OK) | |
| ({ summary: 'Promote participants to admin users.' }) | |
| promoteToAdmin( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: ParticipantsRequest, | |
| ) { | |
| return session.promoteParticipantsToAdmin(id, request); | |
| } | |
| (':id/admin/demote') | |
| (HttpStatus.OK) | |
| ({ summary: 'Demotes participants to regular users.' }) | |
| demoteToAdmin( | |
| session: WhatsappSession, | |
| ('id') id: string, | |
| () request: ParticipantsRequest, | |
| ) { | |
| return session.demoteParticipantsToUser(id, request); | |
| } | |
| } | |