Commit 0f04ee46de
Unsigned
Layout: unified · split
src/render.rs +38 −22
| @@ -442,6 +442,16 @@ fn media_download_command(source: &MediaSource) -> Option<String> { | |||
| 442 | } | 442 | } |
| 443 | } | 443 | } |
| 444 | 444 | ||
| 445 | fn media_download_message<C: HasUrlOrFile>(content: &C) -> Option<String> { | ||
| 446 | media_download_command(content.source()).map(|command| { | ||
| 447 | format!( | ||
| 448 | "attached `{}`, authenticated download: {}", | ||
| 449 | content.body(), | ||
| 450 | command | ||
| 451 | ) | ||
| 452 | }) | ||
| 453 | } | ||
| 454 | |||
| 445 | impl<C: HasUrlOrFile> Render for C { | 455 | impl<C: HasUrlOrFile> Render for C { |
| 446 | type RenderContext = Url; | 456 | type RenderContext = Url; |
| 447 | const TAGS: &'static [&'static str] = &["matrix_media"]; | 457 | const TAGS: &'static [&'static str] = &["matrix_media"]; |
| @@ -456,34 +466,23 @@ impl<C: HasUrlOrFile> Render for C { | |||
| 456 | } | 466 | } |
| 457 | .unwrap_or_else(|_| self.resolve_url().to_string()); | 467 | .unwrap_or_else(|_| self.resolve_url().to_string()); |
| 458 | 468 | ||
| 459 | let message = format!( | 469 | let message = media_download_message(self).unwrap_or_else(|| { |
| 460 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ | 470 | format!( |
| 461 | [{color_reset}{}{color_delimiter}]{color_reset}", | 471 | "{color_delimiter}<{color_reset}{}{color_delimiter}>\ |
| 462 | self.body(), | 472 | [{color_reset}{}{color_delimiter}]{color_reset}", |
| 463 | mxc_url, | 473 | self.body(), |
| 464 | color_delimiter = Weechat::color("color_delimiter"), | 474 | mxc_url, |
| 465 | color_reset = Weechat::color("reset") | 475 | color_delimiter = Weechat::color("color_delimiter"), |
| 466 | ); | 476 | color_reset = Weechat::color("reset") |
| 477 | ) | ||
| 478 | }); | ||
| 467 | 479 | ||
| 468 | let line = RenderedLine { | 480 | let line = RenderedLine { |
| 469 | message, | 481 | message, |
| 470 | tags: self.tags(), | 482 | tags: self.tags(), |
| 471 | }; | 483 | }; |
| 472 | 484 | ||
| 473 | let mut lines = vec![line]; | 485 | RenderedContent { lines: vec![line] } |
| 474 | |||
| 475 | if let Some(command) = media_download_command(self.source()) { | ||
| 476 | lines.push(RenderedLine { | ||
| 477 | message: format!( | ||
| 478 | "{}authenticated download: {}", | ||
| 479 | Weechat::prefix(Prefix::Network), | ||
| 480 | command | ||
| 481 | ), | ||
| 482 | tags: self.tags(), | ||
| 483 | }); | ||
| 484 | } | ||
| 485 | |||
| 486 | RenderedContent { lines } | ||
| 487 | } | 486 | } |
| 488 | } | 487 | } |
| 489 | 488 | ||
| @@ -1035,6 +1034,23 @@ mod tests { | |||
| 1035 | ); | 1034 | ); |
| 1036 | } | 1035 | } |
| 1037 | 1036 | ||
| 1037 | #[test] | ||
| 1038 | fn test_plain_media_renders_authenticated_download_message() { | ||
| 1039 | let content = ImageMessageEventContent::plain( | ||
| 1040 | "image.png".to_owned(), | ||
| 1041 | OwnedMxcUri::from("mxc://matrix.org/some-media-id"), | ||
| 1042 | ); | ||
| 1043 | |||
| 1044 | assert_eq!( | ||
| 1045 | Some( | ||
| 1046 | "attached `image.png`, authenticated download: /matrix media \ | ||
| 1047 | download mxc://matrix.org/some-media-id [file]" | ||
| 1048 | .to_owned() | ||
| 1049 | ), | ||
| 1050 | media_download_message(&content) | ||
| 1051 | ); | ||
| 1052 | } | ||
| 1053 | |||
| 1038 | #[test] | 1054 | #[test] |
| 1039 | fn test_encrypted_media_has_no_plain_download_command() { | 1055 | fn test_encrypted_media_has_no_plain_download_command() { |
| 1040 | use std::collections::BTreeMap; | 1056 | use std::collections::BTreeMap; |