BlueGit

ict rusty-teams

Files

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

content.rs

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