BlueGit

ict rusty-teams

Files

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

ring.rs

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