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"
2029source = "registry+https://github.com/rust-lang/crates.io-index" 2029source = "registry+https://github.com/rust-lang/crates.io-index"
2030checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" 2030checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
2031 2031
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
2032[[package]] 2041[[package]]
2033name = "hyper" 2042name = "hyper"
2034version = "0.14.28" 2043version = "0.14.28"
@@ -2095,6 +2104,7 @@ dependencies = [
2095 "futures", 2104 "futures",
2096 "gethostname", 2105 "gethostname",
2097 "html5ever", 2106 "html5ever",
2107 "humansize",
2098 "image", 2108 "image",
2099 "lazy_static 1.4.0", 2109 "lazy_static 1.4.0",
2100 "libc", 2110 "libc",
@@ -2461,6 +2471,12 @@ dependencies = [
2461 "windows-targets 0.48.5", 2471 "windows-targets 0.48.5",
2462] 2472]
2463 2473
2474[[package]]
2475name = "libm"
2476version = "0.2.8"
2477source = "registry+https://github.com/rust-lang/crates.io-index"
2478checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
2479
2464[[package]] 2480[[package]]
2465name = "libredox" 2481name = "libredox"
2466version = "0.1.3" 2482version = "0.1.3"
Cargo.toml +1
@@ -61,6 +61,7 @@ unicode-segmentation = "^1.7"
61unicode-width = "0.1.10" 61unicode-width = "0.1.10"
62url = {version = "^2.2.2", features = ["serde"]} 62url = {version = "^2.2.2", features = ["serde"]}
63edit = "0.1.4" 63edit = "0.1.4"
64humansize = "2.0.0"
64 65
65[dependencies.modalkit] 66[dependencies.modalkit]
66version = "0.0.19" 67version = "0.0.19"
src/message/mod.rs +118 −4
@@ -10,6 +10,7 @@ use std::ops::{Deref, DerefMut};
10 10
11use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone}; 11use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone};
12use comrak::{markdown_to_html, ComrakOptions}; 12use comrak::{markdown_to_html, ComrakOptions};
13use humansize::{format_size, DECIMAL};
13use serde_json::json; 14use serde_json::json;
14use unicode_width::UnicodeWidthStr; 15use unicode_width::UnicodeWidthStr;
15 16
@@ -509,6 +510,27 @@ impl MessageEvent {
509 } 510 }
510} 511}
511 512
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
512fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> { 534fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
513 let s = match &content.msgtype { 535 let s = match &content.msgtype {
514 MessageType::Text(content) => content.body.as_str(), 536 MessageType::Text(content) => content.body.as_str(),
@@ -518,16 +540,16 @@ fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
518 MessageType::ServerNotice(content) => content.body.as_str(), 540 MessageType::ServerNotice(content) => content.body.as_str(),
519 541
520 MessageType::Audio(content) => { 542 MessageType::Audio(content) => {
521 return Cow::Owned(format!("[Attached Audio: {}]", content.body)); 543 display_file_to_text!(Audio, content);
522 }, 544 },
523 MessageType::File(content) => { 545 MessageType::File(content) => {
524 return Cow::Owned(format!("[Attached File: {}]", content.body)); 546 display_file_to_text!(File, content);
525 }, 547 },
526 MessageType::Image(content) => { 548 MessageType::Image(content) => {
527 return Cow::Owned(format!("[Attached Image: {}]", content.body)); 549 display_file_to_text!(Image, content);
528 }, 550 },
529 MessageType::Video(content) => { 551 MessageType::Video(content) => {
530 return Cow::Owned(format!("[Attached Video: {}]", content.body)); 552 display_file_to_text!(Video, content);
531 }, 553 },
532 _ => { 554 _ => {
533 return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype())); 555 return Cow::Owned(format!("[Unknown message type: {:?}]", content.msgtype()));
@@ -1130,6 +1152,19 @@ impl ToString for Message {
1130 1152
1131#[cfg(test)] 1153#[cfg(test)]
1132pub mod tests { 1154pub 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
1133 use super::*; 1168 use super::*;
1134 use crate::tests::*; 1169 use crate::tests::*;
1135 1170
@@ -1379,4 +1414,83 @@ pub mod tests {
1379 ) 1414 )
1380 ); 1415 );
1381 } 1416 }
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 }
1382} 1496}