BlueGit

ict rusty-teams

Files

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

web_call.rs

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