Skip to content

Commit afcf6ea

Browse files
committed
cacheGroupMetadata
1 parent 9633412 commit afcf6ea

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/api/services/channels/whatsapp.baileys.service.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ export class BaileysStartupService extends ChannelStartupService {
187187
}
188188

189189
private async forceUpdateGroupMetadataCache() {
190+
if (
191+
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
192+
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
193+
)
194+
return;
195+
190196
setInterval(async () => {
191197
this.logger.verbose('Forcing update group metadata cache');
192198
const groups = await this.fetchAllGroups({ getParticipants: 'false' });
@@ -1874,8 +1880,11 @@ export class BaileysStartupService extends ChannelStartupService {
18741880
let mentions: string[];
18751881
if (isJidGroup(sender)) {
18761882
try {
1877-
// const group = await this.findGroup({ groupJid: sender }, 'inner');
1878-
const group = await this.getGroupMetadataCache(sender);
1883+
let group;
1884+
1885+
const cache = this.configService.get<CacheConf>('CACHE');
1886+
if (!cache.REDIS.ENABLED && !cache.LOCAL.ENABLED) group = await this.findGroup({ groupJid: sender }, 'inner');
1887+
else group = await this.getGroupMetadataCache(sender);
18791888

18801889
if (!group) {
18811890
throw new NotFoundException('Group not found');
@@ -1930,7 +1939,11 @@ export class BaileysStartupService extends ChannelStartupService {
19301939
} as unknown as AnyMessageContent,
19311940
{
19321941
...option,
1933-
cachedGroupMetadata: this.getGroupMetadataCache,
1942+
cachedGroupMetadata:
1943+
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
1944+
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
1945+
? null
1946+
: this.getGroupMetadataCache,
19341947
} as unknown as MiscMessageGenerationOptions,
19351948
);
19361949
}
@@ -1946,7 +1959,11 @@ export class BaileysStartupService extends ChannelStartupService {
19461959
} as unknown as AnyMessageContent,
19471960
{
19481961
...option,
1949-
cachedGroupMetadata: this.getGroupMetadataCache,
1962+
cachedGroupMetadata:
1963+
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
1964+
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
1965+
? null
1966+
: this.getGroupMetadataCache,
19501967
} as unknown as MiscMessageGenerationOptions,
19511968
);
19521969
}
@@ -1964,7 +1981,11 @@ export class BaileysStartupService extends ChannelStartupService {
19641981
},
19651982
{
19661983
...option,
1967-
cachedGroupMetadata: this.getGroupMetadataCache,
1984+
cachedGroupMetadata:
1985+
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
1986+
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
1987+
? null
1988+
: this.getGroupMetadataCache,
19681989
} as unknown as MiscMessageGenerationOptions,
19691990
);
19701991
}
@@ -1988,7 +2009,11 @@ export class BaileysStartupService extends ChannelStartupService {
19882009
message as unknown as AnyMessageContent,
19892010
{
19902011
...option,
1991-
cachedGroupMetadata: this.getGroupMetadataCache,
2012+
cachedGroupMetadata:
2013+
!this.configService.get<CacheConf>('CACHE').REDIS.ENABLED &&
2014+
!this.configService.get<CacheConf>('CACHE').LOCAL.ENABLED
2015+
? null
2016+
: this.getGroupMetadataCache,
19922017
} as unknown as MiscMessageGenerationOptions,
19932018
);
19942019
})();
@@ -3205,13 +3230,15 @@ export class BaileysStartupService extends ChannelStartupService {
32053230
return meta;
32063231
} catch (error) {
32073232
this.logger.error(error);
3233+
return null;
32083234
}
32093235
}
32103236

32113237
private async getGroupMetadataCache(groupJid: string) {
32123238
if (!isJidGroup(groupJid)) return null;
32133239

32143240
if (await groupMetadataCache.has(groupJid)) {
3241+
console.log('Has cache for group: ' + groupJid);
32153242
const meta = await groupMetadataCache.get(groupJid);
32163243

32173244
if (Date.now() - meta.timestamp > 3600000) {

0 commit comments

Comments
 (0)