Commit 497be7f099

497be7f0998d850e123362aa0ac2f8fdb8362066

parent: 64e4f67e43

Unregistered key

Aurore Poirier <Lugrim@users.noreply.github.com> · 2024-05-25 23:16 UTC
committer: <noreply@github.com>

Display file sizes for attachments (#278)

Layout: unified · split

Cargo.lock +16
@@ -2029,6 +2029,15 @@ version = "1.0.3"
20292029source = "registry+https://github.com/rust-lang/crates.io-index"
20302030checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
20312031
2032[[package]]
2033name = "humansize"
2034version = "2.1.3"
2035source = "registry+https://github.com/rust-lang/crates.io-index"
2036checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
2037dependencies = [
2038 "libm",
2039]
2040
20322041[[package]]
20332042name = "hyper"
20342043version = "0.14.28"
@@ -2095,6 +2104,7 @@ dependencies = [
20952104 "futures",
20962105 "gethostname",
20972106 "html5ever",
2107 "humansize",
20982108 "image",
20992109 "lazy_static 1.4.0",
21002110 "libc",
@@ -2461,6 +2471,12 @@ dependencies = [
24612471 "windows-targets 0.48.5",
24622472]
24632473
2474[[package]]
2475name = "libm"
2476version = "0.2.8"
2477source = "registry+https://github.com/rust-lang/crates.io-index"
2478checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
2479
24642480[[package]]
24652481name = "libredox"
24662482version = "0.1.3"
Cargo.toml +1
@@ -61,6 +61,7 @@ unicode-segmentation = "^1.7"
6161unicode-width = "0.1.10"
6262url = {version = "^2.2.2", features = ["serde"]}
6363edit = "0.1.4"
64humansize = "2.0.0"
6465
6566[dependencies.modalkit]
6667version = "0.0.19"
src/message/mod.rs +118 −4
@@ -10,6 +10,7 @@ use std::ops::{Deref, DerefMut};
1010
1111use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone};
1212use comrak::{markdown_to_html, ComrakOptions};
13use humansize::{format_size, DECIMAL};
1314use serde_json::json;
1415use 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>)]]`
515macro_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
512534fn 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)]
11321154pub 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}