| 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 Mailbox<'a> { |
| 31 | pub object_id: &'a str, |
| 32 | pub tenant_id: &'a str, |
| 33 | } |
| 34 | |
| 35 | impl Mailbox<'_> { |
| 36 | fn anchor(&self) -> String { |
| 37 | format!("OID:{}@{}", self.object_id, self.tenant_id) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | pub fn standard_channel_body(channel: &NewChannel<'_>) -> Value { |
| 42 | json!({ |
| 43 | "displayName": channel.name, |
| 44 | "description": channel.description, |
| 45 | "groupId": channel.group_id, |
| 46 | "channelType": "Standard", |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | pub fn private_channel_body(channel: &NewChannel<'_>) -> Value { |
| 51 | json!({ |
| 52 | "spaceThreadId": channel.team_thread_id, |
| 53 | "displayName": channel.name, |
| 54 | "description": channel.description, |
| 55 | "channelType": "Private", |
| 56 | "sharedWithParentTeam": false, |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | pub struct TeamAdmin<T: CsaTransport = SessionTransport> { |
| 61 | middle_tier: T, |
| 62 | provisioning: T, |
| 63 | region: String, |
| 64 | } |
| 65 | |
| 66 | impl TeamAdmin<SessionTransport> { |
| 67 | pub fn new(session: &Session) -> Self { |
| 68 | TeamAdmin::with_transports( |
| 69 | SessionTransport::with_scope(session, Scope::new(SPACES, MIDDLE_TIER_SCOPE)), |
| 70 | SessionTransport::with_scope( |
| 71 | session, |
| 72 | Scope::new(PROVISIONING_RESOURCE, PROVISIONING_SCOPE), |
| 73 | ), |
| 74 | DEFAULT_REGION, |
| 75 | ) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl<T: CsaTransport> TeamAdmin<T> { |
| 80 | pub fn with_transports(middle_tier: T, provisioning: T, region: &str) -> Self { |
| 81 | TeamAdmin { |
| 82 | middle_tier, |
| 83 | provisioning, |
| 84 | region: region.to_owned(), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub async fn create_channel( |
| 89 | &self, |
| 90 | channel: &NewChannel<'_>, |
| 91 | mailbox: Mailbox<'_>, |
| 92 | ) -> Result<()> { |
| 93 | let answer = match channel.privacy { |
| 94 | ChannelPrivacy::Standard => { |
| 95 | let url = format!( |
| 96 | "https://teams.cloud.microsoft/api/mt/{}/beta/teams/{}/channels", |
| 97 | self.region, |
| 98 | encode(channel.team_thread_id) |
| 99 | ); |
| 100 | self.middle_tier |
| 101 | .send(with_headers(Request::with_body( |
| 102 | Method::Post, |
| 103 | url, |
| 104 | standard_channel_body(channel), |
| 105 | ))) |
| 106 | .await? |
| 107 | } |
| 108 | ChannelPrivacy::Private => { |
| 109 | let url = format!( |
| 110 | "https://teams.cloud.microsoft/fabric/{}/templates/api/teams/{}/channels?forceSync=false", |
| 111 | self.region, channel.group_id |
| 112 | ); |
| 113 | let mut request = with_headers(Request::with_body( |
| 114 | Method::Post, |
| 115 | url, |
| 116 | private_channel_body(channel), |
| 117 | )); |
| 118 | request |
| 119 | .headers |
| 120 | .push((ANCHOR_HEADER.to_owned(), mailbox.anchor())); |
| 121 | self.provisioning.send(request).await? |
| 122 | } |
| 123 | }; |
| 124 | ensure_success(&answer) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | #[cfg(test)] |
| 129 | mod tests { |
| 130 | use std::sync::Mutex; |
| 131 | |
| 132 | use session::ApiResponse; |
| 133 | |
| 134 | use super::*; |
| 135 | |
| 136 | #[derive(Default)] |
| 137 | struct Recorder { |
| 138 | requests: Mutex<Vec<Request>>, |
| 139 | } |
| 140 | |
| 141 | impl CsaTransport for &Recorder { |
| 142 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 143 | self.requests.lock().unwrap().push(request); |
| 144 | Ok(ApiResponse { |
| 145 | status: 200, |
| 146 | retry_after: None, |
| 147 | body: Value::Null, |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fn channel(privacy: ChannelPrivacy) -> NewChannel<'static> { |
| 153 | NewChannel { |
| 154 | team_thread_id: "19:team@thread.skype", |
| 155 | group_id: "group-1", |
| 156 | name: "Planning", |
| 157 | description: "", |
| 158 | privacy, |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | const MAILBOX: Mailbox<'static> = Mailbox { |
| 163 | object_id: "me", |
| 164 | tenant_id: "tenant", |
| 165 | }; |
| 166 | |
| 167 | #[tokio::test] |
| 168 | async fn standard_channels_go_to_the_middle_tier() { |
| 169 | let middle_tier = Recorder::default(); |
| 170 | let provisioning = Recorder::default(); |
| 171 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 172 | admin |
| 173 | .create_channel(&channel(ChannelPrivacy::Standard), MAILBOX) |
| 174 | .await |
| 175 | .unwrap(); |
| 176 | let requests = middle_tier.requests.lock().unwrap(); |
| 177 | assert_eq!( |
| 178 | requests[0].url, |
| 179 | "https://teams.cloud.microsoft/api/mt/emea/beta/teams/19%3Ateam%40thread.skype/channels" |
| 180 | ); |
| 181 | assert_eq!( |
| 182 | requests[0].body.as_ref().unwrap()["channelType"], |
| 183 | "Standard" |
| 184 | ); |
| 185 | assert_eq!(requests[0].body.as_ref().unwrap()["groupId"], "group-1"); |
| 186 | assert!(provisioning.requests.lock().unwrap().is_empty()); |
| 187 | } |
| 188 | |
| 189 | #[tokio::test] |
| 190 | async fn private_channels_go_to_provisioning_with_the_mailbox_anchor() { |
| 191 | let middle_tier = Recorder::default(); |
| 192 | let provisioning = Recorder::default(); |
| 193 | let admin = TeamAdmin::with_transports(&middle_tier, &provisioning, "emea"); |
| 194 | admin |
| 195 | .create_channel(&channel(ChannelPrivacy::Private), MAILBOX) |
| 196 | .await |
| 197 | .unwrap(); |
| 198 | let requests = provisioning.requests.lock().unwrap(); |
| 199 | assert_eq!( |
| 200 | requests[0].url, |
| 201 | "https://teams.cloud.microsoft/fabric/emea/templates/api/teams/group-1/channels?forceSync=false" |
| 202 | ); |
| 203 | assert!( |
| 204 | requests[0] |
| 205 | .headers |
| 206 | .contains(&(ANCHOR_HEADER.to_owned(), "OID:me@tenant".to_owned())) |
| 207 | ); |
| 208 | let body = requests[0].body.as_ref().unwrap(); |
| 209 | assert_eq!(body["spaceThreadId"], "19:team@thread.skype"); |
| 210 | assert_eq!(body["channelType"], "Private"); |
| 211 | assert!(middle_tier.requests.lock().unwrap().is_empty()); |
| 212 | } |
| 213 | } |