Merge pull request #2191 from JefersonRamos/bugfix/waiting-for-message

Durante o processo de logout de uma instância, as chaves associadas a…
This commit is contained in:
Davidson Gomes 2025-11-07 14:38:36 -03:00 committed by GitHub
commit fca39a2b34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 82 additions and 1 deletions

View File

@ -266,6 +266,28 @@ export class BaileysStartupService extends ChannelStartupService {
this.client?.ws?.close();
const db = this.configService.get<Database>('DATABASE');
const cache = this.configService.get<CacheConf>('CACHE');
const provider = this.configService.get<ProviderSession>('PROVIDER');
if (provider?.ENABLED) {
const authState = await this.authStateProvider.authStateProvider(this.instance.id);
await authState.removeCreds();
}
if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) {
const authState = await useMultiFileAuthStateRedisDb(this.instance.id, this.cache);
await authState.removeCreds();
}
if (db.SAVE_DATA.INSTANCE) {
const authState = await useMultiFileAuthStatePrisma(this.instance.id, this.cache);
await authState.removeCreds();
}
const sessionExists = await this.prismaRepository.session.findFirst({ where: { sessionId: this.instanceId } });
if (sessionExists) {
await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } });

View File

@ -1,6 +1,7 @@
import { prismaRepository } from '@api/server.module';
import { CacheService } from '@api/services/cache.service';
import { CacheConf, configService } from '@config/env.config';
import { Logger } from '@config/logger.config';
import { INSTANCE_DIR } from '@config/path.config';
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
import fs from 'fs/promises';
@ -73,12 +74,15 @@ async function fileExists(file: string): Promise<any> {
}
}
const logger = new Logger('useMultiFileAuthStatePrisma');
export default async function useMultiFileAuthStatePrisma(
sessionId: string,
cache: CacheService,
): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
}> {
const localFolder = path.join(INSTANCE_DIR, sessionId);
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
@ -142,6 +146,26 @@ export default async function useMultiFileAuthStatePrisma(
}
}
async function removeCreds(): Promise<any> {
const cacheConfig = configService.get<CacheConf>('CACHE');
// Redis
try {
if (cacheConfig.REDIS.ENABLED) {
await cache.delete(sessionId);
logger.info({ action: 'redis.delete', sessionId });
return;
}
} catch (err) {
logger.warn({ action: 'redis.delete', sessionId, err });
}
logger.info({ action: 'auth.key.delete', sessionId });
await deleteAuthKey(sessionId);
}
let creds = await readData('creds');
if (!creds) {
creds = initAuthCreds();
@ -183,5 +207,7 @@ export default async function useMultiFileAuthStatePrisma(
saveCreds: () => {
return writeData(creds, 'creds');
},
removeCreds,
};
}

View File

@ -39,7 +39,11 @@ import { Logger } from '@config/logger.config';
import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys';
import { isNotEmpty } from 'class-validator';
export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> };
export type AuthState = {
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
};
export class AuthStateProvider {
constructor(private readonly providerFiles: ProviderFiles) {}
@ -86,6 +90,18 @@ export class AuthStateProvider {
return response;
};
const removeCreds = async () => {
const [response, error] = await this.providerFiles.removeSession(instance);
if (error) {
// this.logger.error(['removeData', error?.message, error?.stack]);
return;
}
logger.info({ action: 'remove.session', instance, response });
return;
};
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
return {
@ -126,6 +142,10 @@ export class AuthStateProvider {
saveCreds: async () => {
return await writeData(creds, 'creds');
},
removeCreds,
};
}
}
const logger = new Logger('useMultiFileAuthStatePrisma');

View File

@ -8,6 +8,7 @@ export async function useMultiFileAuthStateRedisDb(
): Promise<{
state: AuthenticationState;
saveCreds: () => Promise<void>;
removeCreds: () => Promise<void>;
}> {
const logger = new Logger('useMultiFileAuthStateRedisDb');
@ -36,6 +37,16 @@ export async function useMultiFileAuthStateRedisDb(
}
};
async function removeCreds(): Promise<any> {
try {
logger.warn({ action: 'redis.delete', instanceName });
return await cache.delete(instanceName);
} catch {
return;
}
}
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
return {
@ -76,5 +87,7 @@ export async function useMultiFileAuthStateRedisDb(
saveCreds: async () => {
return await writeData(creds, 'creds');
},
removeCreds,
};
}