BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / target.rs

target.rs

23a3899b History
target.rs 3.9 KiB Download Raw
1use calling::meeting::LiveMeeting;
2use store::{ChatRecord, is_bot_id};
3
4use crate::data::{Person, is_one_on_one, others};
5
6const ORGID_PREFIX: &str = "8:orgid:";
7const GROUP_KIND: &str = "group";
8
9pub struct CallPlan {
10 pub title: String,
11 pub callees: Vec<(String, String)>,
12}
13
14pub fn plan_for_chat(chat: &ChatRecord, me: Option<&Person>) -> Option<CallPlan> {
15 if !is_one_on_one(chat) && !chat.kind.eq_ignore_ascii_case(GROUP_KIND) {
16 return None;
17 }
18 let callees: Vec<(String, String)> = others(chat, me)
19 .into_iter()
20 .filter(|(user_id, _)| !user_id.as_deref().is_some_and(is_bot_id))
21 .filter_map(|(user_id, name)| Some((format!("{ORGID_PREFIX}{}", user_id?), name)))
22 .collect();
23 let title = match callees.as_slice() {
24 [] => return None,
25 [(_, name)] if is_one_on_one(chat) => name.clone(),
26 _ => crate::app_state::chat_title(chat),
27 };
28 Some(CallPlan { title, callees })
29}
30
31pub fn is_organizer(meeting: &LiveMeeting, me: Option<&Person>) -> bool {
32 match (meeting.organizer_id.as_deref(), me) {
33 (Some(organizer), Some(me)) => organizer.eq_ignore_ascii_case(&me.user_id),
34 _ => false,
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use store::MemberRecord;
41
42 use super::*;
43
44 fn chat(kind: &str, title: &str, members: &[(Option<&str>, &str)]) -> ChatRecord {
45 ChatRecord {
46 id: "19:chat".into(),
47 kind: kind.into(),
48 title: title.into(),
49 members: members
50 .iter()
51 .map(|(user_id, name)| MemberRecord {
52 user_id: user_id.map(str::to_owned),
53 display_name: (*name).to_owned(),
54 })
55 .collect(),
56 ..Default::default()
57 }
58 }
59
60 fn me() -> Person {
61 Person {
62 user_id: "me-id".into(),
63 display_name: "Me".into(),
64 }
65 }
66
67 #[test]
68 fn a_one_on_one_chat_calls_the_other_person_by_name() {
69 let chat = chat(
70 "oneOnOne",
71 "",
72 &[(Some("me-id"), "Me"), (Some("bea-id"), "Bea")],
73 );
74 let plan = plan_for_chat(&chat, Some(&me())).unwrap();
75 assert_eq!(plan.title, "Bea");
76 assert_eq!(
77 plan.callees,
78 vec![("8:orgid:bea-id".to_owned(), "Bea".to_owned())]
79 );
80 }
81
82 #[test]
83 fn a_group_chat_calls_everybody_else_under_the_chat_title() {
84 let chat = chat(
85 "group",
86 "Retro team",
87 &[
88 (Some("me-id"), "Me"),
89 (Some("bea-id"), "Bea"),
90 (Some("cy-id"), "Cy"),
91 ],
92 );
93 let plan = plan_for_chat(&chat, Some(&me())).unwrap();
94 assert_eq!(plan.title, "Retro team");
95 assert_eq!(plan.callees.len(), 2);
96 }
97
98 #[test]
99 fn meeting_chats_and_chats_without_callable_members_have_no_call_button() {
100 let meeting = chat("meeting", "Standup", &[(Some("bea-id"), "Bea")]);
101 assert!(plan_for_chat(&meeting, Some(&me())).is_none());
102 let alone = chat("oneOnOne", "Notes", &[(Some("me-id"), "Me")]);
103 assert!(plan_for_chat(&alone, Some(&me())).is_none());
104 let unknown_ids = chat("group", "Guests", &[(None, "Guest")]);
105 assert!(plan_for_chat(&unknown_ids, Some(&me())).is_none());
106 }
107
108 #[test]
109 fn the_organizer_may_end_the_meeting() {
110 let meeting = LiveMeeting {
111 thread_id: "t".into(),
112 conversation_url: None,
113 expiration: None,
114 organizer_id: Some("ME-ID".into()),
115 tenant_id: None,
116 meeting_code: None,
117 passcode: None,
118 };
119 assert!(is_organizer(&meeting, Some(&me())));
120 let other = LiveMeeting {
121 organizer_id: Some("someone".into()),
122 ..meeting.clone()
123 };
124 assert!(!is_organizer(&other, Some(&me())));
125 assert!(!is_organizer(&meeting, None));
126 }
127}