@@ -14,6 +14,9 @@ use crate::param::Param;
14
14
use crate :: sync:: SyncData ;
15
15
use crate :: tools:: time;
16
16
use anyhow:: { anyhow, ensure, Result } ;
17
+ use std:: time:: Duration ;
18
+ use tokio:: task;
19
+ use tokio:: time:: sleep;
17
20
18
21
/// How long callee's or caller's phone ring.
19
22
///
@@ -43,7 +46,18 @@ pub struct CallInfo {
43
46
44
47
impl CallInfo {
45
48
fn is_stale_call ( & self ) -> bool {
46
- time ( ) > self . msg . timestamp_sent + RINGING_SECONDS
49
+ self . remaining_ring_seconds ( ) <= 0
50
+ }
51
+
52
+ fn remaining_ring_seconds ( & self ) -> i64 {
53
+ let mut remaining_seconds = self . msg . timestamp_sent + RINGING_SECONDS - time ( ) ;
54
+ if remaining_seconds < 0 {
55
+ remaining_seconds = 0 ;
56
+ }
57
+ if remaining_seconds > RINGING_SECONDS {
58
+ remaining_seconds = RINGING_SECONDS ;
59
+ }
60
+ return remaining_seconds;
47
61
}
48
62
}
49
63
@@ -121,6 +135,10 @@ impl Context {
121
135
Ok ( ( ) )
122
136
}
123
137
138
+ async fn emit_end_call_if_unaccepted ( wait : u64 ) {
139
+ sleep ( Duration :: from_secs ( wait) ) . await ;
140
+ }
141
+
124
142
pub ( crate ) async fn handle_call_msg (
125
143
& self ,
126
144
mime_message : & MimeMessage ,
@@ -134,6 +152,8 @@ impl Context {
134
152
self . emit_event ( EventType :: IncomingCall {
135
153
msg_id : call. msg . id ,
136
154
} ) ;
155
+ let wait = call. remaining_ring_seconds ( ) ;
156
+ task:: spawn ( Context :: emit_end_call_if_unaccepted ( wait. try_into ( ) ?) ) ;
137
157
}
138
158
}
139
159
SystemMessage :: CallAccepted => {
@@ -429,6 +449,18 @@ mod tests {
429
449
..Default :: default ( )
430
450
} ;
431
451
assert ! ( !call_info. is_stale_call( ) ) ;
452
+ assert_eq ! ( call_info. remaining_ring_seconds( ) , RINGING_SECONDS ) ;
453
+
454
+ // call started 5 seconds ago, this is not stale as well
455
+ let call_info = CallInfo {
456
+ msg : Message {
457
+ timestamp_sent : time ( ) - 5 ,
458
+ ..Default :: default ( )
459
+ } ,
460
+ ..Default :: default ( )
461
+ } ;
462
+ assert ! ( !call_info. is_stale_call( ) ) ;
463
+ assert_eq ! ( call_info. remaining_ring_seconds( ) , RINGING_SECONDS - 5 ) ;
432
464
433
465
// a call started one hour ago is clearly stale
434
466
let call_info = CallInfo {
@@ -439,6 +471,7 @@ mod tests {
439
471
..Default :: default ( )
440
472
} ;
441
473
assert ! ( call_info. is_stale_call( ) ) ;
474
+ assert_eq ! ( call_info. remaining_ring_seconds( ) , 0 ) ;
442
475
443
476
Ok ( ( ) )
444
477
}
0 commit comments