@@ -210,6 +210,8 @@ export class Client extends TypedEventEmitter<ClientEventMap> {
210210 private readonly presenceThrottle : PresenceThrottleOptions | undefined
211211 private readonly scheduleLimiter : RateLimiter | undefined
212212 private authExhausted = false
213+ /** Each chat's disappearing timer, learned from inbound messages so outbound sends can inherit it. */
214+ private readonly chatExpiration = new Map < string , number > ( )
213215 private _socket : BaileysSocket | undefined
214216 private reconnectTimer : ReturnType < typeof setTimeout > | undefined
215217 private listenerCleanup : SocketCleanup [ ] = [ ]
@@ -269,6 +271,7 @@ export class Client extends TypedEventEmitter<ClientEventMap> {
269271 this . qrTerminal = options . qrTerminal ?? true
270272 this . statusLog = options . statusLog ?? true
271273 if ( this . statusLog ) suppressLibsignalNoise ( )
274+ this . on ( 'message' , ( msg ) => this . rememberChatExpiration ( msg ) )
272275 this . reconnectOptions = options . reconnect ?? { }
273276 this . baileysExtra = options . baileys ?? { }
274277 this . auth = options . auth ?? new FileAuthStore ( { basePath : `./.zaileys/auth/${ this . sessionId } ` } )
@@ -873,10 +876,31 @@ export class Client extends TypedEventEmitter<ClientEventMap> {
873876 const recordSent = ( message : WAMessage ) : void => {
874877 void this . store . saveMessage ( message ) . catch ( ( err ) => this . logger . warn ( err , 'recordSent failed' ) )
875878 }
879+ const inherit = ( jid : string ) : number | undefined => this . chatExpiration . get ( jid )
876880 if ( this . _provider === 'cloud' || isJid ( to ) ) {
877- return MessageBuilder . create ( socket , to , undefined , recordSent )
881+ return MessageBuilder . create ( socket , to , undefined , recordSent , inherit )
882+ }
883+ return MessageBuilder . create (
884+ socket ,
885+ to ,
886+ ( raw ) => this . resolveRecipient ( raw ) ,
887+ recordSent ,
888+ inherit ,
889+ )
890+ }
891+
892+ private rememberChatExpiration ( msg : MessageContext ) : void {
893+ const room = msg . roomId
894+ if ( room === null ) return
895+ if ( msg . ephemeralDuration === null ) {
896+ this . chatExpiration . delete ( room )
897+ return
898+ }
899+ if ( this . chatExpiration . size >= 500 && ! this . chatExpiration . has ( room ) ) {
900+ const oldest = this . chatExpiration . keys ( ) . next ( ) . value
901+ if ( oldest !== undefined ) this . chatExpiration . delete ( oldest )
878902 }
879- return MessageBuilder . create ( socket , to , ( raw ) => this . resolveRecipient ( raw ) , recordSent )
903+ this . chatExpiration . set ( room , msg . ephemeralDuration )
880904 }
881905
882906 edit ( key : WAMessageKey ) : EditBuilder {
0 commit comments