@@ -137,7 +137,7 @@ export class BaileysStartupService extends ChannelStartupService {
137137 public readonly repository : RepositoryBroker ,
138138 public readonly cache : CacheService ,
139139 public readonly chatwootCache : CacheService ,
140- public readonly messagesLostCache : CacheService ,
140+ public readonly baileysCache : CacheService ,
141141 private readonly providerFiles : ProviderFiles ,
142142 ) {
143143 super ( configService , eventEmitter , repository , chatwootCache ) ;
@@ -146,6 +146,8 @@ export class BaileysStartupService extends ChannelStartupService {
146146 this . instance . qrcode = { count : 0 } ;
147147 this . mobile = false ;
148148 this . recoveringMessages ( ) ;
149+ this . forceUpdateGroupMetadataCache ( ) ;
150+
149151 this . authStateProvider = new AuthStateProvider ( this . providerFiles ) ;
150152 }
151153
@@ -166,9 +168,9 @@ export class BaileysStartupService extends ChannelStartupService {
166168
167169 if ( ( cacheConf ?. REDIS ?. ENABLED && cacheConf ?. REDIS ?. URI !== '' ) || cacheConf ?. LOCAL ?. ENABLED ) {
168170 setInterval ( async ( ) => {
169- this . messagesLostCache . keys ( ) . then ( ( keys ) => {
171+ this . baileysCache . keys ( ) . then ( ( keys ) => {
170172 keys . forEach ( async ( key ) => {
171- const message = await this . messagesLostCache . get ( key . split ( ':' ) [ 2 ] ) ;
173+ const message = await this . baileysCache . get ( key . split ( ':' ) [ 2 ] ) ;
172174
173175 if ( message . messageStubParameters && message . messageStubParameters [ 0 ] === 'Message absent from node' ) {
174176 this . logger . info ( 'Message absent from node, retrying to send, key: ' + key . split ( ':' ) [ 2 ] ) ;
@@ -180,6 +182,17 @@ export class BaileysStartupService extends ChannelStartupService {
180182 }
181183 }
182184
185+ private async forceUpdateGroupMetadataCache ( ) {
186+ setInterval ( async ( ) => {
187+ this . logger . verbose ( 'Forcing update group metadata cache' ) ;
188+ const groups = await this . fetchAllGroups ( { getParticipants : 'false' } ) ;
189+
190+ for ( const group of groups ) {
191+ await this . updateGroupMetadataCache ( group . id ) ;
192+ }
193+ } , 60000 ) ;
194+ }
195+
183196 public get connectionStatus ( ) {
184197 this . logger . verbose ( 'Getting connection status' ) ;
185198 return this . stateConnection ;
@@ -1124,15 +1137,15 @@ export class BaileysStartupService extends ChannelStartupService {
11241137 if ( received . messageStubParameters && received . messageStubParameters [ 0 ] === 'Message absent from node' ) {
11251138 this . logger . info ( 'Recovering message lost' ) ;
11261139
1127- await this . messagesLostCache . set ( received . key . id , received ) ;
1140+ await this . baileysCache . set ( received . key . id , received ) ;
11281141 continue ;
11291142 }
11301143
1131- const retryCache = ( await this . messagesLostCache . get ( received . key . id ) ) || null ;
1144+ const retryCache = ( await this . baileysCache . get ( received . key . id ) ) || null ;
11321145
11331146 if ( retryCache ) {
11341147 this . logger . info ( 'Recovered message lost' ) ;
1135- await this . messagesLostCache . delete ( received . key . id ) ;
1148+ await this . baileysCache . delete ( received . key . id ) ;
11361149 }
11371150
11381151 if (
@@ -1421,6 +1434,12 @@ export class BaileysStartupService extends ChannelStartupService {
14211434
14221435 this . logger . verbose ( 'Sending data to webhook in event GROUPS_UPDATE' ) ;
14231436 this . sendDataWebhook ( Events . GROUPS_UPDATE , groupMetadataUpdate ) ;
1437+
1438+ groupMetadataUpdate . forEach ( ( group ) => {
1439+ if ( isJidGroup ( group . id ) ) {
1440+ this . updateGroupMetadataCache ( group . id ) ;
1441+ }
1442+ } ) ;
14241443 } ,
14251444
14261445 'group-participants.update' : ( participantsUpdate : {
@@ -1857,7 +1876,8 @@ export class BaileysStartupService extends ChannelStartupService {
18571876 let mentions : string [ ] ;
18581877 if ( isJidGroup ( sender ) ) {
18591878 try {
1860- const group = await this . findGroup ( { groupJid : sender } , 'inner' ) ;
1879+ // const group = await this.findGroup({ groupJid: sender }, 'inner');
1880+ const group = await this . getGroupMetadataCache ( sender ) ;
18611881
18621882 if ( ! group ) {
18631883 throw new NotFoundException ( 'Group not found' ) ;
@@ -1910,7 +1930,10 @@ export class BaileysStartupService extends ChannelStartupService {
19101930 key : message [ 'reactionMessage' ] [ 'key' ] ,
19111931 } ,
19121932 } as unknown as AnyMessageContent ,
1913- option as unknown as MiscMessageGenerationOptions ,
1933+ {
1934+ ...option ,
1935+ cachedGroupMetadata : this . getGroupMetadataCache ,
1936+ } as unknown as MiscMessageGenerationOptions ,
19141937 ) ;
19151938 }
19161939 }
@@ -1923,7 +1946,10 @@ export class BaileysStartupService extends ChannelStartupService {
19231946 mentions,
19241947 linkPreview : linkPreview ,
19251948 } as unknown as AnyMessageContent ,
1926- option as unknown as MiscMessageGenerationOptions ,
1949+ {
1950+ ...option ,
1951+ cachedGroupMetadata : this . getGroupMetadataCache ,
1952+ } as unknown as MiscMessageGenerationOptions ,
19271953 ) ;
19281954 }
19291955
@@ -1938,7 +1964,10 @@ export class BaileysStartupService extends ChannelStartupService {
19381964 } ,
19391965 mentions,
19401966 } ,
1941- option as unknown as MiscMessageGenerationOptions ,
1967+ {
1968+ ...option ,
1969+ cachedGroupMetadata : this . getGroupMetadataCache ,
1970+ } as unknown as MiscMessageGenerationOptions ,
19421971 ) ;
19431972 }
19441973
@@ -1959,7 +1988,10 @@ export class BaileysStartupService extends ChannelStartupService {
19591988 return await this . client . sendMessage (
19601989 sender ,
19611990 message as unknown as AnyMessageContent ,
1962- option as unknown as MiscMessageGenerationOptions ,
1991+ {
1992+ ...option ,
1993+ cachedGroupMetadata : this . getGroupMetadataCache ,
1994+ } as unknown as MiscMessageGenerationOptions ,
19631995 ) ;
19641996 } ) ( ) ;
19651997
@@ -3164,6 +3196,40 @@ export class BaileysStartupService extends ChannelStartupService {
31643196 }
31653197
31663198 // Group
3199+ private async updateGroupMetadataCache ( groupJid : string ) {
3200+ try {
3201+ const meta = await this . client . groupMetadata ( groupJid ) ;
3202+ console . log ( 'updateGroupMetadataCache' , groupJid ) ;
3203+ await this . baileysCache . set ( `group-metadata-${ groupJid } ` , {
3204+ timestamp : Date . now ( ) ,
3205+ data : meta ,
3206+ } ) ;
3207+
3208+ return meta ;
3209+ } catch ( error ) {
3210+ this . logger . error ( error ) ;
3211+ }
3212+ }
3213+
3214+ private async getGroupMetadataCache ( groupJid : string ) {
3215+ if ( ! isJidGroup ( groupJid ) ) return null ;
3216+
3217+ console . log ( 'getGroupMetadataCache' , groupJid ) ;
3218+ if ( this . baileysCache . has ( `group-metadata-${ groupJid } ` ) ) {
3219+ console . log ( 'has cache' ) ;
3220+ const meta = await this . baileysCache . get ( `group-metadata-${ groupJid } ` ) ;
3221+
3222+ if ( Date . now ( ) - meta . timestamp > 60000 ) {
3223+ await this . updateGroupMetadataCache ( groupJid ) ;
3224+ }
3225+
3226+ console . log ( 'meta.data' , meta . data ) ;
3227+ return meta . data ;
3228+ }
3229+
3230+ return await this . updateGroupMetadataCache ( groupJid ) ;
3231+ }
3232+
31673233 public async createGroup ( create : CreateGroupDto ) {
31683234 this . logger . verbose ( 'Creating group: ' + create . subject ) ;
31693235 try {
0 commit comments