| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use crate::app::App; |
| 4 | |
| 5 | pub const GRAPH: &str = "https://graph.microsoft.com"; |
| 6 | pub const IC3: &str = "https://ic3.teams.office.com"; |
| 7 | pub const PRESENCE: &str = "https://presence.teams.microsoft.com"; |
| 8 | pub const OUTLOOK: &str = "https://outlook.office.com"; |
| 9 | |
| 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | pub struct Scope { |
| 12 | pub resource: Cow<'static, str>, |
| 13 | pub name: Cow<'static, str>, |
| 14 | } |
| 15 | |
| 16 | impl Scope { |
| 17 | pub fn new(resource: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self { |
| 18 | Scope { |
| 19 | resource: resource.into(), |
| 20 | name: name.into(), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | pub fn graph(name: impl Into<Cow<'static, str>>) -> Self { |
| 25 | Scope::new(GRAPH, name) |
| 26 | } |
| 27 | |
| 28 | /// Tabs to try, best first. The other tab is the fallback, as in the Python client. |
| 29 | pub fn tab_order(&self) -> [App; 2] { |
| 30 | if self.resource == GRAPH && self.name.starts_with("Chat.") { |
| 31 | [App::Outlook, App::Teams] |
| 32 | } else { |
| 33 | [App::Teams, App::Outlook] |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl std::fmt::Display for Scope { |
| 39 | fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 40 | write!(formatter, "{}/{}", self.resource, self.name) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | #[cfg(test)] |
| 45 | mod tests { |
| 46 | use super::*; |
| 47 | |
| 48 | #[test] |
| 49 | fn chat_scopes_prefer_outlook() { |
| 50 | assert_eq!(Scope::graph("Chat.Read").tab_order(), [App::Outlook, App::Teams]); |
| 51 | assert_eq!(Scope::graph("Chat.ReadWrite").tab_order(), [App::Outlook, App::Teams]); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn other_scopes_prefer_teams() { |
| 56 | assert_eq!(Scope::graph("ChannelMessage.Read.All").tab_order(), [App::Teams, App::Outlook]); |
| 57 | assert_eq!(Scope::new(IC3, "Teams.AccessAsUser.All").tab_order(), [App::Teams, App::Outlook]); |
| 58 | assert_eq!(Scope::new(PRESENCE, "Chat.Read").tab_order(), [App::Teams, App::Outlook]); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn displays_resource_and_name() { |
| 63 | assert_eq!(Scope::graph("Chat.Read").to_string(), "https://graph.microsoft.com/Chat.Read"); |
| 64 | } |
| 65 | } |