| 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 | /// Teams starts the Echo test call from the Echo chat's call buttons. |
| 35 | pub fn is_echo_chat(chat: &ChatRecord) -> bool { |
| 36 | let echo_id = calling::signaling::ECHO_BOT_MRI; |
| 37 | let bare_id = echo_id.trim_start_matches("28:"); |
| 38 | is_one_on_one(chat) |
| 39 | && (chat.id.contains(bare_id) |
| 40 | || chat |
| 41 | .members |
| 42 | .iter() |
| 43 | .filter_map(|member| member.user_id.as_deref()) |
| 44 | .any(|user_id| user_id == echo_id || user_id == bare_id)) |
| 45 | } |
| 46 | |
| 47 | pub fn is_organizer(meeting: &LiveMeeting, me: Option<&Person>) -> bool { |
| 48 | match (meeting.organizer_id.as_deref(), me) { |
| 49 | (Some(organizer), Some(me)) => organizer.eq_ignore_ascii_case(&me.user_id), |
| 50 | _ => false, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[cfg(test)] |
| 55 | mod tests { |
| 56 | use store::MemberRecord; |
| 57 | |
| 58 | use super::*; |
| 59 | |
| 60 | fn chat(kind: &str, title: &str, members: &[(Option<&str>, &str)]) -> ChatRecord { |
| 61 | ChatRecord { |
| 62 | id: "19:chat".into(), |
| 63 | kind: kind.into(), |
| 64 | title: title.into(), |
| 65 | members: members |
| 66 | .iter() |
| 67 | .map(|(user_id, name)| MemberRecord { |
| 68 | user_id: user_id.map(str::to_owned), |
| 69 | display_name: (*name).to_owned(), |
| 70 | }) |
| 71 | .collect(), |
| 72 | ..Default::default() |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn me() -> Person { |
| 77 | Person { |
| 78 | user_id: "me-id".into(), |
| 79 | display_name: "Me".into(), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn a_one_on_one_chat_calls_the_other_person_by_name() { |
| 85 | let chat = chat( |
| 86 | "oneOnOne", |
| 87 | "", |
| 88 | &[(Some("me-id"), "Me"), (Some("bea-id"), "Bea")], |
| 89 | ); |
| 90 | let plan = plan_for_chat(&chat, Some(&me())).unwrap(); |
| 91 | assert_eq!(plan.title, "Bea"); |
| 92 | assert_eq!( |
| 93 | plan.callees, |
| 94 | vec![("8:orgid:bea-id".to_owned(), "Bea".to_owned())] |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn a_group_chat_calls_everybody_else_under_the_chat_title() { |
| 100 | let chat = chat( |
| 101 | "group", |
| 102 | "Retro team", |
| 103 | &[ |
| 104 | (Some("me-id"), "Me"), |
| 105 | (Some("bea-id"), "Bea"), |
| 106 | (Some("cy-id"), "Cy"), |
| 107 | ], |
| 108 | ); |
| 109 | let plan = plan_for_chat(&chat, Some(&me())).unwrap(); |
| 110 | assert_eq!(plan.title, "Retro team"); |
| 111 | assert_eq!(plan.callees.len(), 2); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn meeting_chats_and_chats_without_callable_members_have_no_call_button() { |
| 116 | let meeting = chat("meeting", "Standup", &[(Some("bea-id"), "Bea")]); |
| 117 | assert!(plan_for_chat(&meeting, Some(&me())).is_none()); |
| 118 | let alone = chat("oneOnOne", "Notes", &[(Some("me-id"), "Me")]); |
| 119 | assert!(plan_for_chat(&alone, Some(&me())).is_none()); |
| 120 | let unknown_ids = chat("group", "Guests", &[(None, "Guest")]); |
| 121 | assert!(plan_for_chat(&unknown_ids, Some(&me())).is_none()); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn the_notes_chat_has_no_call_button() { |
| 126 | let mut notes = chat("oneOnOne", "Me (You)", &[(Some("me-id"), "Me")]); |
| 127 | notes.id = teams_core::NOTES_CHAT_ID.into(); |
| 128 | assert!(plan_for_chat(¬es, Some(&me())).is_none()); |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn the_echo_chat_is_recognised_by_its_id_or_its_bot_member() { |
| 133 | let mut echo = chat( |
| 134 | "oneOnOne", |
| 135 | "Echo", |
| 136 | &[ |
| 137 | (Some("me-id"), "Me"), |
| 138 | (Some(calling::signaling::ECHO_BOT_MRI), "Echo"), |
| 139 | ], |
| 140 | ); |
| 141 | assert!(is_echo_chat(&echo)); |
| 142 | echo.members.truncate(1); |
| 143 | assert!(!is_echo_chat(&echo)); |
| 144 | echo.id = "19:me-id_cf28171e-fcfd-47e4-a1d6-79460b0b3ca0@unq.gbl.spaces".into(); |
| 145 | assert!(is_echo_chat(&echo)); |
| 146 | let other = chat( |
| 147 | "oneOnOne", |
| 148 | "", |
| 149 | &[(Some("me-id"), "Me"), (Some("bea-id"), "Bea")], |
| 150 | ); |
| 151 | assert!(!is_echo_chat(&other)); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn the_organizer_may_end_the_meeting() { |
| 156 | let meeting = LiveMeeting { |
| 157 | thread_id: "t".into(), |
| 158 | conversation_url: None, |
| 159 | expiration: None, |
| 160 | organizer_id: Some("ME-ID".into()), |
| 161 | tenant_id: None, |
| 162 | meeting_code: None, |
| 163 | passcode: None, |
| 164 | }; |
| 165 | assert!(is_organizer(&meeting, Some(&me()))); |
| 166 | let other = LiveMeeting { |
| 167 | organizer_id: Some("someone".into()), |
| 168 | ..meeting.clone() |
| 169 | }; |
| 170 | assert!(!is_organizer(&other, Some(&me()))); |
| 171 | assert!(!is_organizer(&meeting, None)); |
| 172 | } |
| 173 | } |