BlueGit

ict rusty-teams

Files

rusty-teams / crates / webview / examples / probe.rs

probe.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
probe.rs 3.8 KiB Download Raw
1#[cfg(not(windows))]
2fn main() {
3 eprintln!("probe runs on Windows only");
4 std::process::exit(2);
5}
6
7#[cfg(windows)]
8use std::path::PathBuf;
9#[cfg(windows)]
10use std::time::{Duration, Instant};
11
12#[cfg(windows)]
13use session::{App, Method, Scope, Session, SessionConfig};
14
15#[cfg(windows)]
16#[tokio::main]
17async fn main() {
18 let folder = std::env::args()
19 .nth(1)
20 .map(PathBuf::from)
21 .expect("usage: probe <user-data-folder> [hold-seconds]");
22 let hold = std::env::args()
23 .nth(2)
24 .and_then(|value| value.parse().ok())
25 .unwrap_or(0);
26 let started = Instant::now();
27 let transport = webview::start(webview::HostConfig {
28 user_data_folder: folder,
29 window_title: "Rusty Teams probe - Sign in".into(),
30 log: |line| println!("{line}"),
31 });
32 let session = match Session::with_transport(transport, SessionConfig::default()).await {
33 Ok(session) => session,
34 Err(error) => return println!("connect failed after {:?}: {error}", started.elapsed()),
35 };
36 println!("webviews ready after {:?}", started.elapsed());
37 graph_calls(&session, "loaded").await;
38 presence_call(&session, "loaded").await;
39 match session.subscribe(App::Teams).await {
40 Ok((control, _events)) => match control.enable_events().await {
41 Ok(()) => println!("teams events: enabled"),
42 Err(error) => println!("teams events: {error}"),
43 },
44 Err(error) => println!("teams subscribe: {error}"),
45 }
46 println!("holding {hold} s");
47 tokio::time::sleep(Duration::from_secs(hold)).await;
48 graph_calls(&session, "after hold").await;
49 presence_call(&session, "after hold").await;
50}
51
52#[cfg(windows)]
53async fn presence_call(session: &Session, phase: &str) {
54 let me = match session
55 .request(
56 Method::Get,
57 "https://graph.microsoft.com/v1.0/me?$select=id",
58 &Scope::graph("User.Read"),
59 None,
60 )
61 .await
62 {
63 Ok(response) => response.body["id"].as_str().unwrap_or_default().to_owned(),
64 Err(error) => return println!("{phase} presence: no own id: {error}"),
65 };
66 let call = Instant::now();
67 let body = serde_json::json!([{"mri": format!("8:orgid:{me}")}]);
68 let scope = Scope::new(session::PRESENCE, "user_impersonation");
69 match session
70 .request(
71 Method::Post,
72 "https://presence.teams.microsoft.com/v1/presence/getpresence/",
73 &scope,
74 Some(body),
75 )
76 .await
77 {
78 Ok(response) => println!(
79 "{phase} presence: HTTP {} after {:?}, availability present: {}",
80 response.status,
81 call.elapsed(),
82 response.body.pointer("/0/presence/availability").is_some()
83 ),
84 Err(error) => println!(
85 "{phase} presence: failed after {:?}: {}",
86 call.elapsed(),
87 error.to_string().chars().take(200).collect::<String>()
88 ),
89 }
90}
91
92#[cfg(windows)]
93async fn graph_calls(session: &Session, phase: &str) {
94 for (label, url, scope) in [
95 (
96 "graph me",
97 "https://graph.microsoft.com/v1.0/me?$select=id",
98 Scope::graph("User.Read"),
99 ),
100 (
101 "graph chats",
102 "https://graph.microsoft.com/v1.0/me/chats?$top=1&$select=id",
103 Scope::graph("Chat.Read"),
104 ),
105 ] {
106 let call = Instant::now();
107 match session.request(Method::Get, url, &scope, None).await {
108 Ok(response) => println!(
109 "{phase} {label}: HTTP {} after {:?}",
110 response.status,
111 call.elapsed()
112 ),
113 Err(error) => println!(
114 "{phase} {label}: failed after {:?}: {}",
115 call.elapsed(),
116 error.to_string().chars().take(160).collect::<String>()
117 ),
118 }
119 }
120}