Skip to content

Commit

Permalink
[core] Add WebJSEngineConfigService.ts
Browse files Browse the repository at this point in the history
fix #654 fix #653
  • Loading branch information
devlikepro committed Nov 24, 2024
1 parent abb5bdf commit 7cadbad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/core/app.module.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ServerDebugController,
} from '@waha/api/server.controller';
import { WebsocketGatewayCore } from '@waha/core/api/websocket.gateway.core';
import { WebJSEngineConfigService } from '@waha/core/config/WebJSEngineConfigService';
import { MediaLocalStorageModule } from '@waha/core/media/local/media.local.storage.module';
import { MediaLocalStorageConfig } from '@waha/core/media/local/MediaLocalStorageConfig';
import { BufferJsonReplacerInterceptor } from '@waha/nestjs/BufferJsonReplacerInterceptor';
Expand Down Expand Up @@ -146,6 +147,7 @@ const PROVIDERS = [
},
DashboardConfigServiceCore,
SwaggerConfigServiceCore,
WebJSEngineConfigService,
WhatsappConfigService,
EngineConfigService,
WebsocketGatewayCore,
Expand Down
37 changes: 37 additions & 0 deletions src/core/config/WebJSEngineConfigService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { WebJSConfig } from '../../core/engines/webjs/session.webjs.core';

@Injectable()
export class WebJSEngineConfigService {
constructor(protected configService: ConfigService) {}

getConfig(): WebJSConfig {
let webVersion = this.configService.get<string>(
'WAHA_WEBJS_WEB_VERSION',
undefined,
);
if (webVersion === '2.2412.54-videofix') {
// Deprecated version
webVersion = undefined;
}
return {
webVersion: webVersion,
cacheType: this.getCacheType(),
};
}

getCacheType(): 'local' | 'none' {
const cacheType = this.configService
.get<string>('WAHA_WEBJS_CACHE_TYPE', 'local')
.toLowerCase();
if (cacheType != 'local' && cacheType != 'none') {
throw new Error(
'Invalid cache type, only "local" and "none" are allowed',
);
}

return cacheType;
}
}
10 changes: 7 additions & 3 deletions src/core/engines/webjs/session.webjs.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const QRCode = require('qrcode');

export interface WebJSConfig {
webVersion?: string;
cacheType: 'local' | 'none';
}

export class WhatsappSessionWebJSCore extends WhatsappSession {
Expand Down Expand Up @@ -143,7 +144,11 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
const path = this.getClassDirName();
const webVersion =
this.engineConfig?.webVersion || '2.3000.1018072227-alpha';
this.logger.info(`Using web version: '${webVersion}'`);
const cacheType = this.engineConfig?.cacheType || 'local';
this.logger.info(`Using cache type: '${cacheType}'`);
if (cacheType === 'local') {
this.logger.info(`Using web version: '${webVersion}'`);
}
return {
puppeteer: {
headless: true,
Expand All @@ -153,8 +158,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
},
webVersion: webVersion,
webVersionCache: {
// type: 'none',
type: 'local',
type: cacheType,
path: path,
strict: true,
},
Expand Down
5 changes: 5 additions & 0 deletions src/core/manager.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
NotFoundException,
UnprocessableEntityException,
} from '@nestjs/common';
import { WebJSEngineConfigService } from '@waha/core/config/WebJSEngineConfigService';
import { WebhookConductor } from '@waha/core/integrations/webhooks/WebhookConductor';
import { MediaStorageFactory } from '@waha/core/media/MediaStorageFactory';
import { DefaultMap } from '@waha/utils/DefaultMap';
Expand Down Expand Up @@ -69,6 +70,7 @@ export class SessionManagerCore extends SessionManager {
constructor(
config: WhatsappConfigService,
private engineConfigService: EngineConfigService,
private webjsEngineConfigService: WebJSEngineConfigService,
log: PinoLogger,
private mediaStorageFactory: MediaStorageFactory,
) {
Expand Down Expand Up @@ -175,6 +177,9 @@ export class SessionManagerCore extends SessionManager {
proxyConfig: proxyConfig,
sessionConfig: this.sessionConfig,
};
if (this.EngineClass === WhatsappSessionWebJSCore) {
sessionConfig.engineConfig = this.webjsEngineConfigService.getConfig();
}
await this.sessionAuthRepository.init(name);
// @ts-ignore
const session = new this.EngineClass(sessionConfig);
Expand Down

0 comments on commit 7cadbad

Please sign in to comment.