BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / examples / team_admin_probe.rs

team_admin_probe.rs

team_admin_probe.rs 4.8 KiB Download Raw
1use serde_json::{Value, json};
2use session::{DEFAULT_ENDPOINT, Method, Request, SPACES, Scope, Session};
3
4const CSA_RESOURCE: &str = "https://chatsvcagg.teams.microsoft.com";
5const MIDDLE_TIER: &str = "https://teams.cloud.microsoft/api/mt/emea/beta";
6const CSA: &str = "https://teams.cloud.microsoft/api/csa/emea/api/v1";
7const PROVISIONING: &str = "https://teams.cloud.microsoft/fabric/emea/templates/api";
8const PROVISIONING_RESOURCE: &str = "https://templates.teams.microsoft.com";
9const GRAPH: &str = "https://graph.microsoft.com";
10
11#[tokio::main]
12async fn main() {
13 let arguments: Vec<String> = std::env::args().skip(1).collect();
14 let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned());
15 let session = Session::connect(&endpoint).await.expect("browser");
16 if let ["lib-create", team_thread_id, group_id, name, privacy, object_id, tenant_id] =
17 arguments.iter().map(String::as_str).collect::<Vec<_>>().as_slice()
18 {
19 let channel = chatsvc::NewChannel {
20 team_thread_id,
21 group_id,
22 name,
23 description: "",
24 privacy: if *privacy == "private" {
25 chatsvc::ChannelPrivacy::Private
26 } else {
27 chatsvc::ChannelPrivacy::Standard
28 },
29 };
30 let mailbox = chatsvc::Mailbox { object_id, tenant_id };
31 let outcome = chatsvc::TeamAdmin::new(&session)
32 .create_channel(&channel, mailbox)
33 .await;
34 println!("{outcome:?}");
35 return;
36 }
37 let (method, path, body) = match arguments.iter().map(String::as_str).collect::<Vec<_>>().as_slice() {
38 ["create-team", name] => (
39 Method::Post,
40 "teams/create".to_owned(),
41 Some(json!({"displayName": name, "description": "", "accessType": "Private"})),
42 ),
43 ["create-channel", team_thread_id, group_id, name, channel_type] => (
44 Method::Post,
45 format!("teams/{team_thread_id}/channels"),
46 Some(json!({"displayName": name, "description": "", "groupId": group_id, "channelType": channel_type})),
47 ),
48 ["delete-channel", team_thread_id, channel_id] => (
49 Method::Delete,
50 format!("teams/{team_thread_id}/channels/{channel_id}"),
51 Some(json!({})),
52 ),
53 ["delete-team", group_id] => (Method::Delete, format!("teams/{group_id}/delete"), None),
54 ["get", path] => (Method::Get, (*path).to_owned(), None),
55 ["csa-get", path] => (Method::Get, format!("csa:{path}"), None),
56 ["graph-get", path] => (Method::Get, format!("graph:{path}"), None),
57 ["create-private", group_id, team_thread_id, name] => (
58 Method::Post,
59 format!("provisioning:teams/{group_id}/channels?forceSync=false"),
60 Some(json!({"spaceThreadId": team_thread_id, "displayName": name, "description": "", "channelType": "Private", "sharedWithParentTeam": false})),
61 ),
62 ["send", method, path, body] => (
63 match *method {
64 "PATCH" => Method::Patch,
65 "DELETE" => Method::Delete,
66 "PUT" => Method::Put,
67 _ => Method::Post,
68 },
69 (*path).to_owned(),
70 Some(serde_json::from_str(body).expect("json body")),
71 ),
72 ["provisioning-get", path] => (Method::Get, format!("provisioning:{path}"), None),
73 _ => panic!("usage: create-team NAME | create-channel TEAM_THREAD GROUP NAME | delete-channel TEAM_THREAD CHANNEL | delete-team GROUP | get PATH"),
74 };
75 let (url, scope) = if let Some(path) = path.strip_prefix("csa:") {
76 (format!("{CSA}/{path}"), Scope::new(CSA_RESOURCE, "user_impersonation"))
77 } else if let Some(path) = path.strip_prefix("graph:") {
78 (format!("{GRAPH}/v1.0/{path}"), Scope::new(GRAPH, ".default"))
79 } else if let Some(path) = path.strip_prefix("provisioning:") {
80 (format!("{PROVISIONING}/{path}"), Scope::new(PROVISIONING_RESOURCE, ".default"))
81 } else {
82 (format!("{MIDDLE_TIER}/{path}"), Scope::new(SPACES, "user_impersonation"))
83 };
84 let mut request = match body {
85 Some(body) => Request::with_body(method, url, body),
86 None => Request { method, ..Request::get(url) },
87 };
88 request.headers = vec![
89 ("x-ms-client-type".to_owned(), "TeamsV2Web".to_owned()),
90 ("Accept".to_owned(), "application/json".to_owned()),
91 ];
92 if let Ok(anchor) = std::env::var("ANCHOR_MAILBOX") {
93 request.headers.push(("X-AnchorMailbox".to_owned(), anchor));
94 }
95 let answers = session
96 .batch(&[request], &scope)
97 .await
98 .expect("request");
99 let answer = &answers[0];
100 println!("HTTP {}", answer.status);
101 let body: &Value = &answer.body;
102 println!("{}", serde_json::to_string_pretty(body).unwrap_or_default());
103}