BlueGit

ict rusty-teams

Files

rusty-teams / crates / graph / src / tabs.rs

tabs.rs

tabs.rs 2.3 KiB Download Raw
1use serde_json::Value;
2use session::Scope;
3
4use crate::client::Graph;
5use crate::error::Result;
6use crate::models::Member;
7use crate::urls;
8
9const TABS_SCOPE: &str = "TeamsTab.ReadWrite.All";
10const MEMBERS_SCOPE: &str = "ChannelMember.Read.All";
11
12fn web_url(tab: &Value) -> Option<String> {
13 tab.get("webUrl")
14 .and_then(Value::as_str)
15 .filter(|url| url.starts_with("https://"))
16 .map(str::to_owned)
17}
18
19impl Graph {
20 pub async fn channel_tab_web_url(
21 &self,
22 team_id: &str,
23 channel_id: &str,
24 tab_id: &str,
25 ) -> Result<Option<String>> {
26 let tab = self
27 .get(
28 &urls::channel_tab(team_id, channel_id, tab_id),
29 &Scope::graph(TABS_SCOPE),
30 )
31 .await?;
32 Ok(web_url(&tab))
33 }
34
35 pub async fn channel_member_count(&self, team_id: &str, channel_id: &str) -> Result<usize> {
36 let members: Vec<Value> = self
37 .get_all(
38 &urls::channel_members(team_id, channel_id),
39 Scope::graph(MEMBERS_SCOPE),
40 )
41 .await?;
42 Ok(members.len())
43 }
44
45 pub async fn channel_member_list(&self, team_id: &str, channel_id: &str) -> Result<Vec<Member>> {
46 self.get_all(&urls::channel_member_list(team_id, channel_id), Scope::graph(MEMBERS_SCOPE))
47 .await
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use serde_json::json;
54
55 use super::*;
56
57 #[test]
58 fn the_tab_deep_link_is_its_web_url() {
59 let tab = json!({"id": "t1", "displayName": "Plan", "webUrl": "https://teams.microsoft.com/l/channel/19%3Ac/tab%3A%3At1?groupId=g&tenantId=t"});
60 assert_eq!(
61 web_url(&tab).as_deref(),
62 Some("https://teams.microsoft.com/l/channel/19%3Ac/tab%3A%3At1?groupId=g&tenantId=t")
63 );
64 }
65
66 #[test]
67 fn a_tab_without_a_usable_web_url_has_none() {
68 assert_eq!(web_url(&json!({"id": "t1"})), None);
69 assert_eq!(web_url(&json!({"webUrl": "javascript:alert(1)"})), None);
70 }
71
72 #[test]
73 fn the_tab_url_names_team_channel_and_tab() {
74 assert_eq!(
75 urls::channel_tab("team", "19:c@thread.tacv2", "tab-1"),
76 "https://graph.microsoft.com/v1.0/teams/team/channels/19%3Ac%40thread.tacv2/tabs/tab-1"
77 );
78 }
79}