@@ -12,10 +12,22 @@ use crate::message::{rfc724_mid_exists, Message, MsgId, Viewtype};
12
12
use crate :: mimeparser:: { MimeMessage , SystemMessage } ;
13
13
use crate :: param:: Param ;
14
14
use crate :: sync:: SyncData ;
15
+ use crate :: tools:: time;
15
16
use anyhow:: { anyhow, ensure, Result } ;
16
17
18
+ /// How long callee's or caller's phone ring.
19
+ ///
20
+ /// For the callee, this is to prevent endless ringing
21
+ /// in case the initial "call" is received, but then the caller went offline.
22
+ /// Moreover, this prevents outdated calls to ring
23
+ /// in case the initial "call" message arrives delayed.
24
+ ///
25
+ /// For the caller, this means they should also not wait longer,
26
+ /// as the callee won't start the call afterwards.
27
+ const RINGING_SECONDS : i64 = 60 ;
28
+
17
29
/// Information about the status of a call.
18
- #[ derive( Debug ) ]
30
+ #[ derive( Debug , Default ) ]
19
31
pub struct CallInfo {
20
32
/// Incoming our outgoing call?
21
33
pub incoming : bool ,
@@ -29,6 +41,12 @@ pub struct CallInfo {
29
41
pub msg : Message ,
30
42
}
31
43
44
+ impl CallInfo {
45
+ fn is_stale_call ( & self ) -> bool {
46
+ time ( ) > self . msg . timestamp_sent + RINGING_SECONDS
47
+ }
48
+ }
49
+
32
50
impl Context {
33
51
/// Start an outgoing call.
34
52
pub async fn place_outgoing_call ( & self , chat_id : ChatId ) -> Result < MsgId > {
@@ -112,7 +130,7 @@ impl Context {
112
130
SystemMessage :: IncomingCall => {
113
131
let call = self . load_call_by_root_id ( call_or_child_id) . await ?;
114
132
self . emit_msgs_changed ( call. msg . chat_id , call_or_child_id) ;
115
- if call. incoming {
133
+ if call. incoming && !call . is_stale_call ( ) {
116
134
self . emit_event ( EventType :: IncomingCall {
117
135
msg_id : call. msg . id ,
118
136
} ) ;
@@ -399,4 +417,29 @@ mod tests {
399
417
400
418
Ok ( ( ) )
401
419
}
420
+
421
+ #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
422
+ async fn test_is_stale_call ( ) -> Result < ( ) > {
423
+ // a call started now is not stale
424
+ let call_info = CallInfo {
425
+ msg : Message {
426
+ timestamp_sent : time ( ) ,
427
+ ..Default :: default ( )
428
+ } ,
429
+ ..Default :: default ( )
430
+ } ;
431
+ assert ! ( !call_info. is_stale_call( ) ) ;
432
+
433
+ // a call started one hour ago is clearly stale
434
+ let call_info = CallInfo {
435
+ msg : Message {
436
+ timestamp_sent : time ( ) - 3600 ,
437
+ ..Default :: default ( )
438
+ } ,
439
+ ..Default :: default ( )
440
+ } ;
441
+ assert ! ( call_info. is_stale_call( ) ) ;
442
+
443
+ Ok ( ( ) )
444
+ }
402
445
}
0 commit comments