Spaces:
Runtime error
Runtime error
| import { | |
| Body, | |
| Controller, | |
| Delete, | |
| Get, | |
| NotFoundException, | |
| Param, | |
| Post, | |
| Put, | |
| Query, | |
| UsePipes, | |
| } from '@nestjs/common'; | |
| import { UnprocessableEntityException } from '@nestjs/common/exceptions/unprocessable-entity.exception'; | |
| import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger'; | |
| import { | |
| SessionApiParam, | |
| SessionParam, | |
| } from '@waha/nestjs/params/SessionApiParam'; | |
| import { WAHAValidationPipe } from '@waha/nestjs/pipes/WAHAValidationPipe'; | |
| import { | |
| SessionLogoutDeprecatedRequest, | |
| SessionStartDeprecatedRequest, | |
| SessionStopDeprecatedRequest, | |
| } from '@waha/structures/sessions.deprecated.dto'; | |
| import { generatePrefixedId } from '@waha/utils/ids'; | |
| import { SessionManager } from '../core/abc/manager.abc'; | |
| import { WhatsappSession } from '../core/abc/session.abc'; | |
| import { | |
| ListSessionsQuery, | |
| MeInfo, | |
| SessionCreateRequest, | |
| SessionDTO, | |
| SessionInfo, | |
| SessionUpdateRequest, | |
| } from '../structures/sessions.dto'; | |
| ('api_key') | |
| ('api/sessions') | |
| ('🖥️ Sessions') | |
| class SessionsController { | |
| constructor(private manager: SessionManager) {} | |
| private withLock(name: string, fn: () => any) { | |
| return this.manager.withLock(name, fn); | |
| } | |
| ('/') | |
| ({ summary: 'List all sessions' }) | |
| list( | |
| (new WAHAValidationPipe()) query: ListSessionsQuery, | |
| ): Promise<SessionInfo[]> { | |
| return this.manager.getSessions(query.all); | |
| } | |
| ('/:session') | |
| ({ summary: 'Get session information' }) | |
| (new WAHAValidationPipe()) | |
| async get(('session') name: string): Promise<SessionInfo> { | |
| const session = await this.manager.getSessionInfo(name); | |
| if (session === null) { | |
| throw new NotFoundException('Session not found'); | |
| } | |
| return session; | |
| } | |
| (':session/me') | |
| ({ summary: 'Get information about the authenticated account' }) | |
| getMe( session: WhatsappSession): MeInfo | null { | |
| return session.getSessionMeInfo(); | |
| } | |
| ('') | |
| ({ | |
| summary: 'Create a session', | |
| description: | |
| 'Create session a new session (and start it at the same time if required).', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async create(() request: SessionCreateRequest): Promise<SessionDTO> { | |
| const name = request.name || generatePrefixedId('session'); | |
| await this.withLock(name, async () => { | |
| if (await this.manager.exists(name)) { | |
| const msg = `Session '${name}' already exists. Use PUT to update it.`; | |
| throw new UnprocessableEntityException(msg); | |
| } | |
| const config = request.config; | |
| const start = request.start || false; | |
| await this.manager.upsert(name, config); | |
| if (start) { | |
| await this.manager.assign(name); | |
| await this.manager.start(name); | |
| } | |
| }); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| (':session') | |
| ({ | |
| summary: 'Update a session', | |
| description: '', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async update( | |
| ('session') name: string, | |
| () request: SessionUpdateRequest, | |
| ): Promise<SessionDTO> { | |
| await this.withLock(name, async () => { | |
| if (!(await this.manager.exists(name))) { | |
| throw new NotFoundException('Session not found'); | |
| } | |
| const config = request.config; | |
| const isRunning = this.manager.isRunning(name); | |
| await this.manager.stop(name, true); | |
| await this.manager.upsert(name, config); | |
| if (isRunning) { | |
| await this.manager.start(name); | |
| } | |
| }); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| (':session') | |
| ({ | |
| summary: 'Delete the session', | |
| description: | |
| 'Delete the session with the given name. Stop and logout as well. Idempotent operation.', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async delete(('session') name: string): Promise<void> { | |
| await this.withLock(name, async () => { | |
| await this.manager.unassign(name); | |
| await this.manager.unpair(name); | |
| await this.manager.stop(name, true); | |
| await this.manager.logout(name); | |
| await this.manager.delete(name); | |
| }); | |
| } | |
| (':session/start') | |
| ({ | |
| summary: 'Start the session', | |
| description: | |
| 'Start the session with the given name. The session must exist. Idempotent operation.', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async start(('session') name: string): Promise<SessionDTO> { | |
| await this.withLock(name, async () => { | |
| const exists = await this.manager.exists(name); | |
| if (!exists) { | |
| throw new NotFoundException('Session not found'); | |
| } | |
| await this.manager.assign(name); | |
| await this.manager.start(name); | |
| }); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| (':session/stop') | |
| ({ | |
| summary: 'Stop the session', | |
| description: 'Stop the session with the given name. Idempotent operation.', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async stop(('session') name: string): Promise<SessionDTO> { | |
| await this.withLock(name, async () => { | |
| await this.manager.unassign(name); | |
| await this.manager.stop(name, false); | |
| }); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| (':session/logout') | |
| ({ | |
| summary: 'Logout from the session', | |
| description: 'Logout the session, restart a session if it was not STOPPED', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async logout(('session') name: string): Promise<SessionDTO> { | |
| await this.withLock(name, async () => { | |
| const exists = await this.manager.exists(name); | |
| if (!exists) { | |
| throw new NotFoundException('Session not found'); | |
| } | |
| const isRunning = this.manager.isRunning(name); | |
| await this.manager.unpair(name); | |
| await this.manager.stop(name, true); | |
| await this.manager.logout(name); | |
| if (isRunning) { | |
| await this.manager.start(name); | |
| } | |
| }); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| (':session/restart') | |
| ({ | |
| summary: 'Restart the session', | |
| description: 'Restart the session with the given name.', | |
| }) | |
| (new WAHAValidationPipe()) | |
| async restart(('session') name: string): Promise<SessionDTO> { | |
| await this.manager.restart(name); | |
| return await this.manager.getSessionInfo(name); | |
| } | |
| ('/start/') | |
| ({ | |
| summary: 'Upsert and Start session', | |
| description: | |
| 'Create session (if not exists) or update a config (if exists) and start it.', | |
| deprecated: true, | |
| }) | |
| async DEPRACATED_start( | |
| () request: SessionStartDeprecatedRequest, | |
| ): Promise<SessionDTO> { | |
| const name = request.name; | |
| if (!request.name) { | |
| throw new UnprocessableEntityException('Session name is required'); | |
| } | |
| if (this.manager.isRunning(name)) { | |
| const msg = `Session '${name}' is already started.`; | |
| throw new UnprocessableEntityException(msg); | |
| } | |
| return await this.withLock(name, async () => { | |
| const config = request.config; | |
| await this.manager.upsert(name, config); | |
| await this.manager.assign(name); | |
| return await this.manager.start(name); | |
| }); | |
| } | |
| ('/stop/') | |
| ({ | |
| summary: 'Stop (and Logout if asked) session', | |
| description: 'Stop session and Logout by default.', | |
| deprecated: true, | |
| }) | |
| async DEPRECATED_stop( | |
| () request: SessionStopDeprecatedRequest, | |
| ): Promise<void> { | |
| if (!request.name) { | |
| throw new UnprocessableEntityException('Session name is required'); | |
| } | |
| const name = request.name; | |
| if (request.logout) { | |
| // Old API did remove the session complete | |
| await this.withLock(name, async () => { | |
| await this.manager.unassign(name); | |
| await this.manager.unpair(name); | |
| await this.manager.stop(name, true); | |
| await this.manager.logout(name); | |
| await this.manager.delete(name); | |
| }); | |
| } else { | |
| await this.withLock(name, async () => { | |
| await this.manager.unassign(name); | |
| await this.manager.stop(name, false); | |
| }); | |
| } | |
| return; | |
| } | |
| ('/logout/') | |
| ({ | |
| summary: 'Logout and Delete session.', | |
| description: 'Stop, Logout and Delete session.', | |
| deprecated: true, | |
| }) | |
| async DEPRECATED_logout( | |
| () request: SessionLogoutDeprecatedRequest, | |
| ): Promise<void> { | |
| if (!request.name) { | |
| throw new UnprocessableEntityException('Session name is required'); | |
| } | |
| const name = request.name; | |
| await this.withLock(name, async () => { | |
| await this.manager.unassign(name); | |
| await this.manager.unpair(name); | |
| await this.manager.stop(name, true); | |
| await this.manager.logout(name); | |
| await this.manager.delete(name); | |
| }); | |
| return; | |
| } | |
| } | |
| export { SessionsController }; | |