Commit 90964b68b4
Unsigned
Layout: unified · split
src/render.rs +60 −1
| @@ -415,6 +415,15 @@ fn mxc_to_emxc( | ||
| 415 | 415 | Ok(emxc_url.to_string()) |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | fn media_download_command(source: &MediaSource) -> Option<String> { | |
| 419 | match source { | |
| 420 | MediaSource::Plain(url) => { | |
| 421 | Some(format!("/matrix media download {} <file>", url.as_str())) | |
| 422 | } | |
| 423 | MediaSource::Encrypted(_) => None, | |
| 424 | } | |
| 425 | } | |
| 426 | ||
| 418 | 427 | impl<C: HasUrlOrFile> Render for C { |
| 419 | 428 | type RenderContext = Url; |
| 420 | 429 | const TAGS: &'static [&'static str] = &["matrix_media"]; |
| @@ -443,7 +452,16 @@ impl<C: HasUrlOrFile> Render for C { | ||
| 443 | 452 | tags: self.tags(), |
| 444 | 453 | }; |
| 445 | 454 | |
| 446 | RenderedContent { lines: vec![line] } | |
| 455 | let mut lines = vec![line]; | |
| 456 | ||
| 457 | if let Some(command) = media_download_command(self.source()) { | |
| 458 | lines.push(RenderedLine { | |
| 459 | message: format!("authenticated download: {}", command), | |
| 460 | tags: self.tags(), | |
| 461 | }); | |
| 462 | } | |
| 463 | ||
| 464 | RenderedContent { lines } | |
| 447 | 465 | } |
| 448 | 466 | } |
| 449 | 467 | |
| @@ -1025,4 +1043,45 @@ mod tests { | ||
| 1025 | 1043 | mxc_to_emxc(&mxc_url, &homeserver, &encrypt_info).unwrap() |
| 1026 | 1044 | ); |
| 1027 | 1045 | } |
| 1046 | ||
| 1047 | #[test] | |
| 1048 | fn test_plain_media_download_command() { | |
| 1049 | let source = MediaSource::Plain(OwnedMxcUri::from( | |
| 1050 | "mxc://matrix.org/some-media-id", | |
| 1051 | )); | |
| 1052 | ||
| 1053 | assert_eq!( | |
| 1054 | Some( | |
| 1055 | "/matrix media download mxc://matrix.org/some-media-id <file>" | |
| 1056 | .to_owned() | |
| 1057 | ), | |
| 1058 | media_download_command(&source) | |
| 1059 | ); | |
| 1060 | } | |
| 1061 | ||
| 1062 | #[test] | |
| 1063 | fn test_encrypted_media_has_no_plain_download_command() { | |
| 1064 | use std::collections::BTreeMap; | |
| 1065 | ||
| 1066 | let mut hashes: BTreeMap<String, Base64> = BTreeMap::new(); | |
| 1067 | hashes.insert("sha256".to_string(), Base64::parse("aGFzaA").unwrap()); | |
| 1068 | let encrypt_info = EncryptedFileInit { | |
| 1069 | key: JsonWebKeyInit { | |
| 1070 | k: Base64::parse("dGVzdA").unwrap(), | |
| 1071 | kty: "oct".to_string(), | |
| 1072 | key_ops: vec![], | |
| 1073 | ext: true, | |
| 1074 | alg: "A256CTR".to_string(), | |
| 1075 | } | |
| 1076 | .into(), | |
| 1077 | iv: Base64::parse("aXY").unwrap(), | |
| 1078 | v: "v2".to_string(), | |
| 1079 | url: OwnedMxcUri::from("mxc://some-url"), | |
| 1080 | hashes, | |
| 1081 | } | |
| 1082 | .into(); | |
| 1083 | let source = MediaSource::Encrypted(Box::new(encrypt_info)); | |
| 1084 | ||
| 1085 | assert_eq!(None, media_download_command(&source)); | |
| 1086 | } | |
| 1028 | 1087 | } |