BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / ring.rs

ring.rs

ring.rs 6.9 KiB Download Raw
1use std::time::Instant;
2
3use calling::{IncomingRing, RingSignal, RingState};
4use chrono::{DateTime, Utc};
5
6const ORGID_PREFIX: &str = "8:orgid:";
7
8#[derive(Debug, Clone)]
9pub struct RingEntry {
10 pub ring: IncomingRing,
11 pub state: RingState,
12 pub demo: bool,
13}
14
15impl RingEntry {
16 pub fn caller_user_id(&self) -> Option<String> {
17 self.ring.caller.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned)
18 }
19
20 pub fn caller_name(&self) -> &str {
21 &self.ring.caller.display_name
22 }
23
24 pub fn subtitle(&self) -> &'static str {
25 match (self.ring.video, self.ring.is_group) {
26 (true, _) => "Incoming call",
27 (false, true) => "Incoming group audio call",
28 (false, false) => "Incoming audio call",
29 }
30 }
31
32 pub fn accept_label(&self) -> &'static str {
33 if self.ring.video { "Accept with audio" } else { "Accept" }
34 }
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct MissedCall {
39 pub caller_mri: String,
40 pub caller_name: String,
41 pub thread_id: Option<String>,
42 pub at: DateTime<Utc>,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum RingOutcome {
47 Missed(MissedCall),
48 Gone,
49}
50
51#[derive(Default)]
52pub struct Rings {
53 entries: Vec<RingEntry>,
54}
55
56impl Rings {
57 pub fn push(&mut self, ring: IncomingRing, demo: bool, now: Instant) {
58 if self.entries.iter().any(|entry| entry.ring.ring_id == ring.ring_id) {
59 return;
60 }
61 self.entries.push(RingEntry {
62 ring,
63 state: RingState::start(now),
64 demo,
65 });
66 }
67
68 pub fn get(&self, ring_id: u64) -> Option<&RingEntry> {
69 self.entries.iter().find(|entry| entry.ring.ring_id == ring_id)
70 }
71
72 pub fn ringing(&self) -> impl Iterator<Item = &RingEntry> {
73 self.entries.iter().filter(|entry| entry.state.is_ringing())
74 }
75
76 pub fn is_ringing(&self) -> bool {
77 self.ringing().next().is_some()
78 }
79
80 pub fn is_empty(&self) -> bool {
81 self.entries.is_empty()
82 }
83
84 pub fn signal(&mut self, ring_id: u64, signal: RingSignal, now: Instant) -> Option<RingOutcome> {
85 let index = self.entries.iter().position(|entry| entry.ring.ring_id == ring_id)?;
86 let next = self.entries[index].state.next(signal, now);
87 self.entries[index].state = next;
88 self.settle(index)
89 }
90
91 pub fn tick(&mut self, now: Instant) -> Vec<(u64, RingOutcome)> {
92 let mut outcomes = Vec::new();
93 let ids: Vec<u64> = self.entries.iter().map(|entry| entry.ring.ring_id).collect();
94 for ring_id in ids {
95 if let Some(outcome) = self.signal(ring_id, RingSignal::Tick, now) {
96 outcomes.push((ring_id, outcome));
97 }
98 }
99 outcomes
100 }
101
102 fn settle(&mut self, index: usize) -> Option<RingOutcome> {
103 let entry = &self.entries[index];
104 if entry.state.is_ringing() {
105 return None;
106 }
107 let outcome = if entry.state.is_missed() {
108 RingOutcome::Missed(MissedCall {
109 caller_mri: entry.ring.caller.mri.clone(),
110 caller_name: entry.ring.caller.display_name.clone(),
111 thread_id: entry.ring.thread_id.clone(),
112 at: Utc::now(),
113 })
114 } else {
115 RingOutcome::Gone
116 };
117 self.entries.remove(index);
118 Some(outcome)
119 }
120}
121
122#[cfg(test)]
123mod tests {
124 use std::time::Duration;
125
126 use calling::{Caller, EndKind};
127
128 use super::*;
129
130 fn ring(ring_id: u64, video: bool) -> IncomingRing {
131 IncomingRing {
132 ring_id,
133 caller: Caller {
134 mri: "8:orgid:caller".into(),
135 display_name: "Cara".into(),
136 },
137 thread_id: Some("19:a_b@unq.gbl.spaces".into()),
138 video,
139 is_group: false,
140 subject: None,
141 }
142 }
143
144 #[test]
145 fn a_ring_stays_until_it_is_settled() {
146 let start = Instant::now();
147 let mut rings = Rings::default();
148 rings.push(ring(1, false), false, start);
149 assert!(rings.is_ringing());
150 assert!(rings.tick(start + Duration::from_secs(5)).is_empty());
151 assert!(rings.get(1).is_some());
152 }
153
154 #[test]
155 fn thirty_seconds_without_an_answer_make_a_missed_call() {
156 let start = Instant::now();
157 let mut rings = Rings::default();
158 rings.push(ring(1, false), false, start);
159 let outcomes = rings.tick(start + Duration::from_secs(30));
160 assert_eq!(outcomes.len(), 1);
161 let (ring_id, RingOutcome::Missed(missed)) = &outcomes[0] else { panic!("not missed") };
162 assert_eq!(*ring_id, 1);
163 assert_eq!(missed.caller_name, "Cara");
164 assert_eq!(missed.thread_id.as_deref(), Some("19:a_b@unq.gbl.spaces"));
165 assert!(rings.is_empty() && !rings.is_ringing());
166 }
167
168 #[test]
169 fn the_caller_hanging_up_is_missed_but_answering_elsewhere_is_not() {
170 let start = Instant::now();
171 let mut rings = Rings::default();
172 rings.push(ring(1, false), false, start);
173 rings.push(ring(2, false), false, start);
174 let cancelled = rings.signal(1, RingSignal::Remote(EndKind::Cancelled), start);
175 assert!(matches!(cancelled, Some(RingOutcome::Missed(_))));
176 let elsewhere = rings.signal(2, RingSignal::Remote(EndKind::AnsweredElsewhere), start);
177 assert_eq!(elsewhere, Some(RingOutcome::Gone));
178 }
179
180 #[test]
181 fn accept_and_decline_leave_no_missed_call() {
182 let start = Instant::now();
183 let mut rings = Rings::default();
184 rings.push(ring(1, false), false, start);
185 rings.push(ring(2, false), false, start);
186 assert_eq!(rings.signal(1, RingSignal::Accept, start), Some(RingOutcome::Gone));
187 assert_eq!(rings.signal(2, RingSignal::Decline, start), Some(RingOutcome::Gone));
188 assert!(rings.is_empty());
189 assert_eq!(rings.signal(1, RingSignal::Accept, start), None);
190 }
191
192 #[test]
193 fn a_ring_id_rings_only_once() {
194 let start = Instant::now();
195 let mut rings = Rings::default();
196 rings.push(ring(1, false), false, start);
197 rings.push(ring(1, false), false, start);
198 assert_eq!(rings.ringing().count(), 1);
199 }
200
201 #[test]
202 fn video_calls_offer_accept_with_audio() {
203 let audio = RingEntry { ring: ring(1, false), state: RingState::start(Instant::now()), demo: false };
204 let video = RingEntry { ring: ring(2, true), ..audio.clone() };
205 assert_eq!(audio.subtitle(), "Incoming audio call");
206 assert_eq!(audio.accept_label(), "Accept");
207 assert_eq!(video.subtitle(), "Incoming call");
208 assert_eq!(video.accept_label(), "Accept with audio");
209 assert_eq!(audio.caller_user_id().as_deref(), Some("caller"));
210 }
211
212 #[test]
213 fn group_rings_say_so() {
214 let mut group = ring(3, false);
215 group.is_group = true;
216 let entry = RingEntry { ring: group, state: RingState::start(Instant::now()), demo: false };
217 assert_eq!(entry.subtitle(), "Incoming group audio call");
218 }
219}