BlueGit

ict rusty-teams

Files

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

tabs.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
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(
46 &self,
47 team_id: &str,
48 channel_id: &str,
49 ) -> Result<Vec<Member>> {
50 self.get_all(
51 &urls::channel_member_list(team_id, channel_id),
52 Scope::graph(MEMBERS_SCOPE),
53 )
54 .await
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use serde_json::json;
61
62 use super::*;
63
64 #[test]
65 fn the_tab_deep_link_is_its_web_url() {
66 let tab = json!({"id": "t1", "displayName": "Plan", "webUrl": "https://teams.microsoft.com/l/channel/19%3Ac/tab%3A%3At1?groupId=g&tenantId=t"});
67 assert_eq!(
68 web_url(&tab).as_deref(),
69 Some("https://teams.microsoft.com/l/channel/19%3Ac/tab%3A%3At1?groupId=g&tenantId=t")
70 );
71 }
72
73 #[test]
74 fn a_tab_without_a_usable_web_url_has_none() {
75 assert_eq!(web_url(&json!({"id": "t1"})), None);
76 assert_eq!(web_url(&json!({"webUrl": "javascript:alert(1)"})), None);
77 }
78
79 #[test]
80 fn the_tab_url_names_team_channel_and_tab() {
81 assert_eq!(
82 urls::channel_tab("team", "19:c@thread.tacv2", "tab-1"),
83 "https://graph.microsoft.com/v1.0/teams/team/channels/19%3Ac%40thread.tacv2/tabs/tab-1"
84 );
85 }
86}