Adjust to repository from session worker

This commit is contained in:
Davidson Gomes 2024-06-01 19:20:59 -03:00
parent f48f331d43
commit 9354af3bc7
5 changed files with 53 additions and 24 deletions

View File

@ -49,7 +49,7 @@
"amqplib": "^0.10.3", "amqplib": "^0.10.3",
"@aws-sdk/client-sqs": "^3.569.0", "@aws-sdk/client-sqs": "^3.569.0",
"axios": "^1.6.5", "axios": "^1.6.5",
"@whiskeysockets/baileys": "^6.7.2", "@whiskeysockets/baileys": "6.6.0",
"class-validator": "^0.14.1", "class-validator": "^0.14.1",
"compression": "^1.7.4", "compression": "^1.7.4",
"cors": "^2.8.5", "cors": "^2.8.5",

View File

@ -9,7 +9,7 @@ type ResponseProvider = Promise<[ResponseSuccess?, Error?]>;
export class ProviderFiles { export class ProviderFiles {
constructor(private readonly configService: ConfigService) { constructor(private readonly configService: ConfigService) {
this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session`; this.baseUrl = `http://${this.config.HOST}:${this.config.PORT}/session/${this.config.PREFIX}`;
} }
private readonly logger = new Logger(ProviderFiles.name); private readonly logger = new Logger(ProviderFiles.name);
@ -18,8 +18,6 @@ export class ProviderFiles {
private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER')); private readonly config = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
private readonly prefix = Object.freeze(this.configService.get<ProviderSession>('PROVIDER').PREFIX);
get isEnabled() { get isEnabled() {
return !!this.config?.ENABLED; return !!this.config?.ENABLED;
} }
@ -30,7 +28,7 @@ export class ProviderFiles {
baseURL: this.baseUrl, baseURL: this.baseUrl,
}); });
try { try {
const response = await client.options(`/${this.prefix}/ping`); const response = await client.options('/ping');
if (!response?.data?.pong) { if (!response?.data?.pong) {
throw new Error('Offline file provider.'); throw new Error('Offline file provider.');
} }
@ -48,7 +46,7 @@ export class ProviderFiles {
public async create(instance: string): ResponseProvider { public async create(instance: string): ResponseProvider {
try { try {
const response = await axios.post(`${this.baseUrl}/${this.prefix}`, { const response = await axios.post(`${this.baseUrl}`, {
instance, instance,
}); });
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
@ -65,7 +63,7 @@ export class ProviderFiles {
public async write(instance: string, key: string, data: any): ResponseProvider { public async write(instance: string, key: string, data: any): ResponseProvider {
try { try {
const response = await axios.post(`${this.baseUrl}/${this.prefix}/${instance}/${key}`, data); const response = await axios.post(`${this.baseUrl}/${instance}/${key}`, data);
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -80,7 +78,7 @@ export class ProviderFiles {
public async read(instance: string, key: string): ResponseProvider { public async read(instance: string, key: string): ResponseProvider {
try { try {
const response = await axios.get(`${this.baseUrl}/${this.prefix}/${instance}/${key}`); const response = await axios.get(`${this.baseUrl}/${instance}/${key}`);
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -95,7 +93,7 @@ export class ProviderFiles {
public async delete(instance: string, key: string): ResponseProvider { public async delete(instance: string, key: string): ResponseProvider {
try { try {
const response = await axios.delete(`${this.baseUrl}/${this.prefix}/${instance}/${key}`); const response = await axios.delete(`${this.baseUrl}/${instance}/${key}`);
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [
@ -110,7 +108,7 @@ export class ProviderFiles {
public async allInstances(): ResponseProvider { public async allInstances(): ResponseProvider {
try { try {
const response = await axios.get(`${this.baseUrl}/${this.prefix}/list-instances`); const response = await axios.get(`${this.baseUrl}/list-instances`);
return [{ status: response.status, data: response?.data as string[] }]; return [{ status: response.status, data: response?.data as string[] }];
} catch (error) { } catch (error) {
return [ return [
@ -125,7 +123,7 @@ export class ProviderFiles {
public async removeSession(instance: string): ResponseProvider { public async removeSession(instance: string): ResponseProvider {
try { try {
const response = await axios.delete(`${this.baseUrl}/${this.prefix}/${instance}`); const response = await axios.delete(`${this.baseUrl}/${instance}`);
return [{ status: response.status, data: response?.data }]; return [{ status: response.status, data: response?.data }];
} catch (error) { } catch (error) {
return [ return [

View File

@ -146,7 +146,7 @@ export class BaileysStartupService extends ChannelStartupService {
this.instance.qrcode = { count: 0 }; this.instance.qrcode = { count: 0 };
this.mobile = false; this.mobile = false;
this.recoveringMessages(); this.recoveringMessages();
this.authStateProvider = new AuthStateProvider(this.configService, this.providerFiles); this.authStateProvider = new AuthStateProvider(this.providerFiles);
} }
private authStateProvider: AuthStateProvider; private authStateProvider: AuthStateProvider;
@ -1486,7 +1486,7 @@ export class BaileysStartupService extends ChannelStartupService {
}); });
const chat = chats.find((c) => c.id === data.association.chatId); const chat = chats.find((c) => c.id === data.association.chatId);
if (chat) { if (chat) {
let labels = [...chat.labels]; let labels = [...chat?.labels];
if (data.type === 'remove') { if (data.type === 'remove') {
labels = labels.filter((label) => label !== data.association.labelId); labels = labels.filter((label) => label !== data.association.labelId);
} else if (data.type === 'add') { } else if (data.type === 'add') {

View File

@ -5,7 +5,15 @@ import { Db } from 'mongodb';
import { Collection } from 'mongoose'; import { Collection } from 'mongoose';
import { join } from 'path'; import { join } from 'path';
import { Auth, CacheConf, ConfigService, Database, DelInstance, HttpServer } from '../../config/env.config'; import {
Auth,
CacheConf,
ConfigService,
Database,
DelInstance,
HttpServer,
ProviderSession,
} from '../../config/env.config';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config'; import { INSTANCE_DIR, STORE_DIR } from '../../config/path.config';
import { NotFoundException } from '../../exceptions'; import { NotFoundException } from '../../exceptions';
@ -60,6 +68,8 @@ export class WAMonitoringService {
private readonly logger = new Logger(WAMonitoringService.name); private readonly logger = new Logger(WAMonitoringService.name);
public readonly waInstances: Record<string, BaileysStartupService | BusinessStartupService> = {}; public readonly waInstances: Record<string, BaileysStartupService | BusinessStartupService> = {};
private readonly providerSession = Object.freeze(this.configService.get<ProviderSession>('PROVIDER'));
public delInstanceTime(instance: string) { public delInstanceTime(instance: string) {
const time = this.configService.get<DelInstance>('DEL_INSTANCE'); const time = this.configService.get<DelInstance>('DEL_INSTANCE');
if (typeof time === 'number' && time > 0) { if (typeof time === 'number' && time > 0) {
@ -259,13 +269,21 @@ export class WAMonitoringService {
} }
this.logger.verbose('cleaning up instance in files: ' + instanceName); this.logger.verbose('cleaning up instance in files: ' + instanceName);
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true }); if (this.providerSession?.ENABLED) {
await this.providerFiles.removeSession(instanceName);
} else {
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
}
} }
public async cleaningStoreFiles(instanceName: string) { public async cleaningStoreFiles(instanceName: string) {
if (!this.db.ENABLED) { if (!this.db.ENABLED) {
this.logger.verbose('cleaning store files instance: ' + instanceName); this.logger.verbose('cleaning store files instance: ' + instanceName);
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true }); if (this.providerSession?.ENABLED) {
await this.providerFiles.removeSession(instanceName);
} else {
rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true });
}
execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`); execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`);
execSync(`rm -rf ${join(STORE_DIR, 'contacts', instanceName)}`); execSync(`rm -rf ${join(STORE_DIR, 'contacts', instanceName)}`);
@ -307,7 +325,9 @@ export class WAMonitoringService {
this.logger.verbose('Loading instances'); this.logger.verbose('Loading instances');
try { try {
if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) { if (this.providerSession.ENABLED) {
await this.loadInstancesFromProvider();
} else if (this.redis.REDIS.ENABLED && this.redis.REDIS.SAVE_INSTANCES) {
await this.loadInstancesFromRedis(); await this.loadInstancesFromRedis();
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) { } else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
await this.loadInstancesFromDatabase(); await this.loadInstancesFromDatabase();
@ -405,6 +425,18 @@ export class WAMonitoringService {
} }
} }
private async loadInstancesFromProvider() {
this.logger.verbose('Provider in files enabled');
const [instances] = await this.providerFiles.allInstances();
if (!instances?.data) {
this.logger.verbose('No instances found');
return;
}
await Promise.all(instances?.data?.map(async (instanceName: string) => this.setInstance(instanceName)));
}
private async loadInstancesFromFiles() { private async loadInstancesFromFiles() {
this.logger.verbose('Store in files enabled'); this.logger.verbose('Store in files enabled');
const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' }); const dir = opendirSync(INSTANCE_DIR, { encoding: 'utf-8' });

View File

@ -1,9 +1,9 @@
/** /**
* *
* @author jrCleber * @author jrCleber
* @filename use-multi-file-auth-state-redis-db.ts * @filename use-multi-file-auth-state-provider-files.ts
* Developed by: Cleber Wilson * Developed by: Cleber Wilson
* Creation date: Apr 09, 2023 * Creation date: May 31, 2024
* Contact: contato@codechat.dev * Contact: contato@codechat.dev
* *
* @copyright © Cleber Wilson 2023. All rights reserved. * @copyright © Cleber Wilson 2023. All rights reserved.
@ -45,13 +45,12 @@ import {
import { isNotEmpty } from 'class-validator'; import { isNotEmpty } from 'class-validator';
import { ProviderFiles } from '../api/provider/sessions'; import { ProviderFiles } from '../api/provider/sessions';
import { ConfigService } from '../config/env.config';
import { Logger } from '../config/logger.config'; import { Logger } from '../config/logger.config';
export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> }; export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> };
export class AuthStateProvider { export class AuthStateProvider {
constructor(private readonly configService: ConfigService, private readonly providerFiles: ProviderFiles) {} constructor(private readonly providerFiles: ProviderFiles) {}
private readonly logger = new Logger(AuthStateProvider.name); private readonly logger = new Logger(AuthStateProvider.name);
@ -68,7 +67,7 @@ export class AuthStateProvider {
data: json, data: json,
}); });
if (error) { if (error) {
this.logger.error([error?.message, error?.stack]); this.logger.error(['writeData', error?.message, error?.stack]);
return; return;
} }
return response; return response;
@ -77,7 +76,7 @@ export class AuthStateProvider {
const readData = async (key: string): Promise<any> => { const readData = async (key: string): Promise<any> => {
const [response, error] = await this.providerFiles.read(instance, key); const [response, error] = await this.providerFiles.read(instance, key);
if (error) { if (error) {
this.logger.error([error?.message, error?.stack]); this.logger.error(['readData', error?.message, error?.stack]);
return; return;
} }
if (isNotEmpty(response?.data)) { if (isNotEmpty(response?.data)) {
@ -88,7 +87,7 @@ export class AuthStateProvider {
const removeData = async (key: string) => { const removeData = async (key: string) => {
const [response, error] = await this.providerFiles.delete(instance, key); const [response, error] = await this.providerFiles.delete(instance, key);
if (error) { if (error) {
this.logger.error([error?.message, error?.stack]); this.logger.error(['removeData', error?.message, error?.stack]);
return; return;
} }