@@ -559,10 +559,6 @@ export class BaileysStartupService extends ChannelStartupService {
559559 }
560560 }
561561
562- private getUpsertEmittedCacheKey ( messageId : string ) {
563- return `upsert_emitted_${ this . instanceId } _${ messageId } ` ;
564- }
565-
566562 private async defineAuthState ( ) {
567563 const db = this . configService . get < Database > ( 'DATABASE' ) ;
568564 const cache = this . configService . get < CacheConf > ( 'CACHE' ) ;
@@ -950,13 +946,15 @@ export class BaileysStartupService extends ChannelStartupService {
950946 syncType ?: proto . HistorySync . HistorySyncType ;
951947 } ) => {
952948 try {
953- // Reset counters when a new sync starts (progress resets or decreases)
954- if ( progress <= this . historySyncLastProgress ) {
949+ const normalizedProgress = progress ?? - 1 ;
950+
951+ if ( normalizedProgress <= this . historySyncLastProgress ) {
955952 this . historySyncMessageCount = 0 ;
956953 this . historySyncChatCount = 0 ;
957954 this . historySyncContactCount = 0 ;
958955 }
959- this . historySyncLastProgress = progress ?? - 1 ;
956+
957+ this . historySyncLastProgress = normalizedProgress ;
960958
961959 if ( syncType === proto . HistorySync . HistorySyncType . ON_DEMAND ) {
962960 console . log ( 'received on-demand history sync, messages=' , messages ) ;
@@ -1007,14 +1005,14 @@ export class BaileysStartupService extends ChannelStartupService {
10071005 chatsRaw . push ( { remoteJid : chat . id , instanceId : this . instanceId , name : chat . name } ) ;
10081006 }
10091007
1010- if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . HISTORIC ) {
1011- await this . prismaRepository . chat . createMany ( { data : chatsRaw , skipDuplicates : true } ) ;
1012- }
1013-
10141008 this . historySyncChatCount += chatsRaw . length ;
10151009
10161010 this . sendDataWebhook ( Events . CHATS_SET , chatsRaw ) ;
10171011
1012+ if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . HISTORIC ) {
1013+ await this . prismaRepository . chat . createMany ( { data : chatsRaw , skipDuplicates : true } ) ;
1014+ }
1015+
10181016 const messagesRaw : any [ ] = [ ] ;
10191017
10201018 const messagesRepository : Set < string > = new Set (
@@ -1068,15 +1066,15 @@ export class BaileysStartupService extends ChannelStartupService {
10681066
10691067 this . historySyncMessageCount += messagesRaw . length ;
10701068
1071- if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . HISTORIC ) {
1072- await this . prismaRepository . message . createMany ( { data : messagesRaw , skipDuplicates : true } ) ;
1073- }
1074-
10751069 this . sendDataWebhook ( Events . MESSAGES_SET , [ ...messagesRaw ] , true , undefined , {
10761070 isLatest,
10771071 progress,
10781072 } ) ;
10791073
1074+ if ( this . configService . get < Database > ( 'DATABASE' ) . SAVE_DATA . HISTORIC ) {
1075+ await this . prismaRepository . message . createMany ( { data : messagesRaw , skipDuplicates : true } ) ;
1076+ }
1077+
10801078 if (
10811079 this . configService . get < Chatwoot > ( 'CHATWOOT' ) . ENABLED &&
10821080 this . localChatwoot ?. enabled &&
@@ -1092,7 +1090,7 @@ export class BaileysStartupService extends ChannelStartupService {
10921090 const filteredContacts = contacts . filter ( ( c ) => ! ! c . notify || ! ! c . name ) ;
10931091 this . historySyncContactCount += filteredContacts . length ;
10941092
1095- if ( progress === 100 ) {
1093+ if ( normalizedProgress === 100 ) {
10961094 this . sendDataWebhook ( Events . MESSAGING_HISTORY_SET , {
10971095 messageCount : this . historySyncMessageCount ,
10981096 chatCount : this . historySyncChatCount ,
@@ -1427,6 +1425,8 @@ export class BaileysStartupService extends ChannelStartupService {
14271425 try {
14281426 if ( isVideo && ! this . configService . get < S3 > ( 'S3' ) . SAVE_VIDEO ) {
14291427 this . logger . warn ( 'Video upload is disabled. Skipping video upload.' ) ;
1428+ // Skip video upload by returning early from this block
1429+ return ;
14301430 } else {
14311431 const message : any = received ;
14321432
@@ -1440,35 +1440,34 @@ export class BaileysStartupService extends ChannelStartupService {
14401440
14411441 if ( ! media ) {
14421442 this . logger . verbose ( 'No valid media to upload (messageContextInfo only), skipping MinIO' ) ;
1443- } else {
1444- const { buffer, mediaType, fileName, size } = media ;
1445- const mimetype = mimeTypes . lookup ( fileName ) . toString ( ) ;
1446- const fullName = join (
1447- `${ this . instance . id } ` ,
1448- received . key . remoteJid ,
1449- mediaType ,
1450- `${ Date . now ( ) } _${ fileName } ` ,
1451- ) ;
1452- await s3Service . uploadFile ( fullName , buffer , size . fileLength ?. low , {
1453- 'Content-Type' : mimetype ,
1454- } ) ;
1455-
1456- await this . prismaRepository . media . create ( {
1457- data : {
1458- messageId : msg . id ,
1459- instanceId : this . instanceId ,
1460- type : mediaType ,
1461- fileName : fullName ,
1462- mimetype,
1463- } ,
1464- } ) ;
1465-
1466- const mediaUrl = await s3Service . getObjectUrl ( fullName ) ;
1467-
1468- messageRaw . message . mediaUrl = mediaUrl ;
1469-
1470- await this . prismaRepository . message . update ( { where : { id : msg . id } , data : messageRaw } ) ;
1443+ return ;
14711444 }
1445+
1446+ const { buffer, mediaType, fileName, size } = media ;
1447+ const mimetype = mimeTypes . lookup ( fileName ) . toString ( ) ;
1448+ const fullName = join (
1449+ `${ this . instance . id } ` ,
1450+ received . key . remoteJid ,
1451+ mediaType ,
1452+ `${ Date . now ( ) } _${ fileName } ` ,
1453+ ) ;
1454+ await s3Service . uploadFile ( fullName , buffer , size . fileLength ?. low , { 'Content-Type' : mimetype } ) ;
1455+
1456+ await this . prismaRepository . media . create ( {
1457+ data : {
1458+ messageId : msg . id ,
1459+ instanceId : this . instanceId ,
1460+ type : mediaType ,
1461+ fileName : fullName ,
1462+ mimetype,
1463+ } ,
1464+ } ) ;
1465+
1466+ const mediaUrl = await s3Service . getObjectUrl ( fullName ) ;
1467+
1468+ messageRaw . message . mediaUrl = mediaUrl ;
1469+
1470+ await this . prismaRepository . message . update ( { where : { id : msg . id } , data : messageRaw } ) ;
14721471 }
14731472 }
14741473 } catch ( error ) {
@@ -1515,11 +1514,9 @@ export class BaileysStartupService extends ChannelStartupService {
15151514 if ( messageRaw . key . remoteJid ?. includes ( '@lid' ) && messageRaw . key . remoteJidAlt ) {
15161515 messageRaw . key . remoteJid = messageRaw . key . remoteJidAlt ;
15171516 }
1518- await this . sendDataWebhook ( Events . MESSAGES_UPSERT , messageRaw ) ;
1517+ console . log ( messageRaw ) ;
15191518
1520- if ( messageRaw . messageType === 'audioMessage' && ! messageRaw . key . fromMe && messageRaw . key . id ) {
1521- await this . baileysCache . set ( this . getUpsertEmittedCacheKey ( messageRaw . key . id ) , true , 60 * 10 ) ;
1522- }
1519+ this . sendDataWebhook ( Events . MESSAGES_UPSERT , messageRaw ) ;
15231520
15241521 await chatbotController . emit ( {
15251522 instance : { instanceName : this . instance . name , instanceId : this . instanceId } ,
@@ -1689,36 +1686,6 @@ export class BaileysStartupService extends ChannelStartupService {
16891686 continue ;
16901687 }
16911688
1692- if ( ! key . fromMe && findMessage . messageType === 'audioMessage' && key . id ) {
1693- const upsertCacheKey = this . getUpsertEmittedCacheKey ( key . id ) ;
1694- const alreadyEmitted = await this . baileysCache . get ( upsertCacheKey ) ;
1695-
1696- if ( ! alreadyEmitted ) {
1697- const fallbackUpsertPayload = {
1698- key : findMessage . key ,
1699- pushName : findMessage . pushName ,
1700- status : findMessage . status ,
1701- message : findMessage . message ,
1702- contextInfo : findMessage . contextInfo ,
1703- messageType : findMessage . messageType ,
1704- messageTimestamp : findMessage . messageTimestamp ,
1705- instanceId : findMessage . instanceId ,
1706- source : findMessage . source ,
1707- } ;
1708-
1709- try {
1710- await this . sendDataWebhook ( Events . MESSAGES_UPSERT , fallbackUpsertPayload ) ;
1711- await this . baileysCache . set ( upsertCacheKey , true , 60 * 10 ) ;
1712- this . logger . warn ( `Fallback messages.upsert emitted for audio message ${ key . id } ` ) ;
1713- } catch ( error ) {
1714- this . logger . error ( [
1715- `Failed to emit fallback messages.upsert for audio message ${ key . id } ` ,
1716- error ?. message ,
1717- ] ) ;
1718- }
1719- }
1720- }
1721-
17221689 message . messageId = findMessage . id ;
17231690 }
17241691
0 commit comments