BlueGit

ict rusty-teams

Files

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

content.rs

content.rs 2.4 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 CHANNEL_PATH: &str = "/v1.0/teams/";
9const SHARES_PATH: &str = "/v1.0/shares/";
10const SHARES_SCOPE: &str = "Files.ReadWrite.All";
11const EXTERNAL_IMAGE_SCOPE: &str = "User.ReadBasic.All";
12
13impl Graph {
14 /// Binary GET of a Graph hosted content URL (`.../hostedContents/{id}/$value`).
15 pub async fn download_hosted_content(&self, url: &str) -> Result<Photo> {
16 ensure_graph_url(url)?;
17 let scope = hosted_content_scope(url).ok_or(Error::ForeignNextLink)?;
18 let request = Request::binary_get(url);
19 let answer = self
20 .session()
21 .batch(std::slice::from_ref(&request), &scope)
22 .await?
23 .remove(0);
24 decode_binary(url, answer)
25 }
26
27 /// Anonymous binary GET of a public image URL (GIF or sticker CDN), run inside the page.
28 pub async fn download_external_image(&self, url: &str) -> Result<Photo> {
29 let request = Request::anonymous_binary_get(url, Vec::new());
30 let answer = self
31 .session()
32 .send(request, &Scope::graph(EXTERNAL_IMAGE_SCOPE))
33 .await?;
34 decode_binary(url, answer)
35 }
36}
37
38pub fn hosted_content_scope(url: &str) -> Option<Scope> {
39 let path = url.strip_prefix(GRAPH)?;
40 if path.starts_with(CHANNEL_PATH) {
41 Some(Scope::graph("ChannelMessage.Read.All"))
42 } else if path.starts_with(SHARES_PATH) {
43 Some(Scope::graph(SHARES_SCOPE))
44 } else {
45 None
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn scope_follows_the_conversation_kind() {
55 let chat =
56 "https://graph.microsoft.com/v1.0/chats/19%3Aa/messages/1/hostedContents/2/$value";
57 let channel = "https://graph.microsoft.com/v1.0/teams/t/channels/c/messages/1/hostedContents/2/$value";
58 assert!(hosted_content_scope(chat).is_none());
59 assert_eq!(
60 hosted_content_scope(channel).unwrap().name,
61 "ChannelMessage.Read.All"
62 );
63 let thumbnail =
64 "https://graph.microsoft.com/v1.0/shares/u!aHR0cA/driveItem/thumbnails/0/large/content";
65 assert_eq!(
66 hosted_content_scope(thumbnail).unwrap().name,
67 "Files.ReadWrite.All"
68 );
69 assert!(hosted_content_scope("https://graph.microsoft.com/v1.0/me").is_none());
70 }
71}