| @@ -73,7 +73,7 @@ mod html; |
| 73 | 73 | mod printer; |
| 74 | 74 | mod state; |
| 75 | 75 | |
| 76 | | pub use self::compose::text_to_message; |
| 76 | pub use self::compose::{text_to_message, text_to_text_message_event_content}; |
| 77 | 77 | use self::state::{body_cow_state, html_state}; |
| 78 | 78 | pub use html::TreeGenState; |
| 79 | 79 | |
| @@ -462,17 +462,33 @@ impl MessageEvent { |
| 462 | 462 | MessageEvent::Local(_, _, content) => content, |
| 463 | 463 | }; |
| 464 | 464 | |
| 465 | | if let MessageType::Text(content) = &content.msgtype { |
| 466 | | if let Some(FormattedBody { format: MessageFormat::Html, body }) = &content.formatted { |
| 467 | | Some(parse_matrix_html(body.as_str())) |
| 468 | | } else { |
| 469 | | None |
| 470 | | } |
| 465 | let formatted = match &content.msgtype { |
| 466 | MessageType::Text(content) => content.formatted.as_ref(), |
| 467 | MessageType::Emote(content) => content.formatted.as_ref(), |
| 468 | MessageType::Notice(content) => content.formatted.as_ref(), |
| 469 | |
| 470 | MessageType::Audio(content) => content.formatted.as_ref(), |
| 471 | MessageType::File(content) => content.formatted.as_ref(), |
| 472 | MessageType::Image(content) => content.formatted.as_ref(), |
| 473 | MessageType::Video(content) => content.formatted.as_ref(), |
| 474 | _ => None, |
| 475 | }; |
| 476 | |
| 477 | if let Some(FormattedBody { format: MessageFormat::Html, body }) = formatted { |
| 478 | Some(parse_matrix_html(body.as_str())) |
| 471 | 479 | } else { |
| 472 | 480 | None |
| 473 | 481 | } |
| 474 | 482 | } |
| 475 | 483 | |
| 484 | pub fn filename(&self) -> Option<String> { |
| 485 | match self { |
| 486 | MessageEvent::Original(ev) => content_filename(&ev.content), |
| 487 | MessageEvent::Local(_, _, content) => content_filename(content), |
| 488 | _ => None, |
| 489 | } |
| 490 | } |
| 491 | |
| 476 | 492 | fn redact(&mut self, redaction: SyncRoomRedactionEvent) { |
| 477 | 493 | match self { |
| 478 | 494 | MessageEvent::EncryptedOriginal(_) => return, |
| @@ -514,12 +530,12 @@ impl MessageEvent { |
| 514 | 530 | |
| 515 | 531 | /// Macro rule converting a File / Image / Audio / Video to its text content with the shape: |
| 516 | 532 | /// `[Attached <type>: <content>[ (<human readable file size>)]]` |
| 517 | | macro_rules! display_file_to_text { |
| 518 | | ( $msgtype:ident, $content:expr ) => { |
| 519 | | return Cow::Owned(format!( |
| 533 | macro_rules! display_file_name { |
| 534 | ( $msgtype:ident, $content:expr ) => {{ |
| 535 | Some(format!( |
| 520 | 536 | "[Attached {}: {}{}]", |
| 521 | 537 | stringify!($msgtype), |
| 522 | | $content.body, |
| 538 | $content.filename(), |
| 523 | 539 | $content |
| 524 | 540 | .info |
| 525 | 541 | .as_ref() |
| @@ -530,7 +546,39 @@ macro_rules! display_file_to_text { |
| 530 | 546 | }) |
| 531 | 547 | .unwrap_or_else(String::new) |
| 532 | 548 | )) |
| 533 | | }; |
| 549 | }}; |
| 550 | } |
| 551 | |
| 552 | /// Macro rule extraction the text caption of a File / Image / Audio / Video |
| 553 | macro_rules! display_file_to_text { |
| 554 | ( $msgtype:ident, $content:expr ) => {{ |
| 555 | if $content |
| 556 | .filename |
| 557 | .as_ref() |
| 558 | .is_none_or(|filename| *filename == $content.body) |
| 559 | { |
| 560 | return Cow::Borrowed(""); |
| 561 | } |
| 562 | $content.body.as_str() |
| 563 | }}; |
| 564 | } |
| 565 | |
| 566 | fn content_filename(content: &RoomMessageEventContent) -> Option<String> { |
| 567 | match &content.msgtype { |
| 568 | MessageType::Audio(content) => { |
| 569 | display_file_name!(Audio, content) |
| 570 | }, |
| 571 | MessageType::File(content) => { |
| 572 | display_file_name!(File, content) |
| 573 | }, |
| 574 | MessageType::Image(content) => { |
| 575 | display_file_name!(Image, content) |
| 576 | }, |
| 577 | MessageType::Video(content) => { |
| 578 | display_file_name!(Video, content) |
| 579 | }, |
| 580 | _ => None, |
| 581 | } |
| 534 | 582 | } |
| 535 | 583 | |
| 536 | 584 | fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> { |
| @@ -542,16 +590,16 @@ fn body_cow_content(content: &RoomMessageEventContent) -> Cow<'_, str> { |
| 542 | 590 | MessageType::ServerNotice(content) => content.body.as_str(), |
| 543 | 591 | |
| 544 | 592 | MessageType::Audio(content) => { |
| 545 | | display_file_to_text!(Audio, content); |
| 593 | display_file_to_text!(Audio, content) |
| 546 | 594 | }, |
| 547 | 595 | MessageType::File(content) => { |
| 548 | | display_file_to_text!(File, content); |
| 596 | display_file_to_text!(File, content) |
| 549 | 597 | }, |
| 550 | 598 | MessageType::Image(content) => { |
| 551 | | display_file_to_text!(Image, content); |
| 599 | display_file_to_text!(Image, content) |
| 552 | 600 | }, |
| 553 | 601 | MessageType::Video(content) => { |
| 554 | | display_file_to_text!(Video, content); |
| 602 | display_file_to_text!(Video, content) |
| 555 | 603 | }, |
| 556 | 604 | _ => content.body(), |
| 557 | 605 | }; |
| @@ -1100,41 +1148,48 @@ impl Message { |
| 1100 | 1148 | settings: &'a ApplicationSettings, |
| 1101 | 1149 | previews: &'a PreviewManager, |
| 1102 | 1150 | ) -> (Text<'a>, Option<&'a Protocol>) { |
| 1103 | | if let Some(html) = &self.html { |
| 1104 | | (html.to_text(width, style, settings), None) |
| 1151 | let mut proto = None; |
| 1152 | let placeholder = match self.image_preview.as_ref().and_then(|source| previews.get(source)) |
| 1153 | { |
| 1154 | None => None, |
| 1155 | Some(ImageStatus::Queued(image_preview_size)) => { |
| 1156 | placeholder_frame(Some("Queued..."), width, image_preview_size) |
| 1157 | }, |
| 1158 | Some(ImageStatus::Downloading(image_preview_size)) => { |
| 1159 | placeholder_frame(Some("Downloading..."), width, image_preview_size) |
| 1160 | }, |
| 1161 | Some(ImageStatus::Loaded(backend)) => { |
| 1162 | proto = Some(backend); |
| 1163 | placeholder_frame(Some("No Space..."), width, &backend.area().into()) |
| 1164 | }, |
| 1165 | Some(ImageStatus::Error(err)) => Some(format!("[Image error: {err}]\n")), |
| 1166 | }; |
| 1167 | |
| 1168 | let mut text = if let Some(placeholder) = placeholder { |
| 1169 | wrapped_text(placeholder, width, style) |
| 1105 | 1170 | } else { |
| 1106 | | let mut msg = self.event.body(); |
| 1107 | | if settings.tunables.message_shortcode_display { |
| 1108 | | msg = Cow::Owned(replace_emojis_in_str(msg.as_ref())); |
| 1109 | | } |
| 1171 | Default::default() |
| 1172 | }; |
| 1110 | 1173 | |
| 1174 | if let Some(mut filename) = self.event.filename() { |
| 1111 | 1175 | if self.downloaded { |
| 1112 | | msg.to_mut().push_str(" \u{2705}"); |
| 1176 | filename.push_str(" \u{2705}"); |
| 1113 | 1177 | } |
| 1114 | 1178 | |
| 1115 | | let mut proto = None; |
| 1116 | | let placeholder = |
| 1117 | | match self.image_preview.as_ref().and_then(|source| previews.get(source)) { |
| 1118 | | None => None, |
| 1119 | | Some(ImageStatus::Queued(image_preview_size)) => { |
| 1120 | | placeholder_frame(Some("Queued..."), width, image_preview_size) |
| 1121 | | }, |
| 1122 | | Some(ImageStatus::Downloading(image_preview_size)) => { |
| 1123 | | placeholder_frame(Some("Downloading..."), width, image_preview_size) |
| 1124 | | }, |
| 1125 | | Some(ImageStatus::Loaded(backend)) => { |
| 1126 | | proto = Some(backend); |
| 1127 | | placeholder_frame(Some("No Space..."), width, &backend.area().into()) |
| 1128 | | }, |
| 1129 | | Some(ImageStatus::Error(err)) => Some(format!("[Image error: {err}]\n")), |
| 1130 | | }; |
| 1131 | | |
| 1132 | | if let Some(placeholder) = placeholder { |
| 1133 | | msg.to_mut().insert_str(0, &placeholder); |
| 1179 | text = text + wrapped_text(filename, width, style); |
| 1180 | } |
| 1181 | |
| 1182 | if let Some(html) = &self.html { |
| 1183 | text = text + html.to_text(width, style, settings); |
| 1184 | } else { |
| 1185 | let mut msg = self.event.body(); |
| 1186 | if settings.tunables.message_shortcode_display { |
| 1187 | msg = Cow::Owned(replace_emojis_in_str(msg.as_ref())); |
| 1134 | 1188 | } |
| 1189 | text = text + wrapped_text(msg, width, style); |
| 1190 | }; |
| 1135 | 1191 | |
| 1136 | | (wrapped_text(msg, width, style), proto) |
| 1137 | | } |
| 1192 | (text, proto) |
| 1138 | 1193 | } |
| 1139 | 1194 | |
| 1140 | 1195 | fn sender_span<'a>( |
| @@ -1490,79 +1545,79 @@ pub mod tests { |
| 1490 | 1545 | #[test] |
| 1491 | 1546 | fn test_display_attachment_size() { |
| 1492 | 1547 | assert_eq!( |
| 1493 | | body_cow_content(&RoomMessageEventContent::new(MessageType::Image( |
| 1548 | content_filename(&RoomMessageEventContent::new(MessageType::Image( |
| 1494 | 1549 | ImageMessageEventContent::plain( |
| 1495 | 1550 | "Alt text".to_string(), |
| 1496 | 1551 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1497 | 1552 | ) |
| 1498 | 1553 | .info(Some(Box::default())) |
| 1499 | 1554 | ))), |
| 1500 | | "[Attached Image: Alt text]".to_string() |
| 1555 | "[Attached Image: Alt text]".to_string().into() |
| 1501 | 1556 | ); |
| 1502 | 1557 | |
| 1503 | 1558 | let mut info = ImageInfo::default(); |
| 1504 | 1559 | info.size = Some(442630_u32.into()); |
| 1505 | 1560 | assert_eq!( |
| 1506 | | body_cow_content(&RoomMessageEventContent::new(MessageType::Image( |
| 1561 | content_filename(&RoomMessageEventContent::new(MessageType::Image( |
| 1507 | 1562 | ImageMessageEventContent::plain( |
| 1508 | 1563 | "Alt text".to_string(), |
| 1509 | 1564 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1510 | 1565 | ) |
| 1511 | 1566 | .info(Some(Box::new(info))) |
| 1512 | 1567 | ))), |
| 1513 | | "[Attached Image: Alt text (442.63 kB)]".to_string() |
| 1568 | "[Attached Image: Alt text (442.63 kB)]".to_string().into() |
| 1514 | 1569 | ); |
| 1515 | 1570 | |
| 1516 | 1571 | let mut info = ImageInfo::default(); |
| 1517 | 1572 | info.size = Some(12_u32.into()); |
| 1518 | 1573 | assert_eq!( |
| 1519 | | body_cow_content(&RoomMessageEventContent::new(MessageType::Image( |
| 1574 | content_filename(&RoomMessageEventContent::new(MessageType::Image( |
| 1520 | 1575 | ImageMessageEventContent::plain( |
| 1521 | 1576 | "Alt text".to_string(), |
| 1522 | 1577 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1523 | 1578 | ) |
| 1524 | 1579 | .info(Some(Box::new(info))) |
| 1525 | 1580 | ))), |
| 1526 | | "[Attached Image: Alt text (12 B)]".to_string() |
| 1581 | "[Attached Image: Alt text (12 B)]".to_string().into() |
| 1527 | 1582 | ); |
| 1528 | 1583 | |
| 1529 | 1584 | let mut info = AudioInfo::default(); |
| 1530 | 1585 | info.size = Some(4294967295_u32.into()); |
| 1531 | 1586 | assert_eq!( |
| 1532 | | body_cow_content(&RoomMessageEventContent::new(MessageType::Audio( |
| 1587 | content_filename(&RoomMessageEventContent::new(MessageType::Audio( |
| 1533 | 1588 | AudioMessageEventContent::plain( |
| 1534 | 1589 | "Alt text".to_string(), |
| 1535 | 1590 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1536 | 1591 | ) |
| 1537 | 1592 | .info(Some(Box::new(info))) |
| 1538 | 1593 | ))), |
| 1539 | | "[Attached Audio: Alt text (4.29 GB)]".to_string() |
| 1594 | "[Attached Audio: Alt text (4.29 GB)]".to_string().into() |
| 1540 | 1595 | ); |
| 1541 | 1596 | |
| 1542 | 1597 | let mut info = FileInfo::default(); |
| 1543 | 1598 | info.size = Some(4426300_u32.into()); |
| 1544 | 1599 | assert_eq!( |
| 1545 | | body_cow_content(&RoomMessageEventContent::new(MessageType::File( |
| 1600 | content_filename(&RoomMessageEventContent::new(MessageType::File( |
| 1546 | 1601 | FileMessageEventContent::plain( |
| 1547 | 1602 | "Alt text".to_string(), |
| 1548 | 1603 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1549 | 1604 | ) |
| 1550 | 1605 | .info(Some(Box::new(info))) |
| 1551 | 1606 | ))), |
| 1552 | | "[Attached File: Alt text (4.43 MB)]".to_string() |
| 1607 | "[Attached File: Alt text (4.43 MB)]".to_string().into() |
| 1553 | 1608 | ); |
| 1554 | 1609 | |
| 1555 | 1610 | let mut info = VideoInfo::default(); |
| 1556 | 1611 | info.size = Some(44000_u32.into()); |
| 1557 | 1612 | assert_eq!( |
| 1558 | | body_cow_content(&RoomMessageEventContent::new(MessageType::Video( |
| 1613 | content_filename(&RoomMessageEventContent::new(MessageType::Video( |
| 1559 | 1614 | VideoMessageEventContent::plain( |
| 1560 | 1615 | "Alt text".to_string(), |
| 1561 | 1616 | "mxc://matrix.org/jDErsDugkNlfavzLTjJNUKAH".into() |
| 1562 | 1617 | ) |
| 1563 | 1618 | .info(Some(Box::new(info))) |
| 1564 | 1619 | ))), |
| 1565 | | "[Attached Video: Alt text (44 kB)]".to_string() |
| 1620 | "[Attached Video: Alt text (44 kB)]".to_string().into() |
| 1566 | 1621 | ); |
| 1567 | 1622 | } |
| 1568 | 1623 | } |