BlueGit

ict rusty-teams

Files

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

content.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
content.rs 1.7 KiB Download Raw
1use session::{GRAPH, Request, Scope};
2
3use crate::client::{Graph, ensure_graph_url};
4use crate::error::{Error, Result};
5use crate::models::Photo;
6use crate::people::decode_binary;
7
8const CHAT_PATH: &str = "/v1.0/chats/";
9const CHANNEL_PATH: &str = "/v1.0/teams/";
10
11impl Graph {
12 /// Binary GET of a Graph hosted content URL (`.../hostedContents/{id}/$value`).
13 pub async fn download_hosted_content(&self, url: &str) -> Result<Photo> {
14 ensure_graph_url(url)?;
15 let scope = hosted_content_scope(url).ok_or(Error::ForeignNextLink)?;
16 let request = Request::binary_get(url);
17 let answer = self
18 .session()
19 .batch(std::slice::from_ref(&request), &scope)
20 .await?
21 .remove(0);
22 decode_binary(url, answer)
23 }
24}
25
26pub fn hosted_content_scope(url: &str) -> Option<Scope> {
27 let path = url.strip_prefix(GRAPH)?;
28 if path.starts_with(CHAT_PATH) {
29 Some(Scope::graph("Chat.Read"))
30 } else if path.starts_with(CHANNEL_PATH) {
31 Some(Scope::graph("ChannelMessage.Read.All"))
32 } else {
33 None
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::*;
40
41 #[test]
42 fn scope_follows_the_conversation_kind() {
43 let chat =
44 "https://graph.microsoft.com/v1.0/chats/19%3Aa/messages/1/hostedContents/2/$value";
45 let channel = "https://graph.microsoft.com/v1.0/teams/t/channels/c/messages/1/hostedContents/2/$value";
46 assert_eq!(hosted_content_scope(chat).unwrap().name, "Chat.Read");
47 assert_eq!(
48 hosted_content_scope(channel).unwrap().name,
49 "ChannelMessage.Read.All"
50 );
51 assert!(hosted_content_scope("https://graph.microsoft.com/v1.0/me").is_none());
52 }
53}