Skip to content

Commit 497be7f

Browse files
authored
Display file sizes for attachments (#278)
1 parent 64e4f67 commit 497be7f

File tree

3 files changed

+135
-4
lines changed

3 files changed

+135
-4
lines changed

Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ unicode-segmentation = "^1.7"
6161
unicode-width = "0.1.10"
6262
url = {version = "^2.2.2", features = ["serde"]}
6363
edit = "0.1.4"
64+
humansize = "2.0.0"
6465

6566
[dependencies.modalkit]
6667
version = "0.0.19"

src/message/mod.rs

+118-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::ops::{Deref, DerefMut};
1010

1111
use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone};
1212
use comrak::{markdown_to_html, ComrakOptions};
13+
use humansize::{format_size, DECIMAL};
1314
use serde_json::json;
1415
use unicode_width::UnicodeWidthStr;
1516

@@ -509,6 +510,27 @@ impl MessageEvent {
509510
}
510511
}
511512

513+
/// Macro rule converting a File / Image / Audio / Video to its text content with the shape:
514+
/// `[Attached <type>: <content>[ (<human readable file size>)]]`
515+
macro_rules! display_file_to_text {
516+
( $msgtype:ident, $content:expr ) => {
517+
return Cow::Owned(format!(
518+
"[Attached {}: {}{}]",
519+
stringify!($msgtype),
520+
$content.body,
521+
$content
522+
.info
523+
.as_ref()
524+
.map(|info| {
525+
info.size
526+
.map(|s| format!(" ({})", format_size(u64::from(s), DECIMAL)))
527+
.unwrap_or_else(String::new)
528+
})
529+
.unwrap_or_else(String::new)
530+
))
531+
};
532+
}
533+
512534
fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
513535
let s = match &content.msgtype {
514536
MessageType::Text(content) => content.body.as_str(),
@@ -518,16 +540,16 @@ fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
518540
MessageType::ServerNotice(content) => content.body.as_str(),
519541

520542
MessageType::Audio(content) => {
521-
return Cow::Owned(format!("[Attached Audio: {}]", content.body));
543+
display_file_to_text!(Audio, content);
522544
},
523545
MessageType::File(content) => {
524-
return Cow::Owned(format!("[Attached File: {}]", content.body));
546+
display_file_to_text!(File, content);
525547
},
526548
MessageType::Image(content) => {
527-
return Cow::Owned(format!("[Attached Image: {}]", content.body));
549+
display_file_to_text!(Image, content);
528550
},
529551
MessageType::Video(content) => {
530-
return Cow::Owned(format!("[Attached Video: {}]", content.body));
552+
display_file_to_text!(Video, content);
531553
},
532554
_ => {
533555
return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype()));
@@ -1130,6 +1152,19 @@ impl ToString for Message {
11301152

11311153
#[cfg(test)]
11321154
pub mod tests {
1155+
use matrix_sdk::ruma::events::room::{
1156+
message::{
1157+
AudioInfo,
1158+
AudioMessageEventContent,
1159+
FileInfo,
1160+
FileMessageEventContent,
1161+
ImageMessageEventContent,
1162+
VideoInfo,
1163+
VideoMessageEventContent,
1164+
},
1165+
ImageInfo,
1166+
};
1167+
11331168
use super::*;
11341169
use crate::tests::*;
11351170

@@ -1379,4 +1414,83 @@ pub mod tests {
13791414
)
13801415
);
13811416
}
1417+
1418+
#[test]
1419+
fn test_display_attachment_size() {
1420+
assert_eq!(
1421+
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
1422+
ImageMessageEventContent::plain(
1423+
"Alt text".to_string(),
1424+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1425+
)
1426+
.info(Some(Box::new(ImageInfo::default())))
1427+
))),
1428+
"[Attached Image: Alt text]".to_string()
1429+
);
1430+
1431+
let mut info = ImageInfo::default();
1432+
info.size = Some(442630_u32.into());
1433+
assert_eq!(
1434+
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
1435+
ImageMessageEventContent::plain(
1436+
"Alt text".to_string(),
1437+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1438+
)
1439+
.info(Some(Box::new(info)))
1440+
))),
1441+
"[Attached Image: Alt text (442.63 kB)]".to_string()
1442+
);
1443+
1444+
let mut info = ImageInfo::default();
1445+
info.size = Some(12_u32.into());
1446+
assert_eq!(
1447+
body_cow_content(&RoomMessageEventContent::new(MessageType::Image(
1448+
ImageMessageEventContent::plain(
1449+
"Alt text".to_string(),
1450+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1451+
)
1452+
.info(Some(Box::new(info)))
1453+
))),
1454+
"[Attached Image: Alt text (12 B)]".to_string()
1455+
);
1456+
1457+
let mut info = AudioInfo::default();
1458+
info.size = Some(4294967295_u32.into());
1459+
assert_eq!(
1460+
body_cow_content(&RoomMessageEventContent::new(MessageType::Audio(
1461+
AudioMessageEventContent::plain(
1462+
"Alt text".to_string(),
1463+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1464+
)
1465+
.info(Some(Box::new(info)))
1466+
))),
1467+
"[Attached Audio: Alt text (4.29 GB)]".to_string()
1468+
);
1469+
1470+
let mut info = FileInfo::default();
1471+
info.size = Some(4426300_u32.into());
1472+
assert_eq!(
1473+
body_cow_content(&RoomMessageEventContent::new(MessageType::File(
1474+
FileMessageEventContent::plain(
1475+
"Alt text".to_string(),
1476+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1477+
)
1478+
.info(Some(Box::new(info)))
1479+
))),
1480+
"[Attached File: Alt text (4.43 MB)]".to_string()
1481+
);
1482+
1483+
let mut info = VideoInfo::default();
1484+
info.size = Some(44000_u32.into());
1485+
assert_eq!(
1486+
body_cow_content(&RoomMessageEventContent::new(MessageType::Video(
1487+
VideoMessageEventContent::plain(
1488+
"Alt text".to_string(),
1489+
"mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into()
1490+
)
1491+
.info(Some(Box::new(info)))
1492+
))),
1493+
"[Attached Video: Alt text (44 kB)]".to_string()
1494+
);
1495+
}
13821496
}

0 commit comments

Comments
 (0)