BlueGit

ict rusty-teams

Files

rusty-teams / crates / session / src / transport.rs

transport.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
transport.rs 4.6 KiB Download Raw
1use std::future::Future;
2use std::pin::Pin;
3use std::sync::atomic::{AtomicBool, Ordering};
4
5use serde_json::json;
6
7use crate::app::{App, find_app_tab, has_login_tab};
8use crate::error::Result;
9use crate::events::{TabControl, TabEvents, open};
10use crate::http::{list_targets, new_tab};
11
12pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
13pub type OpenedTab = (TabControl, Option<TabEvents>);
14
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
16pub struct Diagnosis {
17 pub login_pending: bool,
18 pub app_tab_open: bool,
19}
20
21/// Where the signed-in Teams and Outlook web apps live: a debug port or an embedded webview.
22pub trait Transport: Send + Sync {
23 fn check(&self) -> BoxFuture<'_, Result<()>>;
24
25 /// `None` when the app has no tab. Without `with_events` the tab's protocol events are dropped.
26 fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>>;
27
28 /// Reloads the app so it fetches tokens again.
29 fn wake(&self, app: App) -> BoxFuture<'_, Result<()>>;
30
31 fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>>;
32
33 /// While held the host must not navigate the app on its own (no parking).
34 fn hold(&self, _app: App, _held: bool) {}
35}
36
37pub struct CdpTransport {
38 endpoint: String,
39 outlook_requested: AtomicBool,
40}
41
42impl CdpTransport {
43 pub fn new(endpoint: &str) -> Self {
44 CdpTransport {
45 endpoint: endpoint.trim_end_matches('/').to_owned(),
46 outlook_requested: AtomicBool::new(false),
47 }
48 }
49}
50
51/// Some tenants grant the Teams web client no Graph chat scopes; Outlook on the web has them and signs in silently next to a signed-in Teams.
52fn wants_outlook_tab(targets: &[crate::app::Target], app: App) -> bool {
53 app == App::Outlook && find_app_tab(targets, App::Teams).is_some() && !has_login_tab(targets)
54}
55
56impl Transport for CdpTransport {
57 fn check(&self) -> BoxFuture<'_, Result<()>> {
58 Box::pin(async move { list_targets(&self.endpoint).await.map(drop) })
59 }
60
61 fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>> {
62 Box::pin(async move {
63 let targets = list_targets(&self.endpoint).await?;
64 let Some(websocket_url) =
65 find_app_tab(&targets, app).and_then(|target| target.websocket_url.clone())
66 else {
67 if wants_outlook_tab(&targets, app)
68 && !self.outlook_requested.swap(true, Ordering::SeqCst)
69 {
70 new_tab(&self.endpoint, App::Outlook.start_url()).await?;
71 }
72 return Ok(None);
73 };
74 open(&websocket_url, with_events).await.map(Some)
75 })
76 }
77
78 fn wake(&self, app: App) -> BoxFuture<'_, Result<()>> {
79 Box::pin(async move {
80 let targets = list_targets(&self.endpoint).await?;
81 let Some(target) = find_app_tab(&targets, app) else {
82 return Ok(());
83 };
84 let parked = app.is_park_url(&target.url);
85 let (control, _) =
86 open(target.websocket_url.as_deref().unwrap_or_default(), false).await?;
87 if parked {
88 control
89 .call("Page.navigate", json!({"url": app.start_url()}))
90 .await?;
91 } else {
92 control
93 .call("Page.reload", json!({"ignoreCache": false}))
94 .await?;
95 }
96 Ok(())
97 })
98 }
99
100 fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>> {
101 Box::pin(async move {
102 let targets = list_targets(&self.endpoint).await?;
103 Ok(Diagnosis {
104 login_pending: has_login_tab(&targets),
105 app_tab_open: App::ALL
106 .iter()
107 .any(|&app| find_app_tab(&targets, app).is_some()),
108 })
109 })
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116 use crate::app::Target;
117
118 fn page(url: &str) -> Target {
119 Target {
120 kind: "page".to_owned(),
121 url: url.to_owned(),
122 websocket_url: Some("ws://tab".to_owned()),
123 }
124 }
125
126 #[test]
127 fn outlook_opens_only_next_to_a_signed_in_teams_tab() {
128 let teams = page("https://teams.cloud.microsoft/");
129 let login = page("https://login.microsoftonline.com/common/oauth2");
130 assert!(wants_outlook_tab(&[teams.clone()], App::Outlook));
131 assert!(!wants_outlook_tab(&[teams.clone()], App::Teams));
132 assert!(!wants_outlook_tab(&[], App::Outlook));
133 assert!(!wants_outlook_tab(&[teams, login], App::Outlook));
134 }
135}