Commit 57bcbe693c

57bcbe693c09a4742bdfffee1178e2b2c1f75a93

parent: f0a595f780

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-30 14:24 UTC
committer: <strk@kbt.io>

feat: expose Matrix media metadata to relay clients

Layout: unified · split

src/commands/media.rs +29 −1
@@ -50,7 +50,7 @@ impl MediaCommand {
5050
5151 let file = args
5252 .value_of("file")
53 .map(|file| PathBuf::from(Weechat::expand_home(file)))
53 .map(normalize_download_file)
5454 .or_else(|| default_download_file(&uri, &download_prefix));
5555 let file = match file {
5656 Some(file) => file,
@@ -96,6 +96,22 @@ impl MediaCommand {
9696 ];
9797}
9898
99fn normalize_download_file(file: &str) -> PathBuf {
100 PathBuf::from(Weechat::expand_home(strip_download_file_quotes(file)))
101}
102
103fn strip_download_file_quotes(file: &str) -> &str {
104 let file = file.trim();
105 file
106 .strip_prefix('"')
107 .and_then(|file| file.strip_suffix('"'))
108 .or_else(|| {
109 file.strip_prefix('\'')
110 .and_then(|file| file.strip_suffix('\''))
111 })
112 .unwrap_or(file)
113}
114
99115fn default_download_file(uri: &MxcUri, prefix: &str) -> Option<PathBuf> {
100116 uri.as_str()
101117 .trim_end_matches('/')
@@ -182,6 +198,18 @@ mod tests {
182198 );
183199 }
184200
201 #[test]
202 fn strips_weechat_preserved_quotes_from_download_file() {
203 assert_eq!(
204 "/tmp/a path/image.png",
205 strip_download_file_quotes("\"/tmp/a path/image.png\"")
206 );
207 assert_eq!(
208 "/tmp/a path/image.png",
209 strip_download_file_quotes("'/tmp/a path/image.png'")
210 );
211 }
212
185213 #[test]
186214 fn default_download_prefix_can_include_directory() {
187215 let uri = Box::<MxcUri>::from("mxc://matrix.org/some-media-id");
src/render.rs +54 −5
@@ -1,3 +1,4 @@
1use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
12use url::Url;
23
34use matrix_sdk::{
@@ -525,10 +526,34 @@ fn media_download_message<C: HasUrlOrFile>(content: &C) -> Option<String> {
525526 })
526527}
527528
529fn media_tags<C: HasUrlOrFile>(content: &C) -> Vec<String> {
530 let mut tags = vec![
531 "matrix_media".to_owned(),
532 format!("matrix_media_kind_{}", content.media_kind()),
533 format!(
534 "matrix_media_name_{}",
535 URL_SAFE_NO_PAD.encode(content.body())
536 ),
537 ];
538
539 if let MediaSource::Plain(uri) = content.source() {
540 tags.push(format!(
541 "matrix_media_uri_{}",
542 URL_SAFE_NO_PAD.encode(uri.as_str())
543 ));
544 }
545
546 tags
547}
548
528549impl<C: HasUrlOrFile> Render for C {
529550 type RenderContext = Url;
530551 const TAGS: &'static [&'static str] = &["matrix_media"];
531552
553 fn tags(&self) -> Vec<String> {
554 media_tags(self)
555 }
556
532557 fn render(&self, homeserver: &Self::RenderContext) -> RenderedContent {
533558 // Convert MXC to HTTP(s) or EMXC, but fallback to MXC if unable to.
534559 let mxc_url = match self.encrypted_file() {
@@ -1317,6 +1342,8 @@ has_formatted_body!(TextMessageEventContent);
13171342pub trait HasUrlOrFile {
13181343 fn body(&self) -> &str;
13191344
1345 fn media_kind(&self) -> &'static str;
1346
13201347 #[inline]
13211348 fn resolve_url(&self) -> &MxcUri {
13221349 match self.source() {
@@ -1333,12 +1360,16 @@ pub trait HasUrlOrFile {
13331360// Same as above: a simple macro to implement the trait for structs with `url`
13341361// and `file` fields.
13351362macro_rules! has_url_or_file {
1336 ($content: ident) => {
1363 ($content: ident, $kind: literal) => {
13371364 impl HasUrlOrFile for $content {
13381365 fn body(&self) -> &str {
13391366 &self.body
13401367 }
13411368
1369 fn media_kind(&self) -> &'static str {
1370 $kind
1371 }
1372
13421373 fn source(&self) -> &MediaSource {
13431374 &self.source
13441375 }
@@ -1353,10 +1384,10 @@ macro_rules! has_url_or_file {
13531384 };
13541385}
13551386
1356has_url_or_file!(AudioMessageEventContent);
1357has_url_or_file!(FileMessageEventContent);
1358has_url_or_file!(ImageMessageEventContent);
1359has_url_or_file!(VideoMessageEventContent);
1387has_url_or_file!(AudioMessageEventContent, "audio");
1388has_url_or_file!(FileMessageEventContent, "file");
1389has_url_or_file!(ImageMessageEventContent, "image");
1390has_url_or_file!(VideoMessageEventContent, "video");
13601391
13611392/// Rendering implementation for membership events (joins, leaves, bans, profile
13621393/// changes, etc).
@@ -1597,6 +1628,24 @@ mod tests {
15971628 );
15981629 }
15991630
1631 #[test]
1632 fn plain_image_exposes_structured_media_tags() {
1633 let content = ImageMessageEventContent::plain(
1634 "image.png".to_owned(),
1635 OwnedMxcUri::from("mxc://matrix.org/some-media-id"),
1636 );
1637
1638 assert_eq!(
1639 vec![
1640 "matrix_media",
1641 "matrix_media_kind_image",
1642 "matrix_media_name_aW1hZ2UucG5n",
1643 "matrix_media_uri_bXhjOi8vbWF0cml4Lm9yZy9zb21lLW1lZGlhLWlk",
1644 ],
1645 content.tags()
1646 );
1647 }
1648
16001649 #[test]
16011650 fn formatted_body_plain_text_decodes_matrix_html() {
16021651 let body = "<p>Hello <b>world</b> &amp; \