BlueGit

ict rusty-teams

Files

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

end_screen.rs

end_screen.rs 6.8 KiB Download Raw
1use calling::{EndKind, EndReason, MeetingTarget};
2use chatsvc::sounds::CallTone;
3
4use super::model::{ActiveCall, CallKind};
5
6const ORGID_PREFIX: &str = "8:orgid:";
7pub const DISMISS_LABEL: &str = "Dismiss";
8
9pub fn end_tone(kind: CallKind, moving_rooms: bool) -> Option<CallTone> {
10 if moving_rooms {
11 return None;
12 }
13 Some(match kind {
14 CallKind::Meeting => CallTone::MeetingLeft,
15 CallKind::Test | CallKind::Direct | CallKind::Group => CallTone::CallEnded,
16 })
17}
18
19pub fn join_tone(kind: CallKind) -> Option<CallTone> {
20 (kind == CallKind::Meeting).then_some(CallTone::MeetingJoined)
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum EndCause {
25 Removed,
26 Dropped,
27}
28
29pub fn end_cause(reason: &EndReason, kind: CallKind) -> Option<EndCause> {
30 match reason {
31 EndReason::Dropped => Some(EndCause::Dropped),
32 EndReason::Remote(EndKind::Removed) if kind == CallKind::Meeting => Some(EndCause::Removed),
33 _ => None,
34 }
35}
36
37#[derive(Debug, Clone, PartialEq)]
38pub struct EndPeer {
39 pub user_id: Option<String>,
40 pub name: String,
41}
42
43#[derive(Debug, Clone, PartialEq)]
44pub struct CallEndScreen {
45 pub cause: EndCause,
46 pub kind: CallKind,
47 pub title: String,
48 pub conversation_id: Option<String>,
49 pub meeting_target: Option<MeetingTarget>,
50 pub peer: Option<EndPeer>,
51}
52
53impl CallEndScreen {
54 pub fn for_call(call: &ActiveCall, reason: &EndReason) -> Option<Self> {
55 let model = &call.model;
56 let cause = end_cause(reason, model.kind)?;
57 let peer = (model.kind == CallKind::Direct)
58 .then(|| model.tiles.first())
59 .flatten()
60 .map(|tile| EndPeer {
61 user_id: tile
62 .user_id
63 .clone()
64 .or_else(|| tile.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned)),
65 name: tile.name.clone(),
66 });
67 Some(CallEndScreen {
68 cause,
69 kind: model.kind,
70 title: model.title.clone(),
71 conversation_id: call.conversation_id.clone(),
72 meeting_target: call.meeting_target.clone(),
73 peer,
74 })
75 }
76
77 fn is_two_party(&self) -> bool {
78 matches!(self.kind, CallKind::Direct | CallKind::Test)
79 }
80
81 pub fn heading(&self) -> &'static str {
82 match (self.cause, self.kind) {
83 (EndCause::Removed, CallKind::Meeting) => "Removed from the meeting",
84 (EndCause::Removed, _) => "Removed from the call",
85 (EndCause::Dropped, _) => "Call dropped",
86 }
87 }
88
89 pub fn description(&self) -> &'static str {
90 match self.cause {
91 EndCause::Removed if self.kind == CallKind::Meeting => {
92 "Someone just removed you from the meeting. You can try to rejoin."
93 }
94 EndCause::Removed => "Someone just removed you from the call. You can try to rejoin.",
95 EndCause::Dropped if self.is_two_party() => {
96 "Sorry we couldn't reconnect your call. Make sure you have an internet connection and try calling back."
97 }
98 EndCause::Dropped => {
99 "Sorry we couldn't get you back into the meeting. Make sure you have an internet connection and try to rejoin."
100 }
101 }
102 }
103
104 pub fn retry_label(&self) -> &'static str {
105 match self.cause {
106 EndCause::Dropped if self.is_two_party() => "Call back",
107 EndCause::Removed | EndCause::Dropped => "Rejoin",
108 }
109 }
110
111 pub fn can_retry(&self) -> bool {
112 match self.kind {
113 CallKind::Test => true,
114 CallKind::Meeting => self.meeting_target.is_some(),
115 CallKind::Direct | CallKind::Group => self.conversation_id.is_some(),
116 }
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 fn screen(cause: EndCause, kind: CallKind) -> CallEndScreen {
125 CallEndScreen {
126 cause,
127 kind,
128 title: "Standup".to_owned(),
129 conversation_id: Some("19:chat".to_owned()),
130 meeting_target: None,
131 peer: None,
132 }
133 }
134
135 #[test]
136 fn meetings_leave_with_the_meetup_tone_and_calls_with_the_call_tone() {
137 assert_eq!(
138 end_tone(CallKind::Meeting, false),
139 Some(CallTone::MeetingLeft)
140 );
141 assert_eq!(end_tone(CallKind::Direct, false), Some(CallTone::CallEnded));
142 assert_eq!(end_tone(CallKind::Group, false), Some(CallTone::CallEnded));
143 assert_eq!(end_tone(CallKind::Test, false), Some(CallTone::CallEnded));
144 assert_eq!(end_tone(CallKind::Meeting, true), None);
145 }
146
147 #[test]
148 fn only_meetings_play_the_join_tone() {
149 assert_eq!(join_tone(CallKind::Meeting), Some(CallTone::MeetingJoined));
150 assert_eq!(join_tone(CallKind::Direct), None);
151 assert_eq!(join_tone(CallKind::Group), None);
152 }
153
154 #[test]
155 fn only_drops_and_removals_from_meetings_get_an_end_screen() {
156 let removed = EndReason::Remote(EndKind::Removed);
157 assert_eq!(
158 end_cause(&EndReason::Dropped, CallKind::Direct),
159 Some(EndCause::Dropped)
160 );
161 assert_eq!(
162 end_cause(&removed, CallKind::Meeting),
163 Some(EndCause::Removed)
164 );
165 assert_eq!(end_cause(&removed, CallKind::Direct), None);
166 assert_eq!(end_cause(&EndReason::LocalHangup, CallKind::Meeting), None);
167 assert_eq!(
168 end_cause(&EndReason::Remote(EndKind::Normal), CallKind::Direct),
169 None
170 );
171 }
172
173 #[test]
174 fn texts_follow_teams_per_case() {
175 let removed = screen(EndCause::Removed, CallKind::Meeting);
176 assert_eq!(removed.heading(), "Removed from the meeting");
177 assert_eq!(removed.retry_label(), "Rejoin");
178 let dropped_call = screen(EndCause::Dropped, CallKind::Direct);
179 assert_eq!(dropped_call.heading(), "Call dropped");
180 assert_eq!(dropped_call.retry_label(), "Call back");
181 assert!(dropped_call.description().contains("try calling back"));
182 let dropped_meeting = screen(EndCause::Dropped, CallKind::Meeting);
183 assert_eq!(dropped_meeting.retry_label(), "Rejoin");
184 assert!(
185 dropped_meeting
186 .description()
187 .contains("back into the meeting")
188 );
189 }
190
191 #[test]
192 fn a_meeting_needs_its_target_to_rejoin() {
193 let mut meeting = screen(EndCause::Removed, CallKind::Meeting);
194 assert!(!meeting.can_retry());
195 meeting.meeting_target = Some(MeetingTarget {
196 thread_id: "19:meeting".to_owned(),
197 tenant_id: "tenant".to_owned(),
198 organizer_id: "organizer".to_owned(),
199 meeting_data: None,
200 });
201 assert!(meeting.can_retry());
202 assert!(screen(EndCause::Dropped, CallKind::Group).can_retry());
203 }
204}