Skip to content

Commit 8034e7f

Browse files
committed
cacheGroupMetadata
1 parent 52230ed commit 8034e7f

File tree

5 files changed

+87
-21
lines changed

5 files changed

+87
-21
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class InstanceController {
4343
private readonly proxyService: ProxyController,
4444
private readonly cache: CacheService,
4545
private readonly chatwootCache: CacheService,
46-
private readonly messagesLostCache: CacheService,
46+
private readonly baileysCache: CacheService,
4747
private readonly providerFiles: ProviderFiles,
4848
) {}
4949

@@ -112,7 +112,7 @@ export class InstanceController {
112112
this.repository,
113113
this.cache,
114114
this.chatwootCache,
115-
this.messagesLostCache,
115+
this.baileysCache,
116116
this.providerFiles,
117117
);
118118
} else {
@@ -122,7 +122,7 @@ export class InstanceController {
122122
this.repository,
123123
this.cache,
124124
this.chatwootCache,
125-
this.messagesLostCache,
125+
this.baileysCache,
126126
this.providerFiles,
127127
);
128128
}

src/api/server.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export const repository = new RepositoryBroker(
109109

110110
export const cache = new CacheService(new CacheEngine(configService, 'instance').getEngine());
111111
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
112-
const messagesLostCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
112+
const baileysCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
113113
const providerFiles = new ProviderFiles(configService);
114114

115115
export const waMonitor = new WAMonitoringService(
@@ -118,7 +118,7 @@ export const waMonitor = new WAMonitoringService(
118118
repository,
119119
cache,
120120
chatwootCache,
121-
messagesLostCache,
121+
baileysCache,
122122
providerFiles,
123123
);
124124

@@ -170,7 +170,7 @@ export const instanceController = new InstanceController(
170170
proxyController,
171171
cache,
172172
chatwootCache,
173-
messagesLostCache,
173+
baileysCache,
174174
providerFiles,
175175
);
176176
export const sendMessageController = new SendMessageController(waMonitor);

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

Lines changed: 77 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class BaileysStartupService extends ChannelStartupService {
137137
public readonly repository: RepositoryBroker,
138138
public readonly cache: CacheService,
139139
public readonly chatwootCache: CacheService,
140-
public readonly messagesLostCache: CacheService,
140+
public readonly baileysCache: CacheService,
141141
private readonly providerFiles: ProviderFiles,
142142
) {
143143
super(configService, eventEmitter, repository, chatwootCache);
@@ -146,6 +146,8 @@ export class BaileysStartupService extends ChannelStartupService {
146146
this.instance.qrcode = { count: 0 };
147147
this.mobile = false;
148148
this.recoveringMessages();
149+
this.forceUpdateGroupMetadataCache();
150+
149151
this.authStateProvider = new AuthStateProvider(this.providerFiles);
150152
}
151153

@@ -166,9 +168,9 @@ export class BaileysStartupService extends ChannelStartupService {
166168

167169
if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) {
168170
setInterval(async () => {
169-
this.messagesLostCache.keys().then((keys) => {
171+
this.baileysCache.keys().then((keys) => {
170172
keys.forEach(async (key) => {
171-
const message = await this.messagesLostCache.get(key.split(':')[2]);
173+
const message = await this.baileysCache.get(key.split(':')[2]);
172174

173175
if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') {
174176
this.logger.info('Message absent from node, retrying to send, key: ' + key.split(':')[2]);
@@ -180,6 +182,17 @@ export class BaileysStartupService extends ChannelStartupService {
180182
}
181183
}
182184

185+
private async forceUpdateGroupMetadataCache() {
186+
setInterval(async () => {
187+
this.logger.verbose('Forcing update group metadata cache');
188+
const groups = await this.fetchAllGroups({ getParticipants: 'false' });
189+
190+
for (const group of groups) {
191+
await this.updateGroupMetadataCache(group.id);
192+
}
193+
}, 60000);
194+
}
195+
183196
public get connectionStatus() {
184197
this.logger.verbose('Getting connection status');
185198
return this.stateConnection;
@@ -1124,15 +1137,15 @@ export class BaileysStartupService extends ChannelStartupService {
11241137
if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') {
11251138
this.logger.info('Recovering message lost');
11261139

1127-
await this.messagesLostCache.set(received.key.id, received);
1140+
await this.baileysCache.set(received.key.id, received);
11281141
continue;
11291142
}
11301143

1131-
const retryCache = (await this.messagesLostCache.get(received.key.id)) || null;
1144+
const retryCache = (await this.baileysCache.get(received.key.id)) || null;
11321145

11331146
if (retryCache) {
11341147
this.logger.info('Recovered message lost');
1135-
await this.messagesLostCache.delete(received.key.id);
1148+
await this.baileysCache.delete(received.key.id);
11361149
}
11371150

11381151
if (
@@ -1421,6 +1434,12 @@ export class BaileysStartupService extends ChannelStartupService {
14211434

14221435
this.logger.verbose('Sending data to webhook in event GROUPS_UPDATE');
14231436
this.sendDataWebhook(Events.GROUPS_UPDATE, groupMetadataUpdate);
1437+
1438+
groupMetadataUpdate.forEach((group) => {
1439+
if (isJidGroup(group.id)) {
1440+
this.updateGroupMetadataCache(group.id);
1441+
}
1442+
});
14241443
},
14251444

14261445
'group-participants.update': (participantsUpdate: {
@@ -1857,7 +1876,8 @@ export class BaileysStartupService extends ChannelStartupService {
18571876
let mentions: string[];
18581877
if (isJidGroup(sender)) {
18591878
try {
1860-
const group = await this.findGroup({ groupJid: sender }, 'inner');
1879+
// const group = await this.findGroup({ groupJid: sender }, 'inner');
1880+
const group = await this.getGroupMetadataCache(sender);
18611881

18621882
if (!group) {
18631883
throw new NotFoundException('Group not found');
@@ -1910,7 +1930,10 @@ export class BaileysStartupService extends ChannelStartupService {
19101930
key: message['reactionMessage']['key'],
19111931
},
19121932
} as unknown as AnyMessageContent,
1913-
option as unknown as MiscMessageGenerationOptions,
1933+
{
1934+
...option,
1935+
cachedGroupMetadata: this.getGroupMetadataCache,
1936+
} as unknown as MiscMessageGenerationOptions,
19141937
);
19151938
}
19161939
}
@@ -1923,7 +1946,10 @@ export class BaileysStartupService extends ChannelStartupService {
19231946
mentions,
19241947
linkPreview: linkPreview,
19251948
} as unknown as AnyMessageContent,
1926-
option as unknown as MiscMessageGenerationOptions,
1949+
{
1950+
...option,
1951+
cachedGroupMetadata: this.getGroupMetadataCache,
1952+
} as unknown as MiscMessageGenerationOptions,
19271953
);
19281954
}
19291955

@@ -1938,7 +1964,10 @@ export class BaileysStartupService extends ChannelStartupService {
19381964
},
19391965
mentions,
19401966
},
1941-
option as unknown as MiscMessageGenerationOptions,
1967+
{
1968+
...option,
1969+
cachedGroupMetadata: this.getGroupMetadataCache,
1970+
} as unknown as MiscMessageGenerationOptions,
19421971
);
19431972
}
19441973

@@ -1959,7 +1988,10 @@ export class BaileysStartupService extends ChannelStartupService {
19591988
return await this.client.sendMessage(
19601989
sender,
19611990
message as unknown as AnyMessageContent,
1962-
option as unknown as MiscMessageGenerationOptions,
1991+
{
1992+
...option,
1993+
cachedGroupMetadata: this.getGroupMetadataCache,
1994+
} as unknown as MiscMessageGenerationOptions,
19631995
);
19641996
})();
19651997

@@ -3164,6 +3196,40 @@ export class BaileysStartupService extends ChannelStartupService {
31643196
}
31653197

31663198
// Group
3199+
private async updateGroupMetadataCache(groupJid: string) {
3200+
try {
3201+
const meta = await this.client.groupMetadata(groupJid);
3202+
console.log('updateGroupMetadataCache', groupJid);
3203+
await this.baileysCache.set(`group-metadata-${groupJid}`, {
3204+
timestamp: Date.now(),
3205+
data: meta,
3206+
});
3207+
3208+
return meta;
3209+
} catch (error) {
3210+
this.logger.error(error);
3211+
}
3212+
}
3213+
3214+
private async getGroupMetadataCache(groupJid: string) {
3215+
if (!isJidGroup(groupJid)) return null;
3216+
3217+
console.log('getGroupMetadataCache', groupJid);
3218+
if (this.baileysCache.has(`group-metadata-${groupJid}`)) {
3219+
console.log('has cache');
3220+
const meta = await this.baileysCache.get(`group-metadata-${groupJid}`);
3221+
3222+
if (Date.now() - meta.timestamp > 60000) {
3223+
await this.updateGroupMetadataCache(groupJid);
3224+
}
3225+
3226+
console.log('meta.data', meta.data);
3227+
return meta.data;
3228+
}
3229+
3230+
return await this.updateGroupMetadataCache(groupJid);
3231+
}
3232+
31673233
public async createGroup(create: CreateGroupDto) {
31683234
this.logger.verbose('Creating group: ' + create.subject);
31693235
try {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class BusinessStartupService extends ChannelStartupService {
3636
public readonly repository: RepositoryBroker,
3737
public readonly cache: CacheService,
3838
public readonly chatwootCache: CacheService,
39-
public readonly messagesLostCache: CacheService,
39+
public readonly baileysCache: CacheService,
4040
private readonly providerFiles: ProviderFiles,
4141
) {
4242
super(configService, eventEmitter, repository, chatwootCache);

src/api/services/monitor.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class WAMonitoringService {
4444
private readonly repository: RepositoryBroker,
4545
private readonly cache: CacheService,
4646
private readonly chatwootCache: CacheService,
47-
private readonly messagesLostCache: CacheService,
47+
private readonly baileysCache: CacheService,
4848
private readonly providerFiles: ProviderFiles,
4949
) {
5050
this.logger.verbose('instance created');
@@ -368,7 +368,7 @@ export class WAMonitoringService {
368368
this.repository,
369369
this.cache,
370370
this.chatwootCache,
371-
this.messagesLostCache,
371+
this.baileysCache,
372372
this.providerFiles,
373373
);
374374

@@ -380,7 +380,7 @@ export class WAMonitoringService {
380380
this.repository,
381381
this.cache,
382382
this.chatwootCache,
383-
this.messagesLostCache,
383+
this.baileysCache,
384384
this.providerFiles,
385385
);
386386

0 commit comments

Comments
 (0)