| 1 | use serde_json::{Value, json}; |
| 2 | use session::{Method, Request, SPACES, Scope, Session}; |
| 3 | |
| 4 | use crate::error::Result; |
| 5 | use crate::language_settings::with_headers; |
| 6 | use crate::messages::{encode, ensure_success}; |
| 7 | use crate::pins::{CsaTransport, DEFAULT_REGION, SessionTransport}; |
| 8 | |
| 9 | const MIDDLE_TIER_SCOPE: &str = "user_impersonation"; |
| 10 | const PROVISIONING_RESOURCE: &str = "https://templates.teams.microsoft.com"; |
| 11 | const PROVISIONING_SCOPE: &str = ".default"; |
| 12 | const ANCHOR_HEADER: &str = "X-AnchorMailbox"; |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 15 | pub enum ChannelPrivacy { |
| 16 | Standard, |
| 17 | Private, |
| 18 | } |
| 19 | |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub struct NewChannel<'a> { |
| 22 | pub team_thread_id: &'a str, |
| 23 | pub group_id: &'a str, |
| 24 | pub name: &'a str, |
| 25 | pub description: &'a str, |
| 26 | pub privacy: ChannelPrivacy, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 30 | pub struct ExistingChannel<'a> { |
| 31 | pub team_thread_id: &'a str, |
| 32 | pub group_id: &'a str, |
| 33 | pub channel_id: &'a str, |
| 34 | pub substrate_group_id: Option<&'a str>, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 38 | pub struct ChannelEdit<'a> { |
| 39 | pub name: &'a str, |
| 40 | pub description: Option<&'a str>, |
| 41 | } |
| 42 | |
| 43 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 44 | pub struct Mailbox<'a> { |
| 45 | pub object_id: &'a str, |
| 46 | pub tenant_id: &'a str, |
| 47 | } |
| 48 | |
| 49 | impl Mailbox<'_> { |
| 50 | fn anchor(&self) -> String { |
| 51 | format!("OID:{}@{}", self.object_id, self.tenant_id) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn channel_edit_body(edit: ChannelEdit<'_>) -> Value { |
| 56 | let mut body = json!({ "displayName": edit.name }); |
| 57 | if let Some(description) = edit.description { |
| 58 | body["description"] = json!(description); |
| 59 | } |
| 60 | body |
| 61 | } |
| 62 | |
| 63 | fn substrate_channel_id(substrate_group_id: &str, tenant_id: &str) -> String { |
| 64 | if substrate_group_id.starts_with("OID:") { |
| 65 | substrate_group_id.to_owned() |
| 66 | } else { |
| 67 | format!("OID:{substrate_group_id}@{tenant_id}") |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | pub fn standard_channel_body(channel: &NewChannel<'_>) -> Value { |
| 72 | json!({ |
| 73 | "displayName": channel.name, |
| 74 | "description": channel.description, |
| 75 | "groupId": channel.group_id, |
| 76 | "channelType": "Standard", |
| 77 | }) |
| 78 | } |
| 79 | |
| 80 | pub fn private_channel_body(channel: &NewChannel<'_>) -> Value { |
| 81 | json!({ |
| 82 | "spaceThreadId": channel.team_thread_id, |
| 83 | "displayName": channel.name, |
| 84 | "description": channel.description, |
| 85 | "channelType": "Private", |
| 86 | "sharedWithParentTeam": false, |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | pub struct TeamAdmin<T: CsaTransport = SessionTransport> { |
| 91 | middle_tier: T, |
| 92 | provisioning: T, |
| 93 | region: String, |
| 94 | } |
| 95 | |
| 96 | impl TeamAdmin<SessionTransport> { |
| 97 | pub fn new(session: &Session) -> Self { |
| 98 | TeamAdmin::with_transports( |
| 99 | SessionTransport::with_scope(session, Scope::new(SPACES, MIDDLE_TIER_SCOPE)), |
| 100 | SessionTransport::with_scope( |
| 101 | session, |
| 102 | Scope::new(PROVISIONING_RESOURCE, PROVISIONING_SCOPE), |
| 103 | ), |
| 104 | DEFAULT_REGION, |
| 105 | ) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl<T: CsaTransport> TeamAdmin<T> { |
| 110 | pub fn with_transports(middle_tier: T, provisioning: T, region: &str) -> Self { |
| 111 | TeamAdmin { |
| 112 | middle_tier, |
| 113 | provisioning, |
| 114 | region: region.to_owned(), |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub async fn create_channel( |
| 119 | &self, |
| 120 | channel: &NewChannel<'_>, |
| 121 | mailbox: Mailbox<'_>, |
| 122 | ) -> Result<()> { |
| 123 | let answer = match channel.privacy { |
| 124 | ChannelPrivacy::Standard => { |
| 125 | let url = format!( |
| 126 | "https://teams.cloud.microsoft/api/mt/{}/beta/teams/{}/channels", |
| 127 | self.region, |
| 128 | encode(channel.team_thread_id) |
| 129 | ); |
| 130 | self.middle_tier |
| 131 | .send(with_headers(Request::with_body( |
| 132 | Method::Post, |
| 133 | url, |
| 134 | standard_channel_body(channel), |
| 135 | ))) |
| 136 | .await? |
| 137 | } |
| 138 | ChannelPrivacy::Private => { |
| 139 | let url = format!( |
| 140 | "https://teams.cloud.microsoft/fabric/{}/templates/api/teams/{}/channels?forceSync=false", |
| 141 | self.region, channel.group_id |
| 142 | ); |
| 143 | let request = Request::with_body(Method::Post, url, private_channel_body(channel)); |
| 144 | self.provisioning |
| 145 | .send(with_anchor(request, mailbox)) |
| 146 | .await? |
| 147 | } |
| 148 | }; |
| 149 | ensure_success(&answer) |
| 150 | } |
| 151 | |
| 152 | pub async fn update_channel( |
| 153 | &self, |
| 154 | channel: ExistingChannel<'_>, |
| 155 | edit: ChannelEdit<'_>, |
| 156 | mailbox: Mailbox<'_>, |
| 157 | ) -> Result<()> { |
| 158 | let body = channel_edit_body(edit); |
| 159 | let answer = match channel.substrate_group_id { |
| 160 | None => { |
| 161 | self.middle_tier |
| 162 | .send(with_headers(Request::with_body( |
| 163 | Method::Patch, |
| 164 | self.middle_tier_channel_url(channel), |
| 165 | body, |
| 166 | ))) |
| 167 | .await? |
| 168 | } |
| 169 | Some(substrate_group_id) => { |
| 170 | let request = Request::with_body( |
| 171 | Method::Patch, |
| 172 | self.provisioning_channel_url(channel, substrate_group_id, mailbox), |
| 173 | body, |
| 174 | ); |
| 175 | self.provisioning |
| 176 | .send(with_anchor(request, mailbox)) |
| 177 | .await? |
| 178 | } |
| 179 | }; |
| 180 | ensure_success(&answer) |
| 181 | } |
| 182 | |
| 183 | pub async fn delete_channel( |
| 184 | &self, |
| 185 | channel: ExistingChannel<'_>, |
| 186 | mailbox: Mailbox<'_>, |
| 187 | ) -> Result<()> { |
| 188 | let answer = match channel.substrate_group_id { |
| 189 | None => { |
| 190 | self.middle_tier |
| 191 | .send(with_headers(Request::with_body( |
| 192 | Method::Delete, |
| 193 | self.middle_tier_channel_url(channel), |
| 194 | json!({}), |
| 195 | ))) |
| 196 | .await? |
| 197 | } |
| 198 | Some(substrate_group_id) => { |
| 199 | let request = Request::delete(self.provisioning_channel_url( |
| 200 | channel, |
| 201 | substrate_group_id, |
| 202 | mailbox, |
| 203 | )); |
| 204 | self.provisioning |
| 205 | .send(with_anchor(request, mailbox)) |
| 206 | .await? |
| 207 | } |
| 208 | }; |
| 209 | ensure_success(&answer) |
| 210 | } |
| 211 | |
| 212 | fn middle_tier_channel_url(&self, channel: ExistingChannel<'_>) -> String { |
| 213 | format!( |
| 214 | "https://teams.cloud.microsoft/api/mt/{}/beta/teams/{}/channels/{}", |
| 215 | self.region, |
| 216 | encode(channel.team_thread_id), |
| 217 | encode(channel.channel_id) |
| 218 | ) |
| 219 | } |
| 220 | |
| 221 | fn provisioning_channel_url( |
| 222 | &self, |
| 223 | channel: ExistingChannel<'_>, |
| 224 | substrate_group_id: &str, |
| 225 | mailbox: Mailbox<'_>, |
| 226 | ) -> String { |
| 227 | format!( |
| 228 | "https://teams.cloud.microsoft/fabric/{}/templates/api/teams/{}/channels/{}?forceSync=false", |
| 229 | self.region, |
| 230 | channel.group_id, |
| 231 | encode(&substrate_channel_id(substrate_group_id, mailbox.tenant_id)) |
| 232 | ) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | fn with_anchor(request: Request, mailbox: Mailbox<'_>) -> Request { |
| 237 | let mut request = with_headers(request); |
| 238 | request |
| 239 | .headers |
| 240 | .push((ANCHOR_HEADER.to_owned(), mailbox.anchor())); |
| 241 | request |
| 242 | } |
| 243 | |
| 244 | #[cfg(test)] |
| 245 | mod tests { |
| 246 | use std::sync::Mutex; |
| 247 | |
| 248 | use session::ApiResponse; |
| 249 | |
| 250 | use super::*; |
| 251 | |
| 252 | #[derive(Default)] |
| 253 | struct Recorder { |
| 254 | requests: Mutex<Vec<Request>>, |
| 255 | } |
| 256 | |
| 257 | impl CsaTransport for &Recorder { |
| 258 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 259 | self.requests.lock().unwrap().push(request); |
| 260 | Ok(ApiResponse { |
| 261 | status: 200, |
| 262 | retry_after: None, |
| 263 | body: Value::Null, |
| 264 | }) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | fn channel(privacy: ChannelPrivacy) -> NewChannel<'static> { |
| 269 | NewChannel { |
| 270 | team_thread_id: "19:team@thread.skype", |
| 271 | group_id: "group-1", |
| 272 | name: "Planning", |
| 273 | description: "", |
| 274 | privacy, |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | const MAILBOX: Mailbox<'static> = Mailbox { |
| 279 | object_id: "me", |
| 280 | tenant_id: "tenant", |
| 281 | }; |
| 282 | |
| 283 | #[tokio::test] |
| 284 | async fn standard_channels_go_to_the_middle_tier() { |
| 285 | let middle_tier = Recorder::default(); |
| 286 | let provisioning = Recorder::default(); |
| 287 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 288 | admin |
| 289 | .create_channel(&channel(ChannelPrivacy::Standard), MAILBOX) |
| 290 | .await |
| 291 | .unwrap(); |
| 292 | let requests = middle_tier.requests.lock().unwrap(); |
| 293 | assert_eq!( |
| 294 | requests[0].url, |
| 295 | "https://teams.cloud.microsoft/api/mt/emea/beta/teams/19%3Ateam%40thread.skype/channels" |
| 296 | ); |
| 297 | assert_eq!( |
| 298 | requests[0].body.as_ref().unwrap()["channelType"], |
| 299 | "Standard" |
| 300 | ); |
| 301 | assert_eq!(requests[0].body.as_ref().unwrap()["groupId"], "group-1"); |
| 302 | assert!(provisioning.requests.lock().unwrap().is_empty()); |
| 303 | } |
| 304 | |
| 305 | #[tokio::test] |
| 306 | async fn private_channels_go_to_provisioning_with_the_mailbox_anchor() { |
| 307 | let middle_tier = Recorder::default(); |
| 308 | let provisioning = Recorder::default(); |
| 309 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 310 | admin |
| 311 | .create_channel(&channel(ChannelPrivacy::Private), MAILBOX) |
| 312 | .await |
| 313 | .unwrap(); |
| 314 | let requests = provisioning.requests.lock().unwrap(); |
| 315 | assert_eq!( |
| 316 | requests[0].url, |
| 317 | "https://teams.cloud.microsoft/fabric/emea/templates/api/teams/group-1/channels?forceSync=false" |
| 318 | ); |
| 319 | assert!( |
| 320 | requests[0] |
| 321 | .headers |
| 322 | .contains(&(ANCHOR_HEADER.to_owned(), "OID:me@tenant".to_owned())) |
| 323 | ); |
| 324 | let body = requests[0].body.as_ref().unwrap(); |
| 325 | assert_eq!(body["spaceThreadId"], "19:team@thread.skype"); |
| 326 | assert_eq!(body["channelType"], "Private"); |
| 327 | assert!(middle_tier.requests.lock().unwrap().is_empty()); |
| 328 | } |
| 329 | |
| 330 | const STANDARD: ExistingChannel<'static> = ExistingChannel { |
| 331 | team_thread_id: "19:team@thread.skype", |
| 332 | group_id: "group-1", |
| 333 | channel_id: "19:channel@thread.skype", |
| 334 | substrate_group_id: None, |
| 335 | }; |
| 336 | |
| 337 | #[tokio::test] |
| 338 | async fn standard_channel_edits_and_deletes_go_to_the_middle_tier() { |
| 339 | let middle_tier = Recorder::default(); |
| 340 | let provisioning = Recorder::default(); |
| 341 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 342 | let edit = ChannelEdit { |
| 343 | name: "Renamed", |
| 344 | description: None, |
| 345 | }; |
| 346 | admin.update_channel(STANDARD, edit, MAILBOX).await.unwrap(); |
| 347 | admin.delete_channel(STANDARD, MAILBOX).await.unwrap(); |
| 348 | let requests = middle_tier.requests.lock().unwrap(); |
| 349 | let url = "https://teams.cloud.microsoft/api/mt/emea/beta/teams/19%3Ateam%40thread.skype/channels/19%3Achannel%40thread.skype"; |
| 350 | assert_eq!( |
| 351 | (requests[0].method, requests[0].url.as_str()), |
| 352 | (Method::Patch, url) |
| 353 | ); |
| 354 | assert_eq!( |
| 355 | requests[0].body.as_ref().unwrap(), |
| 356 | &json!({"displayName": "Renamed"}) |
| 357 | ); |
| 358 | assert_eq!( |
| 359 | (requests[1].method, requests[1].url.as_str()), |
| 360 | (Method::Delete, url) |
| 361 | ); |
| 362 | assert!(provisioning.requests.lock().unwrap().is_empty()); |
| 363 | } |
| 364 | |
| 365 | #[tokio::test] |
| 366 | async fn private_channel_edits_and_deletes_go_to_provisioning_by_substrate_id() { |
| 367 | let middle_tier = Recorder::default(); |
| 368 | let provisioning = Recorder::default(); |
| 369 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 370 | let private = ExistingChannel { |
| 371 | substrate_group_id: Some("substrate-1"), |
| 372 | ..STANDARD |
| 373 | }; |
| 374 | let edit = ChannelEdit { |
| 375 | name: "Renamed", |
| 376 | description: Some("About"), |
| 377 | }; |
| 378 | admin.update_channel(private, edit, MAILBOX).await.unwrap(); |
| 379 | admin.delete_channel(private, MAILBOX).await.unwrap(); |
| 380 | let requests = provisioning.requests.lock().unwrap(); |
| 381 | let url = "https://teams.cloud.microsoft/fabric/emea/templates/api/teams/group-1/channels/OID%3Asubstrate-1%40tenant?forceSync=false"; |
| 382 | assert_eq!( |
| 383 | (requests[0].method, requests[0].url.as_str()), |
| 384 | (Method::Patch, url) |
| 385 | ); |
| 386 | assert_eq!( |
| 387 | requests[0].body.as_ref().unwrap(), |
| 388 | &json!({"displayName": "Renamed", "description": "About"}) |
| 389 | ); |
| 390 | assert_eq!( |
| 391 | (requests[1].method, requests[1].url.as_str()), |
| 392 | (Method::Delete, url) |
| 393 | ); |
| 394 | assert!( |
| 395 | requests[1] |
| 396 | .headers |
| 397 | .contains(&(ANCHOR_HEADER.to_owned(), "OID:me@tenant".to_owned())) |
| 398 | ); |
| 399 | assert!(middle_tier.requests.lock().unwrap().is_empty()); |
| 400 | } |
| 401 | } |