diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 9c4f358c..bc3ace61 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -43,7 +43,7 @@ export class InstanceController { private readonly proxyService: ProxyController, private readonly cache: CacheService, private readonly chatwootCache: CacheService, - private readonly messagesLostCache: CacheService, + private readonly baileysCache: CacheService, private readonly providerFiles: ProviderFiles, ) {} @@ -112,7 +112,7 @@ export class InstanceController { this.repository, this.cache, this.chatwootCache, - this.messagesLostCache, + this.baileysCache, this.providerFiles, ); } else { @@ -122,7 +122,7 @@ export class InstanceController { this.repository, this.cache, this.chatwootCache, - this.messagesLostCache, + this.baileysCache, this.providerFiles, ); } diff --git a/src/api/server.module.ts b/src/api/server.module.ts index e282be1c..9b469ea7 100644 --- a/src/api/server.module.ts +++ b/src/api/server.module.ts @@ -109,7 +109,7 @@ export const repository = new RepositoryBroker( export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine()); const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine()); -const messagesLostCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine()); +const baileysCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine()); const providerFiles = new ProviderFiles(configService); export const waMonitor = new WAMonitoringService( @@ -118,7 +118,7 @@ export const waMonitor = new WAMonitoringService( repository, cache, chatwootCache, - messagesLostCache, + baileysCache, providerFiles, ); @@ -170,7 +170,7 @@ export const instanceController = new InstanceController( proxyController, cache, chatwootCache, - messagesLostCache, + baileysCache, providerFiles, ); export const sendMessageController = new SendMessageController(waMonitor); diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 4dafe2aa..b61be675 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -137,7 +137,7 @@ export class BaileysStartupService extends ChannelStartupService { public readonly repository: RepositoryBroker, public readonly cache: CacheService, public readonly chatwootCache: CacheService, - public readonly messagesLostCache: CacheService, + public readonly baileysCache: CacheService, private readonly providerFiles: ProviderFiles, ) { super(configService, eventEmitter, repository, chatwootCache); @@ -146,6 +146,8 @@ export class BaileysStartupService extends ChannelStartupService { this.instance.qrcode = { count: 0 }; this.mobile = false; this.recoveringMessages(); + this.forceUpdateGroupMetadataCache(); + this.authStateProvider = new AuthStateProvider(this.providerFiles); } @@ -166,9 +168,9 @@ export class BaileysStartupService extends ChannelStartupService { if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) { setInterval(async () => { - this.messagesLostCache.keys().then((keys) => { + this.baileysCache.keys().then((keys) => { keys.forEach(async (key) => { - const message = await this.messagesLostCache.get(key.split(':')[2]); + const message = await this.baileysCache.get(key.split(':')[2]); if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') { this.logger.info('Message absent from node, retrying to send, key: ' + key.split(':')[2]); @@ -180,6 +182,17 @@ export class BaileysStartupService extends ChannelStartupService { } } + private async forceUpdateGroupMetadataCache() { + setInterval(async () => { + this.logger.verbose('Forcing update group metadata cache'); + const groups = await this.fetchAllGroups({ getParticipants: 'false' }); + + for (const group of groups) { + await this.updateGroupMetadataCache(group.id); + } + }, 60000); + } + public get connectionStatus() { this.logger.verbose('Getting connection status'); return this.stateConnection; @@ -1124,15 +1137,15 @@ export class BaileysStartupService extends ChannelStartupService { if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') { this.logger.info('Recovering message lost'); - await this.messagesLostCache.set(received.key.id, received); + await this.baileysCache.set(received.key.id, received); continue; } - const retryCache = (await this.messagesLostCache.get(received.key.id)) || null; + const retryCache = (await this.baileysCache.get(received.key.id)) || null; if (retryCache) { this.logger.info('Recovered message lost'); - await this.messagesLostCache.delete(received.key.id); + await this.baileysCache.delete(received.key.id); } if ( @@ -1421,6 +1434,12 @@ export class BaileysStartupService extends ChannelStartupService { this.logger.verbose('Sending data to webhook in event GROUPS_UPDATE'); this.sendDataWebhook(Events.GROUPS_UPDATE, groupMetadataUpdate); + + groupMetadataUpdate.forEach((group) => { + if (isJidGroup(group.id)) { + this.updateGroupMetadataCache(group.id); + } + }); }, 'group-participants.update': (participantsUpdate: { @@ -1857,7 +1876,8 @@ export class BaileysStartupService extends ChannelStartupService { let mentions: string[]; if (isJidGroup(sender)) { try { - const group = await this.findGroup({ groupJid: sender }, 'inner'); + // const group = await this.findGroup({ groupJid: sender }, 'inner'); + const group = await this.getGroupMetadataCache(sender); if (!group) { throw new NotFoundException('Group not found'); @@ -1910,7 +1930,10 @@ export class BaileysStartupService extends ChannelStartupService { key: message['reactionMessage']['key'], }, } as unknown as AnyMessageContent, - option as unknown as MiscMessageGenerationOptions, + { + ...option, + cachedGroupMetadata: this.getGroupMetadataCache, + } as unknown as MiscMessageGenerationOptions, ); } } @@ -1923,7 +1946,10 @@ export class BaileysStartupService extends ChannelStartupService { mentions, linkPreview: linkPreview, } as unknown as AnyMessageContent, - option as unknown as MiscMessageGenerationOptions, + { + ...option, + cachedGroupMetadata: this.getGroupMetadataCache, + } as unknown as MiscMessageGenerationOptions, ); } @@ -1938,7 +1964,10 @@ export class BaileysStartupService extends ChannelStartupService { }, mentions, }, - option as unknown as MiscMessageGenerationOptions, + { + ...option, + cachedGroupMetadata: this.getGroupMetadataCache, + } as unknown as MiscMessageGenerationOptions, ); } @@ -1959,7 +1988,10 @@ export class BaileysStartupService extends ChannelStartupService { return await this.client.sendMessage( sender, message as unknown as AnyMessageContent, - option as unknown as MiscMessageGenerationOptions, + { + ...option, + cachedGroupMetadata: this.getGroupMetadataCache, + } as unknown as MiscMessageGenerationOptions, ); })(); @@ -3164,6 +3196,40 @@ export class BaileysStartupService extends ChannelStartupService { } // Group + private async updateGroupMetadataCache(groupJid: string) { + try { + const meta = await this.client.groupMetadata(groupJid); + console.log('updateGroupMetadataCache', groupJid); + await this.baileysCache.set(`group-metadata-${groupJid}`, { + timestamp: Date.now(), + data: meta, + }); + + return meta; + } catch (error) { + this.logger.error(error); + } + } + + private async getGroupMetadataCache(groupJid: string) { + if (!isJidGroup(groupJid)) return null; + + console.log('getGroupMetadataCache', groupJid); + if (this.baileysCache.has(`group-metadata-${groupJid}`)) { + console.log('has cache'); + const meta = await this.baileysCache.get(`group-metadata-${groupJid}`); + + if (Date.now() - meta.timestamp > 60000) { + await this.updateGroupMetadataCache(groupJid); + } + + console.log('meta.data', meta.data); + return meta.data; + } + + return await this.updateGroupMetadataCache(groupJid); + } + public async createGroup(create: CreateGroupDto) { this.logger.verbose('Creating group: ' + create.subject); try { diff --git a/src/api/services/channels/whatsapp.business.service.ts b/src/api/services/channels/whatsapp.business.service.ts index e44f36c7..86178659 100644 --- a/src/api/services/channels/whatsapp.business.service.ts +++ b/src/api/services/channels/whatsapp.business.service.ts @@ -36,7 +36,7 @@ export class BusinessStartupService extends ChannelStartupService { public readonly repository: RepositoryBroker, public readonly cache: CacheService, public readonly chatwootCache: CacheService, - public readonly messagesLostCache: CacheService, + public readonly baileysCache: CacheService, private readonly providerFiles: ProviderFiles, ) { super(configService, eventEmitter, repository, chatwootCache); diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index c342e977..101b005e 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -44,7 +44,7 @@ export class WAMonitoringService { private readonly repository: RepositoryBroker, private readonly cache: CacheService, private readonly chatwootCache: CacheService, - private readonly messagesLostCache: CacheService, + private readonly baileysCache: CacheService, private readonly providerFiles: ProviderFiles, ) { this.logger.verbose('instance created'); @@ -368,7 +368,7 @@ export class WAMonitoringService { this.repository, this.cache, this.chatwootCache, - this.messagesLostCache, + this.baileysCache, this.providerFiles, ); @@ -380,7 +380,7 @@ export class WAMonitoringService { this.repository, this.cache, this.chatwootCache, - this.messagesLostCache, + this.baileysCache, this.providerFiles, );