| 1 | use calling::MeetingTarget; |
| 2 | use gpui_kit::*; |
| 3 | use serde_json::json; |
| 4 | |
| 5 | use crate::app_state::AppState; |
| 6 | |
| 7 | pub const META_WEB_CALLS: &str = "calls.teams_web"; |
| 8 | const TEAMS_WEB: &str = "https://teams.microsoft.com/l"; |
| 9 | const WEB_CALL_TITLE: &str = "Teams call"; |
| 10 | |
| 11 | pub fn stored_web_calls(store: &store::Store) -> bool { |
| 12 | store.meta(META_WEB_CALLS).ok().flatten().as_deref() == Some("true") |
| 13 | } |
| 14 | |
| 15 | pub fn meeting_web_url(target: &MeetingTarget) -> String { |
| 16 | let context = json!({"Tid": target.tenant_id, "Oid": target.organizer_id}).to_string(); |
| 17 | format!( |
| 18 | "{TEAMS_WEB}/meetup-join/{}/0?context={}", |
| 19 | encode(&target.thread_id), |
| 20 | encode(&context) |
| 21 | ) |
| 22 | } |
| 23 | |
| 24 | /// Teams on the web has no call link for a chat id, so the chat opens and the call starts there. |
| 25 | pub fn chat_web_url(thread_id: &str) -> String { |
| 26 | format!("{TEAMS_WEB}/chat/{}/0", encode(thread_id)) |
| 27 | } |
| 28 | |
| 29 | fn encode(text: &str) -> String { |
| 30 | text.bytes() |
| 31 | .map(|byte| match byte { |
| 32 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => { |
| 33 | (byte as char).to_string() |
| 34 | } |
| 35 | byte => format!("%{byte:02X}"), |
| 36 | }) |
| 37 | .collect() |
| 38 | } |
| 39 | |
| 40 | impl AppState { |
| 41 | pub fn set_web_calls(&mut self, enabled: bool, cx: &mut Context<Self>) { |
| 42 | self.web_calls = enabled; |
| 43 | let _ = self |
| 44 | .store |
| 45 | .set_meta(META_WEB_CALLS, if enabled { "true" } else { "false" }); |
| 46 | cx.notify(); |
| 47 | } |
| 48 | |
| 49 | /// Opens the call in Teams on the web when that setting is on; false leaves it to the own engine. |
| 50 | pub(super) fn try_web_call(&mut self, url: String, cx: &mut Context<Self>) -> bool { |
| 51 | if !self.web_calls || self.mode.demo { |
| 52 | return false; |
| 53 | } |
| 54 | open_web_call(url, cx) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[cfg(windows)] |
| 59 | fn open_web_call(url: String, cx: &mut App) -> bool { |
| 60 | crate::task_dialog::hosted::open_call_window(url, WEB_CALL_TITLE.to_owned(), cx) |
| 61 | } |
| 62 | |
| 63 | #[cfg(not(windows))] |
| 64 | fn open_web_call(url: String, cx: &mut App) -> bool { |
| 65 | let _ = WEB_CALL_TITLE; |
| 66 | cx.open_url(&url); |
| 67 | true |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use calling::MeetingTarget; |
| 73 | |
| 74 | use super::{chat_web_url, meeting_web_url}; |
| 75 | |
| 76 | #[test] |
| 77 | fn a_meeting_opens_the_teams_join_link_with_tenant_and_organizer() { |
| 78 | let target = MeetingTarget { |
| 79 | thread_id: "19:meeting_abc@thread.v2".to_owned(), |
| 80 | tenant_id: "tenant-1".to_owned(), |
| 81 | organizer_id: "organizer-1".to_owned(), |
| 82 | meeting_data: None, |
| 83 | }; |
| 84 | assert_eq!( |
| 85 | meeting_web_url(&target), |
| 86 | "https://teams.microsoft.com/l/meetup-join/19%3Ameeting_abc%40thread.v2/0?context=%7B%22Tid%22%3A%22tenant-1%22%2C%22Oid%22%3A%22organizer-1%22%7D" |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | #[test] |
| 91 | fn a_chat_opens_its_teams_chat_link() { |
| 92 | assert_eq!( |
| 93 | chat_web_url("19:abc@thread.v2"), |
| 94 | "https://teams.microsoft.com/l/chat/19%3Aabc%40thread.v2/0" |
| 95 | ); |
| 96 | } |
| 97 | } |