Skip to content

Commit 74f634e

Browse files
committed
do not emit IncomingCall in case the call is stale
1 parent 9c5552e commit 74f634e

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/calls.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ use crate::message::{rfc724_mid_exists, Message, MsgId, Viewtype};
1212
use crate::mimeparser::{MimeMessage, SystemMessage};
1313
use crate::param::Param;
1414
use crate::sync::SyncData;
15+
use crate::tools::time;
1516
use anyhow::{anyhow, ensure, Result};
1617

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+
1729
/// Information about the status of a call.
18-
#[derive(Debug)]
30+
#[derive(Debug, Default)]
1931
pub struct CallInfo {
2032
/// Incoming our outgoing call?
2133
pub incoming: bool,
@@ -29,6 +41,12 @@ pub struct CallInfo {
2941
pub msg: Message,
3042
}
3143

44+
impl CallInfo {
45+
fn is_stale_call(&self) -> bool {
46+
time() > self.msg.timestamp_sent + RINGING_SECONDS
47+
}
48+
}
49+
3250
impl Context {
3351
/// Start an outgoing call.
3452
pub async fn place_outgoing_call(&self, chat_id: ChatId) -> Result<MsgId> {
@@ -112,7 +130,7 @@ impl Context {
112130
SystemMessage::IncomingCall => {
113131
let call = self.load_call_by_root_id(call_or_child_id).await?;
114132
self.emit_msgs_changed(call.msg.chat_id, call_or_child_id);
115-
if call.incoming {
133+
if call.incoming && !call.is_stale_call() {
116134
self.emit_event(EventType::IncomingCall {
117135
msg_id: call.msg.id,
118136
});
@@ -399,4 +417,29 @@ mod tests {
399417

400418
Ok(())
401419
}
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+
}
402445
}

0 commit comments

Comments
 (0)