Skip to content

Commit c0523d6

Browse files
fix(history-sync): ignore non-primary completions
1 parent e2ab632 commit c0523d6

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,15 +1017,18 @@ export class BaileysStartupService extends ChannelStartupService {
10171017
syncType?: proto.HistorySync.HistorySyncType;
10181018
}) => {
10191019
try {
1020+
const shouldTrackHistorySync = progress !== undefined && this.isSyncNotificationFromUsedSyncType(syncType);
10201021
const normalizedProgress = progress ?? -1;
10211022

1022-
if (normalizedProgress <= this.historySyncLastProgress) {
1023+
if (shouldTrackHistorySync && normalizedProgress <= this.historySyncLastProgress) {
10231024
this.historySyncMessageCount = 0;
10241025
this.historySyncChatCount = 0;
10251026
this.historySyncContactCount = 0;
10261027
}
10271028

1028-
this.historySyncLastProgress = normalizedProgress;
1029+
if (shouldTrackHistorySync) {
1030+
this.historySyncLastProgress = normalizedProgress;
1031+
}
10291032

10301033
if (syncType === proto.HistorySync.HistorySyncType.ON_DEMAND) {
10311034
console.log('received on-demand history sync, messages=', messages);
@@ -1121,7 +1124,9 @@ export class BaileysStartupService extends ChannelStartupService {
11211124
await this.prismaRepository.chat.createMany({ data: chatsToCreateMany, skipDuplicates: true });
11221125
}
11231126

1124-
this.historySyncChatCount += chatsRaw.length;
1127+
if (shouldTrackHistorySync) {
1128+
this.historySyncChatCount += chatsRaw.length;
1129+
}
11251130

11261131
this.sendDataWebhook(Events.CHATS_SET, chatsRaw);
11271132

@@ -1176,7 +1181,9 @@ export class BaileysStartupService extends ChannelStartupService {
11761181
messagesRaw.push(this.prepareMessage(m));
11771182
}
11781183

1179-
this.historySyncMessageCount += messagesRaw.length;
1184+
if (shouldTrackHistorySync) {
1185+
this.historySyncMessageCount += messagesRaw.length;
1186+
}
11801187

11811188
if (this.configService.get<Database>('DATABASE').SAVE_DATA.HISTORIC) {
11821189
await this.prismaRepository.message.createMany({ data: messagesRaw, skipDuplicates: true });
@@ -1200,14 +1207,17 @@ export class BaileysStartupService extends ChannelStartupService {
12001207
}
12011208

12021209
const filteredContacts = contacts.filter((c) => !!c.notify || !!c.name);
1203-
this.historySyncContactCount += filteredContacts.length;
1210+
if (shouldTrackHistorySync) {
1211+
this.historySyncContactCount += filteredContacts.length;
1212+
}
12041213

1205-
if (normalizedProgress === 100) {
1214+
if (shouldTrackHistorySync && normalizedProgress === 100) {
12061215
this.sendDataWebhook(Events.MESSAGING_HISTORY_SET, {
12071216
messageCount: this.historySyncMessageCount,
12081217
chatCount: this.historySyncChatCount,
12091218
contactCount: this.historySyncContactCount,
12101219
progress: normalizedProgress,
1220+
syncType,
12111221
});
12121222

12131223
this.historySyncMessageCount = 0;
@@ -2214,7 +2224,7 @@ export class BaileysStartupService extends ChannelStartupService {
22142224
this.configService.get<Chatwoot>('CHATWOOT').ENABLED &&
22152225
this.localChatwoot?.enabled &&
22162226
this.localChatwoot.importMessages &&
2217-
this.isSyncNotificationFromUsedSyncType(msg)
2227+
this.isSyncNotificationFromUsedSyncType(msg?.syncType)
22182228
) {
22192229
if (msg.chunkOrder === 1) {
22202230
this.chatwootService.startImportHistoryMessages(instance);
@@ -2230,10 +2240,12 @@ export class BaileysStartupService extends ChannelStartupService {
22302240
return true;
22312241
}
22322242

2233-
private isSyncNotificationFromUsedSyncType(msg: proto.Message.IHistorySyncNotification) {
2243+
private isSyncNotificationFromUsedSyncType(
2244+
syncType?: proto.HistorySync.HistorySyncType | proto.Message.HistorySyncType,
2245+
) {
22342246
return (
2235-
(this.localSettings.syncFullHistory && msg?.syncType === 2) ||
2236-
(!this.localSettings.syncFullHistory && msg?.syncType === 3)
2247+
(this.localSettings.syncFullHistory && syncType === proto.HistorySync.HistorySyncType.FULL) ||
2248+
(!this.localSettings.syncFullHistory && syncType === proto.HistorySync.HistorySyncType.RECENT)
22372249
);
22382250
}
22392251

0 commit comments

Comments
 (0)