BlueGit

ict rusty-teams

Files

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

team_admin_probe.rs

team_admin_probe.rs 5.7 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 channel_arguments: Vec<&str> = arguments.iter().map(String::as_str).collect();
38 if let [action @ ("lib-edit" | "lib-delete"), team_thread_id, group_id, channel_id, substrate, name, object_id, tenant_id] =
39 channel_arguments.as_slice()
40 {
41 let channel = chatsvc::ExistingChannel {
42 team_thread_id,
43 group_id,
44 channel_id,
45 substrate_group_id: (*substrate != "-").then_some(*substrate),
46 };
47 let mailbox = chatsvc::Mailbox { object_id, tenant_id };
48 let admin = chatsvc::TeamAdmin::new(&session);
49 let outcome = if *action == "lib-edit" {
50 let edit = chatsvc::ChannelEdit { name, description: None };
51 admin.update_channel(channel, edit, mailbox).await
52 } else {
53 admin.delete_channel(channel, mailbox).await
54 };
55 println!("{outcome:?}");
56 return;
57 }
58 let (method, path, body) = match arguments.iter().map(String::as_str).collect::<Vec<_>>().as_slice() {
59 ["create-team", name] => (
60 Method::Post,
61 "teams/create".to_owned(),
62 Some(json!({"displayName": name, "description": "", "accessType": "Private"})),
63 ),
64 ["create-channel", team_thread_id, group_id, name, channel_type] => (
65 Method::Post,
66 format!("teams/{team_thread_id}/channels"),
67 Some(json!({"displayName": name, "description": "", "groupId": group_id, "channelType": channel_type})),
68 ),
69 ["delete-channel", team_thread_id, channel_id] => (
70 Method::Delete,
71 format!("teams/{team_thread_id}/channels/{channel_id}"),
72 Some(json!({})),
73 ),
74 ["delete-team", group_id] => (Method::Delete, format!("teams/{group_id}/delete"), None),
75 ["get", path] => (Method::Get, (*path).to_owned(), None),
76 ["csa-get", path] => (Method::Get, format!("csa:{path}"), None),
77 ["graph-get", path] => (Method::Get, format!("graph:{path}"), None),
78 ["create-private", group_id, team_thread_id, name] => (
79 Method::Post,
80 format!("provisioning:teams/{group_id}/channels?forceSync=false"),
81 Some(json!({"spaceThreadId": team_thread_id, "displayName": name, "description": "", "channelType": "Private", "sharedWithParentTeam": false})),
82 ),
83 ["send", method, path, body] => (
84 match *method {
85 "PATCH" => Method::Patch,
86 "DELETE" => Method::Delete,
87 "PUT" => Method::Put,
88 _ => Method::Post,
89 },
90 (*path).to_owned(),
91 Some(serde_json::from_str(body).expect("json body")),
92 ),
93 ["provisioning-get", path] => (Method::Get, format!("provisioning:{path}"), None),
94 _ => panic!("usage: create-team NAME | create-channel TEAM_THREAD GROUP NAME | delete-channel TEAM_THREAD CHANNEL | delete-team GROUP | get PATH"),
95 };
96 let (url, scope) = if let Some(path) = path.strip_prefix("csa:") {
97 (format!("{CSA}/{path}"), Scope::new(CSA_RESOURCE, "user_impersonation"))
98 } else if let Some(path) = path.strip_prefix("graph:") {
99 (format!("{GRAPH}/v1.0/{path}"), Scope::new(GRAPH, ".default"))
100 } else if let Some(path) = path.strip_prefix("provisioning:") {
101 (format!("{PROVISIONING}/{path}"), Scope::new(PROVISIONING_RESOURCE, ".default"))
102 } else {
103 (format!("{MIDDLE_TIER}/{path}"), Scope::new(SPACES, "user_impersonation"))
104 };
105 let mut request = match body {
106 Some(body) => Request::with_body(method, url, body),
107 None => Request { method, ..Request::get(url) },
108 };
109 request.headers = vec![
110 ("x-ms-client-type".to_owned(), "TeamsV2Web".to_owned()),
111 ("Accept".to_owned(), "application/json".to_owned()),
112 ];
113 if let Ok(anchor) = std::env::var("ANCHOR_MAILBOX") {
114 request.headers.push(("X-AnchorMailbox".to_owned(), anchor));
115 }
116 let answers = session
117 .batch(&[request], &scope)
118 .await
119 .expect("request");
120 let answer = &answers[0];
121 println!("HTTP {}", answer.status);
122 let body: &Value = &answer.body;
123 println!("{}", serde_json::to_string_pretty(body).unwrap_or_default());
124}