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