@@ -88,8 +88,8 @@ import { sendTelemetry } from '@utils/sendTelemetry';
8888import useMultiFileAuthStatePrisma from '@utils/use-multi-file-auth-state-prisma' ;
8989import { AuthStateProvider } from '@utils/use-multi-file-auth-state-provider-files' ;
9090import { useMultiFileAuthStateRedisDb } from '@utils/use-multi-file-auth-state-redis-db' ;
91- import axios from 'axios' ;
9291import audioDecode from 'audio-decode' ;
92+ import axios from 'axios' ;
9393import makeWASocket , {
9494 AnyMessageContent ,
9595 BufferedEventData ,
@@ -442,7 +442,7 @@ export class BaileysStartupService extends ChannelStartupService {
442442 qrcodeTerminal . generate ( qr , { small : true } , ( qrcode ) =>
443443 this . logger . log (
444444 `\n{ instance: ${ this . instance . name } pairingCode: ${ this . instance . qrcode . pairingCode } , qrcodeCount: ${ this . instance . qrcode . count } }\n` +
445- qrcode ,
445+ qrcode ,
446446 ) ,
447447 ) ;
448448
@@ -468,16 +468,16 @@ export class BaileysStartupService extends ChannelStartupService {
468468
469469 const statusCode = ( lastDisconnect ?. error as Boom ) ?. output ?. statusCode ;
470470 const codesToNotReconnect = [ DisconnectReason . loggedOut , DisconnectReason . forbidden , 402 , 406 ] ;
471-
471+
472472 // FIX: Do not reconnect if it's the initial connection (waiting for QR code)
473473 // This prevents infinite loop that blocks QR code generation
474474 const isInitialConnection = ! this . instance . wuid && ( this . instance . qrcode ?. count ?? 0 ) === 0 ;
475-
475+
476476 if ( isInitialConnection ) {
477477 this . logger . info ( 'Initial connection closed, waiting for QR code generation...' ) ;
478478 return ;
479479 }
480-
480+
481481 const shouldReconnect = ! codesToNotReconnect . includes ( statusCode ) ;
482482
483483 this . logger . info ( {
@@ -494,9 +494,7 @@ export class BaileysStartupService extends ChannelStartupService {
494494 await this . connectToWhatsapp ( this . phoneNumber ) ;
495495 } , 3000 ) ;
496496 } else {
497- this . logger . info (
498- `Skipping reconnection for status code ${ statusCode } (code is in codesToNotReconnect list)` ,
499- ) ;
497+ this . logger . info ( `Skipping reconnection for status code ${ statusCode } (code is in codesToNotReconnect list)` ) ;
500498 this . sendDataWebhook ( Events . STATUS_INSTANCE , {
501499 instance : this . instance . name ,
502500 status : 'closed' ,
@@ -1019,13 +1017,15 @@ export class BaileysStartupService extends ChannelStartupService {
10191017 syncType ?: proto . HistorySync . HistorySyncType ;
10201018 } ) => {
10211019 try {
1022- // Reset counters when a new sync starts (progress resets or decreases)
1023- if ( progress <= this . historySyncLastProgress ) {
1020+ const normalizedProgress = progress ?? - 1 ;
1021+
1022+ if ( normalizedProgress <= this . historySyncLastProgress ) {
10241023 this . historySyncMessageCount = 0 ;
10251024 this . historySyncChatCount = 0 ;
10261025 this . historySyncContactCount = 0 ;
10271026 }
1028- this . historySyncLastProgress = progress ?? - 1 ;
1027+
1028+ this . historySyncLastProgress = normalizedProgress ;
10291029
10301030 if ( syncType === proto . HistorySync . HistorySyncType . ON_DEMAND ) {
10311031 console . log ( 'received on-demand history sync, messages=' , messages ) ;
@@ -1129,16 +1129,16 @@ export class BaileysStartupService extends ChannelStartupService {
11291129
11301130 const messagesRepository : Set < string > = new Set (
11311131 chatwootImport . getRepositoryMessagesCache ( instance ) ??
1132- (
1133- await this . prismaRepository . message . findMany ( {
1134- select : { key : true } ,
1135- where : { instanceId : this . instanceId } ,
1136- } )
1137- ) . map ( ( message ) => {
1138- const key = message . key as { id : string } ;
1132+ (
1133+ await this . prismaRepository . message . findMany ( {
1134+ select : { key : true } ,
1135+ where : { instanceId : this . instanceId } ,
1136+ } )
1137+ ) . map ( ( message ) => {
1138+ const key = message . key as { id : string } ;
11391139
1140- return key . id ;
1141- } ) ,
1140+ return key . id ;
1141+ } ) ,
11421142 ) ;
11431143
11441144 if ( chatwootImport . getRepositoryMessagesCache ( instance ) === null ) {
@@ -1202,11 +1202,7 @@ export class BaileysStartupService extends ChannelStartupService {
12021202 const filteredContacts = contacts . filter ( ( c ) => ! ! c . notify || ! ! c . name ) ;
12031203 this . historySyncContactCount += filteredContacts . length ;
12041204
1205- await this . contactHandle [ 'contacts.upsert' ] (
1206- filteredContacts . map ( ( c ) => ( { id : c . id , name : c . name ?? c . notify } ) ) ,
1207- ) ;
1208-
1209- if ( progress === 100 ) {
1205+ if ( normalizedProgress === 100 ) {
12101206 this . sendDataWebhook ( Events . MESSAGING_HISTORY_SET , {
12111207 messageCount : this . historySyncMessageCount ,
12121208 chatCount : this . historySyncChatCount ,
@@ -1219,6 +1215,10 @@ export class BaileysStartupService extends ChannelStartupService {
12191215 this . historySyncLastProgress = - 1 ;
12201216 }
12211217
1218+ await this . contactHandle [ 'contacts.upsert' ] (
1219+ filteredContacts . map ( ( c ) => ( { id : c . id , name : c . name ?? c . notify } ) ) ,
1220+ ) ;
1221+
12221222 contacts = undefined ;
12231223 messages = undefined ;
12241224 chats = undefined ;
@@ -1826,7 +1826,7 @@ export class BaileysStartupService extends ChannelStartupService {
18261826
18271827 if ( ! findMessage ?. id ) {
18281828 this . logger . verbose (
1829- `Original message not found for update after ${ maxRetries } retries . Skipping. This is expected for protocol messages or ephemeral events not saved to the database. Key: ${ JSON . stringify ( key ) } ` ,
1829+ `Original message not found for update. Skipping. This is expected for protocol messages or ephemeral events not saved to the database. Key: ${ JSON . stringify ( key ) } ` ,
18301830 ) ;
18311831 continue ;
18321832 }
@@ -2337,12 +2337,12 @@ export class BaileysStartupService extends ChannelStartupService {
23372337 const url = match [ 0 ] . replace ( / [ . , ) ; \] ] + $ / u, '' ) ;
23382338 if ( ! url ) return undefined ;
23392339
2340- const previewData = await getLinkPreview ( url , {
2340+ const previewData = ( await getLinkPreview ( url , {
23412341 imagesPropertyType : 'og' , // fetches only open-graph images
23422342 headers : {
23432343 'user-agent' : 'googlebot' , // fetches with googlebot to prevent login pages
23442344 } ,
2345- } ) as any ;
2345+ } ) ) as any ;
23462346
23472347 if ( ! previewData || ! previewData . title ) return undefined ;
23482348
@@ -2356,9 +2356,9 @@ export class BaileysStartupService extends ChannelStartupService {
23562356 thumbnailUrl : image ,
23572357 sourceUrl : url ,
23582358 mediaUrl : url ,
2359- renderLargerThumbnail : true
2359+ renderLargerThumbnail : true ,
23602360 // showAdAttribution: true // Removed to prevent "Sent via ad" label
2361- }
2361+ } ,
23622362 } ;
23632363 } catch ( error ) {
23642364 this . logger . error ( `Error generating link preview: ${ error } ` ) ;
0 commit comments