BlueGit

ict rusty-teams

Files

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

ring.rs

ring.rs 7.1 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 video 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 { "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 first_ringing_id(&self) -> Option<u64> {
77 self.ringing().next().map(|entry| entry.ring.ring_id)
78 }
79
80 pub fn is_ringing(&self) -> bool {
81 self.ringing().next().is_some()
82 }
83
84 pub fn is_empty(&self) -> bool {
85 self.entries.is_empty()
86 }
87
88 pub fn signal(&mut self, ring_id: u64, signal: RingSignal, now: Instant) -> Option<RingOutcome> {
89 let index = self.entries.iter().position(|entry| entry.ring.ring_id == ring_id)?;
90 let next = self.entries[index].state.next(signal, now);
91 self.entries[index].state = next;
92 self.settle(index)
93 }
94
95 pub fn tick(&mut self, now: Instant) -> Vec<(u64, RingOutcome)> {
96 let mut outcomes = Vec::new();
97 let ids: Vec<u64> = self.entries.iter().map(|entry| entry.ring.ring_id).collect();
98 for ring_id in ids {
99 if let Some(outcome) = self.signal(ring_id, RingSignal::Tick, now) {
100 outcomes.push((ring_id, outcome));
101 }
102 }
103 outcomes
104 }
105
106 fn settle(&mut self, index: usize) -> Option<RingOutcome> {
107 let entry = &self.entries[index];
108 if entry.state.is_ringing() {
109 return None;
110 }
111 let outcome = if entry.state.is_missed() {
112 RingOutcome::Missed(MissedCall {
113 caller_mri: entry.ring.caller.mri.clone(),
114 caller_name: entry.ring.caller.display_name.clone(),
115 thread_id: entry.ring.thread_id.clone(),
116 at: Utc::now(),
117 })
118 } else {
119 RingOutcome::Gone
120 };
121 self.entries.remove(index);
122 Some(outcome)
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use std::time::Duration;
129
130 use calling::{Caller, EndKind};
131
132 use super::*;
133
134 fn ring(ring_id: u64, video: bool) -> IncomingRing {
135 IncomingRing {
136 ring_id,
137 caller: Caller {
138 mri: "8:orgid:caller".into(),
139 display_name: "Cara".into(),
140 },
141 thread_id: Some("19:a_b@unq.gbl.spaces".into()),
142 video,
143 is_group: false,
144 subject: None,
145 }
146 }
147
148 #[test]
149 fn a_ring_stays_until_it_is_settled() {
150 let start = Instant::now();
151 let mut rings = Rings::default();
152 rings.push(ring(1, false), false, start);
153 assert!(rings.is_ringing());
154 assert!(rings.tick(start + Duration::from_secs(5)).is_empty());
155 assert!(rings.get(1).is_some());
156 }
157
158 #[test]
159 fn thirty_seconds_without_an_answer_make_a_missed_call() {
160 let start = Instant::now();
161 let mut rings = Rings::default();
162 rings.push(ring(1, false), false, start);
163 let outcomes = rings.tick(start + Duration::from_secs(30));
164 assert_eq!(outcomes.len(), 1);
165 let (ring_id, RingOutcome::Missed(missed)) = &outcomes[0] else { panic!("not missed") };
166 assert_eq!(*ring_id, 1);
167 assert_eq!(missed.caller_name, "Cara");
168 assert_eq!(missed.thread_id.as_deref(), Some("19:a_b@unq.gbl.spaces"));
169 assert!(rings.is_empty() && !rings.is_ringing());
170 }
171
172 #[test]
173 fn the_caller_hanging_up_is_missed_but_answering_elsewhere_is_not() {
174 let start = Instant::now();
175 let mut rings = Rings::default();
176 rings.push(ring(1, false), false, start);
177 rings.push(ring(2, false), false, start);
178 let cancelled = rings.signal(1, RingSignal::Remote(EndKind::Cancelled), start);
179 assert!(matches!(cancelled, Some(RingOutcome::Missed(_))));
180 let elsewhere = rings.signal(2, RingSignal::Remote(EndKind::AnsweredElsewhere), start);
181 assert_eq!(elsewhere, Some(RingOutcome::Gone));
182 }
183
184 #[test]
185 fn accept_and_decline_leave_no_missed_call() {
186 let start = Instant::now();
187 let mut rings = Rings::default();
188 rings.push(ring(1, false), false, start);
189 rings.push(ring(2, false), false, start);
190 assert_eq!(rings.signal(1, RingSignal::Accept, start), Some(RingOutcome::Gone));
191 assert_eq!(rings.signal(2, RingSignal::Decline, start), Some(RingOutcome::Gone));
192 assert!(rings.is_empty());
193 assert_eq!(rings.signal(1, RingSignal::Accept, start), None);
194 }
195
196 #[test]
197 fn a_ring_id_rings_only_once() {
198 let start = Instant::now();
199 let mut rings = Rings::default();
200 rings.push(ring(1, false), false, start);
201 rings.push(ring(1, false), false, start);
202 assert_eq!(rings.ringing().count(), 1);
203 }
204
205 #[test]
206 fn video_calls_offer_audio_and_video_answers() {
207 let audio = RingEntry { ring: ring(1, false), state: RingState::start(Instant::now()), demo: false };
208 let video = RingEntry { ring: ring(2, true), ..audio.clone() };
209 assert_eq!(audio.subtitle(), "Incoming audio call");
210 assert_eq!(audio.accept_label(), "Accept");
211 assert_eq!(video.subtitle(), "Incoming video call");
212 assert_eq!(video.accept_label(), "Audio");
213 assert_eq!(audio.caller_user_id().as_deref(), Some("caller"));
214 }
215
216 #[test]
217 fn group_rings_say_so() {
218 let mut group = ring(3, false);
219 group.is_group = true;
220 let entry = RingEntry { ring: group, state: RingState::start(Instant::now()), demo: false };
221 assert_eq!(entry.subtitle(), "Incoming group audio call");
222 }
223}