Skip to content

Commit cb24a21

Browse files
committed
feat(baileys): implementa PR evolution-foundation#2510 - history sync completion event com contadores
1 parent 838e34d commit cb24a21

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ export class BaileysStartupService extends ChannelStartupService {
269269
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
270270
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates
271271

272+
// 🔧 PR #2510: Contadores de history sync para emitir MESSAGING_HISTORY_SET
273+
private historySyncMessageCount = 0;
274+
private historySyncChatCount = 0;
275+
private historySyncContactCount = 0;
276+
private historySyncLastProgress = 0;
277+
272278
public stateConnection: wa.StateConnection = { state: 'close' };
273279

274280
public phoneNumber: string;
@@ -1002,6 +1008,16 @@ export class BaileysStartupService extends ChannelStartupService {
10021008
syncType?: proto.HistorySync.HistorySyncType;
10031009
}) => {
10041010
try {
1011+
// 🔧 PR #2510: Reset contadores se progress reiniciar ou diminuir
1012+
if (progress !== undefined && (progress < this.historySyncLastProgress || progress === 0)) {
1013+
this.logger.verbose(
1014+
`🔄 History sync reset detected (progress: ${progress}% < ${this.historySyncLastProgress}%)`,
1015+
);
1016+
this.historySyncMessageCount = 0;
1017+
this.historySyncChatCount = 0;
1018+
this.historySyncContactCount = 0;
1019+
}
1020+
this.historySyncLastProgress = progress ?? 0;
10051021
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
10061022
console.log('received on-demand history sync, messages=', messages);
10071023
}
@@ -1073,6 +1089,9 @@ export class BaileysStartupService extends ChannelStartupService {
10731089
});
10741090
}
10751091

1092+
// 🔧 PR #2510: Incrementa contador de chats
1093+
this.historySyncChatCount += chatsRaw.length;
1094+
10761095
const messagesRaw: any[] = [];
10771096

10781097
const messagesRepository: Set<string> = new Set(
@@ -1136,6 +1155,9 @@ export class BaileysStartupService extends ChannelStartupService {
11361155
});
11371156
}
11381157

1158+
// 🔧 PR #2510: Incrementa contador de mensagens
1159+
this.historySyncMessageCount += messagesRaw.length;
1160+
11391161
if (
11401162
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
11411163
this.localChatwoot?.enabled &&
@@ -1148,8 +1170,31 @@ export class BaileysStartupService extends ChannelStartupService {
11481170
);
11491171
}
11501172

1173+
// 🔧 PR #2510: Filtra e conta contatos antes de upsert
1174+
const filteredContacts = contacts.filter((c) => !!c.notify || !!c.name);
1175+
this.historySyncContactCount += filteredContacts.length;
1176+
1177+
// 🔧 PR #2510: Emite MESSAGING_HISTORY_SET quando sync completa (progress = 100)
1178+
if (progress === 100) {
1179+
this.logger.log(
1180+
`✅ History sync completed: ${this.historySyncMessageCount} messages, ${this.historySyncChatCount} chats, ${this.historySyncContactCount} contacts`,
1181+
);
1182+
1183+
this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, {
1184+
messageCount: this.historySyncMessageCount,
1185+
chatCount: this.historySyncChatCount,
1186+
contactCount: this.historySyncContactCount,
1187+
});
1188+
1189+
// Reset contadores após emitir evento
1190+
this.historySyncMessageCount = 0;
1191+
this.historySyncChatCount = 0;
1192+
this.historySyncContactCount = 0;
1193+
this.historySyncLastProgress = 0;
1194+
}
1195+
11511196
await this.contactHandle['contacts.upsert'](
1152-
contacts.filter((c) => !!c.notify || !!c.name).map((c) => ({ id: c.id, name: c.name ?? c.notify })),
1197+
filteredContacts.map((c) => ({ id: c.id, name: c.name ?? c.notify })),
11531198
);
11541199

11551200
contacts = undefined;

0 commit comments

Comments
 (0)