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