cacheGroupMetadata

This commit is contained in:
Davidson Gomes 2024-06-05 21:48:37 -03:00
parent 9633412ef6
commit afcf6eab71

View File

@ -187,6 +187,12 @@ export class BaileysStartupService extends ChannelStartupService {
} }
private async forceUpdateGroupMetadataCache() { private async forceUpdateGroupMetadataCache() {
if (
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
)
return;
setInterval(async () => { setInterval(async () => {
this.logger.verbose('Forcing update group metadata cache'); this.logger.verbose('Forcing update group metadata cache');
const groups = await this.fetchAllGroups({ getParticipants: 'false' }); const groups = await this.fetchAllGroups({ getParticipants: 'false' });
@ -1874,8 +1880,11 @@ export class BaileysStartupService extends ChannelStartupService {
let mentions: string[]; let mentions: string[];
if (isJidGroup(sender)) { if (isJidGroup(sender)) {
try { try {
// const group = await this.findGroup({ groupJid: sender }, 'inner'); let group;
const group = await this.getGroupMetadataCache(sender);
const cache = this.configService.get<CacheConf>('CACHE');
if (!cache.REDIS.ENABLED && !cache.LOCAL.ENABLED) group = await this.findGroup({ groupJid: sender }, 'inner');
else group = await this.getGroupMetadataCache(sender);
if (!group) { if (!group) {
throw new NotFoundException('Group not found'); throw new NotFoundException('Group not found');
@ -1930,7 +1939,11 @@ export class BaileysStartupService extends ChannelStartupService {
} as unknown as AnyMessageContent, } as unknown as AnyMessageContent,
{ {
...option, ...option,
cachedGroupMetadata: this.getGroupMetadataCache, cachedGroupMetadata:
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
? null
: this.getGroupMetadataCache,
} as unknown as MiscMessageGenerationOptions, } as unknown as MiscMessageGenerationOptions,
); );
} }
@ -1946,7 +1959,11 @@ export class BaileysStartupService extends ChannelStartupService {
} as unknown as AnyMessageContent, } as unknown as AnyMessageContent,
{ {
...option, ...option,
cachedGroupMetadata: this.getGroupMetadataCache, cachedGroupMetadata:
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
? null
: this.getGroupMetadataCache,
} as unknown as MiscMessageGenerationOptions, } as unknown as MiscMessageGenerationOptions,
); );
} }
@ -1964,7 +1981,11 @@ export class BaileysStartupService extends ChannelStartupService {
}, },
{ {
...option, ...option,
cachedGroupMetadata: this.getGroupMetadataCache, cachedGroupMetadata:
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
? null
: this.getGroupMetadataCache,
} as unknown as MiscMessageGenerationOptions, } as unknown as MiscMessageGenerationOptions,
); );
} }
@ -1988,7 +2009,11 @@ export class BaileysStartupService extends ChannelStartupService {
message as unknown as AnyMessageContent, message as unknown as AnyMessageContent,
{ {
...option, ...option,
cachedGroupMetadata: this.getGroupMetadataCache, cachedGroupMetadata:
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
? null
: this.getGroupMetadataCache,
} as unknown as MiscMessageGenerationOptions, } as unknown as MiscMessageGenerationOptions,
); );
})(); })();
@ -3205,6 +3230,7 @@ export class BaileysStartupService extends ChannelStartupService {
return meta; return meta;
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
return null;
} }
} }
@ -3212,6 +3238,7 @@ export class BaileysStartupService extends ChannelStartupService {
if (!isJidGroup(groupJid)) return null; if (!isJidGroup(groupJid)) return null;
if (await groupMetadataCache.has(groupJid)) { if (await groupMetadataCache.has(groupJid)) {
console.log('Has cache for group: ' + groupJid);
const meta = await groupMetadataCache.get(groupJid); const meta = await groupMetadataCache.get(groupJid);
if (Date.now() - meta.timestamp > 3600000) { if (Date.now() - meta.timestamp > 3600000) {