BlueGit

ict rusty-teams

Files

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

team_admin_probe.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
team_admin_probe.rs 6.3 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 [
17 "lib-create",
18 team_thread_id,
19 group_id,
20 name,
21 privacy,
22 object_id,
23 tenant_id,
24 ] = arguments
25 .iter()
26 .map(String::as_str)
27 .collect::<Vec<_>>()
28 .as_slice()
29 {
30 let channel = chatsvc::NewChannel {
31 team_thread_id,
32 group_id,
33 name,
34 description: "",
35 privacy: if *privacy == "private" {
36 chatsvc::ChannelPrivacy::Private
37 } else {
38 chatsvc::ChannelPrivacy::Standard
39 },
40 };
41 let mailbox = chatsvc::Mailbox {
42 object_id,
43 tenant_id,
44 };
45 let outcome = chatsvc::TeamAdmin::new(&session)
46 .create_channel(&channel, mailbox)
47 .await;
48 println!("{outcome:?}");
49 return;
50 }
51 let channel_arguments: Vec<&str> = arguments.iter().map(String::as_str).collect();
52 if let [
53 action @ ("lib-edit" | "lib-delete"),
54 team_thread_id,
55 group_id,
56 channel_id,
57 substrate,
58 name,
59 object_id,
60 tenant_id,
61 ] = channel_arguments.as_slice()
62 {
63 let channel = chatsvc::ExistingChannel {
64 team_thread_id,
65 group_id,
66 channel_id,
67 substrate_group_id: (*substrate != "-").then_some(*substrate),
68 };
69 let mailbox = chatsvc::Mailbox {
70 object_id,
71 tenant_id,
72 };
73 let admin = chatsvc::TeamAdmin::new(&session);
74 let outcome = if *action == "lib-edit" {
75 let edit = chatsvc::ChannelEdit {
76 name,
77 description: None,
78 };
79 admin.update_channel(channel, edit, mailbox).await
80 } else {
81 admin.delete_channel(channel, mailbox).await
82 };
83 println!("{outcome:?}");
84 return;
85 }
86 let (method, path, body) = match arguments
87 .iter()
88 .map(String::as_str)
89 .collect::<Vec<_>>()
90 .as_slice()
91 {
92 ["create-team", name] => (
93 Method::Post,
94 "teams/create".to_owned(),
95 Some(json!({"displayName": name, "description": "", "accessType": "Private"})),
96 ),
97 [
98 "create-channel",
99 team_thread_id,
100 group_id,
101 name,
102 channel_type,
103 ] => (
104 Method::Post,
105 format!("teams/{team_thread_id}/channels"),
106 Some(
107 json!({"displayName": name, "description": "", "groupId": group_id, "channelType": channel_type}),
108 ),
109 ),
110 ["delete-channel", team_thread_id, channel_id] => (
111 Method::Delete,
112 format!("teams/{team_thread_id}/channels/{channel_id}"),
113 Some(json!({})),
114 ),
115 ["delete-team", group_id] => (Method::Delete, format!("teams/{group_id}/delete"), None),
116 ["get", path] => (Method::Get, (*path).to_owned(), None),
117 ["csa-get", path] => (Method::Get, format!("csa:{path}"), None),
118 ["graph-get", path] => (Method::Get, format!("graph:{path}"), None),
119 ["create-private", group_id, team_thread_id, name] => (
120 Method::Post,
121 format!("provisioning:teams/{group_id}/channels?forceSync=false"),
122 Some(
123 json!({"spaceThreadId": team_thread_id, "displayName": name, "description": "", "channelType": "Private", "sharedWithParentTeam": false}),
124 ),
125 ),
126 ["send", method, path, body] => (
127 match *method {
128 "PATCH" => Method::Patch,
129 "DELETE" => Method::Delete,
130 "PUT" => Method::Put,
131 _ => Method::Post,
132 },
133 (*path).to_owned(),
134 Some(serde_json::from_str(body).expect("json body")),
135 ),
136 ["provisioning-get", path] => (Method::Get, format!("provisioning:{path}"), None),
137 _ => panic!(
138 "usage: create-team NAME | create-channel TEAM_THREAD GROUP NAME | delete-channel TEAM_THREAD CHANNEL | delete-team GROUP | get PATH"
139 ),
140 };
141 let (url, scope) = if let Some(path) = path.strip_prefix("csa:") {
142 (
143 format!("{CSA}/{path}"),
144 Scope::new(CSA_RESOURCE, "user_impersonation"),
145 )
146 } else if let Some(path) = path.strip_prefix("graph:") {
147 (
148 format!("{GRAPH}/v1.0/{path}"),
149 Scope::new(GRAPH, ".default"),
150 )
151 } else if let Some(path) = path.strip_prefix("provisioning:") {
152 (
153 format!("{PROVISIONING}/{path}"),
154 Scope::new(PROVISIONING_RESOURCE, ".default"),
155 )
156 } else {
157 (
158 format!("{MIDDLE_TIER}/{path}"),
159 Scope::new(SPACES, "user_impersonation"),
160 )
161 };
162 let mut request = match body {
163 Some(body) => Request::with_body(method, url, body),
164 None => Request {
165 method,
166 ..Request::get(url)
167 },
168 };
169 request.headers = vec![
170 ("x-ms-client-type".to_owned(), "TeamsV2Web".to_owned()),
171 ("Accept".to_owned(), "application/json".to_owned()),
172 ];
173 if let Ok(anchor) = std::env::var("ANCHOR_MAILBOX") {
174 request.headers.push(("X-AnchorMailbox".to_owned(), anchor));
175 }
176 let answers = session.batch(&[request], &scope).await.expect("request");
177 let answer = &answers[0];
178 println!("HTTP {}", answer.status);
179 let body: &Value = &answer.body;
180 println!("{}", serde_json::to_string_pretty(body).unwrap_or_default());
181}