Skip to content

Commit fca39a2

Browse files
Merge pull request #2191 from JefersonRamos/bugfix/waiting-for-message
Durante o processo de logout de uma instância, as chaves associadas a…
2 parents 1e3a235 + feff038 commit fca39a2

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ export class BaileysStartupService extends ChannelStartupService {
266266

267267
this.client?.ws?.close();
268268

269+
const db = this.configService.get<Database>('DATABASE');
270+
const cache = this.configService.get<CacheConf>('CACHE');
271+
const provider = this.configService.get<ProviderSession>('PROVIDER');
272+
273+
if (provider?.ENABLED) {
274+
const authState = await this.authStateProvider.authStateProvider(this.instance.id);
275+
276+
await authState.removeCreds();
277+
}
278+
279+
if (cache?.REDIS.ENABLED && cache?.REDIS.SAVE_INSTANCES) {
280+
const authState = await useMultiFileAuthStateRedisDb(this.instance.id, this.cache);
281+
282+
await authState.removeCreds();
283+
}
284+
285+
if (db.SAVE_DATA.INSTANCE) {
286+
const authState = await useMultiFileAuthStatePrisma(this.instance.id, this.cache);
287+
288+
await authState.removeCreds();
289+
}
290+
269291
const sessionExists = await this.prismaRepository.session.findFirst({ where: { sessionId: this.instanceId } });
270292
if (sessionExists) {
271293
await this.prismaRepository.session.delete({ where: { sessionId: this.instanceId } });

src/utils/use-multi-file-auth-state-prisma.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { prismaRepository } from '@api/server.module';
22
import { CacheService } from '@api/services/cache.service';
33
import { CacheConf, configService } from '@config/env.config';
4+
import { Logger } from '@config/logger.config';
45
import { INSTANCE_DIR } from '@config/path.config';
56
import { AuthenticationState, BufferJSON, initAuthCreds, WAProto as proto } from 'baileys';
67
import fs from 'fs/promises';
@@ -73,12 +74,15 @@ async function fileExists(file: string): Promise<any> {
7374
}
7475
}
7576

77+
const logger = new Logger('useMultiFileAuthStatePrisma');
78+
7679
export default async function useMultiFileAuthStatePrisma(
7780
sessionId: string,
7881
cache: CacheService,
7982
): Promise<{
8083
state: AuthenticationState;
8184
saveCreds: () => Promise<void>;
85+
removeCreds: () => Promise<void>;
8286
}> {
8387
const localFolder = path.join(INSTANCE_DIR, sessionId);
8488
const localFile = (key: string) => path.join(localFolder, fixFileName(key) + '.json');
@@ -142,6 +146,26 @@ export default async function useMultiFileAuthStatePrisma(
142146
}
143147
}
144148

149+
async function removeCreds(): Promise<any> {
150+
const cacheConfig = configService.get<CacheConf>('CACHE');
151+
152+
// Redis
153+
try {
154+
if (cacheConfig.REDIS.ENABLED) {
155+
await cache.delete(sessionId);
156+
logger.info({ action: 'redis.delete', sessionId });
157+
158+
return;
159+
}
160+
} catch (err) {
161+
logger.warn({ action: 'redis.delete', sessionId, err });
162+
}
163+
164+
logger.info({ action: 'auth.key.delete', sessionId });
165+
166+
await deleteAuthKey(sessionId);
167+
}
168+
145169
let creds = await readData('creds');
146170
if (!creds) {
147171
creds = initAuthCreds();
@@ -183,5 +207,7 @@ export default async function useMultiFileAuthStatePrisma(
183207
saveCreds: () => {
184208
return writeData(creds, 'creds');
185209
},
210+
211+
removeCreds,
186212
};
187213
}

src/utils/use-multi-file-auth-state-provider-files.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ import { Logger } from '@config/logger.config';
3939
import { AuthenticationCreds, AuthenticationState, BufferJSON, initAuthCreds, proto, SignalDataTypeMap } from 'baileys';
4040
import { isNotEmpty } from 'class-validator';
4141

42-
export type AuthState = { state: AuthenticationState; saveCreds: () => Promise<void> };
42+
export type AuthState = {
43+
state: AuthenticationState;
44+
saveCreds: () => Promise<void>;
45+
removeCreds: () => Promise<void>;
46+
};
4347

4448
export class AuthStateProvider {
4549
constructor(private readonly providerFiles: ProviderFiles) {}
@@ -86,6 +90,18 @@ export class AuthStateProvider {
8690
return response;
8791
};
8892

93+
const removeCreds = async () => {
94+
const [response, error] = await this.providerFiles.removeSession(instance);
95+
if (error) {
96+
// this.logger.error(['removeData', error?.message, error?.stack]);
97+
return;
98+
}
99+
100+
logger.info({ action: 'remove.session', instance, response });
101+
102+
return;
103+
};
104+
89105
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
90106

91107
return {
@@ -126,6 +142,10 @@ export class AuthStateProvider {
126142
saveCreds: async () => {
127143
return await writeData(creds, 'creds');
128144
},
145+
146+
removeCreds,
129147
};
130148
}
131149
}
150+
151+
const logger = new Logger('useMultiFileAuthStatePrisma');

src/utils/use-multi-file-auth-state-redis-db.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export async function useMultiFileAuthStateRedisDb(
88
): Promise<{
99
state: AuthenticationState;
1010
saveCreds: () => Promise<void>;
11+
removeCreds: () => Promise<void>;
1112
}> {
1213
const logger = new Logger('useMultiFileAuthStateRedisDb');
1314

@@ -36,6 +37,16 @@ export async function useMultiFileAuthStateRedisDb(
3637
}
3738
};
3839

40+
async function removeCreds(): Promise<any> {
41+
try {
42+
logger.warn({ action: 'redis.delete', instanceName });
43+
44+
return await cache.delete(instanceName);
45+
} catch {
46+
return;
47+
}
48+
}
49+
3950
const creds: AuthenticationCreds = (await readData('creds')) || initAuthCreds();
4051

4152
return {
@@ -76,5 +87,7 @@ export async function useMultiFileAuthStateRedisDb(
7687
saveCreds: async () => {
7788
return await writeData(creds, 'creds');
7889
},
90+
91+
removeCreds,
7992
};
8093
}

0 commit comments

Comments
 (0)