diff --git a/src/api/sessions.controller.ts b/src/api/sessions.controller.ts index 2dfde98d..eba2ad3f 100644 --- a/src/api/sessions.controller.ts +++ b/src/api/sessions.controller.ts @@ -67,7 +67,7 @@ class SessionsController { @SessionApiParam @UsePipes(new WAHAValidationPipe()) async get(@Param('session') name: string): Promise { - const session = this.manager.getSessionInfo(name); + const session = await this.manager.getSessionInfo(name); if (session === null) { throw new NotFoundException('Session not found'); } diff --git a/src/core/abc/manager.abc.ts b/src/core/abc/manager.abc.ts index b7e3ebaf..92093ad8 100644 --- a/src/core/abc/manager.abc.ts +++ b/src/core/abc/manager.abc.ts @@ -6,6 +6,7 @@ import { EventEmitter } from 'events'; import { WAHAEngine, WAHAEvents } from '../../structures/enums.dto'; import { SessionConfig, + SessionDetailedInfo, SessionDTO, SessionInfo, } from '../../structures/sessions.dto'; @@ -50,7 +51,7 @@ export abstract class SessionManager implements BeforeApplicationShutdown { abstract getSession(name: string): WhatsappSession; - abstract getSessionInfo(name: string): Promise; + abstract getSessionInfo(name: string): Promise; abstract getSessions(all: boolean): Promise; diff --git a/src/core/manager.core.ts b/src/core/manager.core.ts index b2368a25..0f62e9d2 100644 --- a/src/core/manager.core.ts +++ b/src/core/manager.core.ts @@ -19,6 +19,7 @@ import { import { ProxyConfig, SessionConfig, + SessionDetailedInfo, SessionDTO, SessionInfo, } from '../structures/sessions.dto'; @@ -270,11 +271,23 @@ export class SessionManagerCore extends SessionManager { const session = this.session as WhatsappSession; const me = session.getSessionMeInfo(); + return [ + { + name: session.name, + status: session.status, + config: session.sessionConfig, + me: me, + }, + ]; + } + + private async fetchEngineInfo() { + const session = this.session as WhatsappSession; // Get engine info let engineInfo = {}; if (session) { try { - engineInfo = await promiseTimeout(10, session.getEngineInfo()); + engineInfo = await promiseTimeout(1000, session.getEngineInfo()); } catch (error) { this.log.warn( { session: this.session, error: error }, @@ -286,21 +299,19 @@ export class SessionManagerCore extends SessionManager { engine: session?.engine, ...engineInfo, }; - return [ - { - name: session.name, - status: session.status, - config: session.sessionConfig, - me: me, - engine: engine, - }, - ]; + return engine; } - async getSessionInfo(name: string): Promise { + async getSessionInfo(name: string): Promise { if (name !== this.DEFAULT) { return null; } - return this.getSessions(true).then((sessions) => sessions[0]); + const sessions = await this.getSessions(true); + if (sessions.length === 0) { + return null; + } + const session = sessions[0]; + const engine = await this.fetchEngineInfo(); + return { ...session, engine: engine }; } } diff --git a/src/structures/sessions.dto.ts b/src/structures/sessions.dto.ts index e9bc948e..6c54832c 100644 --- a/src/structures/sessions.dto.ts +++ b/src/structures/sessions.dto.ts @@ -153,6 +153,9 @@ export class MeInfo { export class SessionInfo extends SessionDTO { me?: MeInfo; +} + +export class SessionDetailedInfo extends SessionInfo { engine?: any; }