| @@ -10,6 +10,7 @@ use std::ops::{Deref, DerefMut}; |
| 10 | |
10 | |
| 11 | use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone}; |
11 | use chrono::{DateTime, Local as LocalTz, NaiveDateTime, TimeZone}; |
| 12 | use comrak::{markdown_to_html, ComrakOptions}; |
12 | use comrak::{markdown_to_html, ComrakOptions}; |
| |
13 | use humansize::{format_size, DECIMAL}; |
| 13 | use serde_json::json; |
14 | use serde_json::json; |
| 14 | use unicode_width::UnicodeWidthStr; |
15 | use 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>)]]` |
| |
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 | |
| 512 | fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> { |
534 | fn 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)] |
| 1132 | pub mod tests { |
1154 | 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 | |
| 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 | } |