Skip to content

Commit

Permalink
chore: Recover lost messages from cache in WhatsApp Baileys service
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcode-tec committed Jun 26, 2024
1 parent a737fed commit 0ded76d
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,45 @@ export class BaileysStartupService extends ChannelStartupService {
public phoneNumber: string;

private async recoveringMessages() {
this.logger.info('Recovering messages lost');
const cacheConf = this.configService.get<CacheConf>('CACHE');

if ((cacheConf?.REDIS?.ENABLED && cacheConf?.REDIS?.URI !== '') || cacheConf?.LOCAL?.ENABLED) {
this.logger.info('Recovering messages lost from cache');
setInterval(async () => {
this.baileysCache.keys().then((keys) => {
keys.forEach(async (key) => {
const message = await this.baileysCache.get(key.split(':')[2]);
const data = await this.baileysCache.get(key.split(':')[2]);

let message: any;
let retry: number;

if (!data?.message) {
message = data;
retry = 0;
} else {
message = data.message;
retry = data.retry;
}

if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node') {
this.logger.info('Message absent from node, retrying to send, key: ' + key.split(':')[2]);
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
retry = retry + 1;
this.logger.info(`Message absent from node, retrying to send, key: ${key.split(':')[2]} retry: ${retry}`);
if (message.messageStubParameters[1]) {
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
}

this.baileysCache.set(key.split(':')[2], { message, retry });

if (retry >= 100) {
this.logger.warn(`Message absent from node, retry limit reached, key: ${key.split(':')[2]}`);
this.baileysCache.delete(key.split(':')[2]);
return;
}
}
});
});
}, 30000);
// 15 minutes
}, 15 * 60 * 1000);
}
}

Expand Down Expand Up @@ -1112,9 +1135,12 @@ export class BaileysStartupService extends ChannelStartupService {
}

if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') {
this.logger.info('Recovering message lost');
this.logger.info(`Recovering message lost messageId: ${received.key.id}`);

await this.baileysCache.set(received.key.id, received);
await this.baileysCache.set(received.key.id, {
message: received,
retry: 0,
});
continue;
}

Expand Down

0 comments on commit 0ded76d

Please sign in to comment.