From 4d5ab2bea4dce2f11ed65e36e7d63fcdd08eb76c Mon Sep 17 00:00:00 2001 From: iequidoo Date: Tue, 3 Dec 2024 15:08:43 -0300 Subject: [PATCH] fix: Store plaintext in mime_headers of truncated sent messages (#6273) This fixes HTML display of truncated (long) sent messages ("Show full message" in UIs). Before, incorrect HTML was stored (with missing line breaks etc.) for them. Now stored plaintext is formatted to HTML upon calling `MsgId::get_html()` and this results in the same HTML as on a receiver side. --- src/chat.rs | 10 ++++++++-- src/mimeparser.rs | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/chat.rs b/src/chat.rs index e57372c931..abd3cc5c60 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -2104,13 +2104,19 @@ impl Chat { } else { None }; + let new_mime_headers = new_mime_headers.map(|s| new_html_mimepart(s).build().as_string()); let new_mime_headers = new_mime_headers.or_else(|| match was_truncated { - true => Some(msg.text.clone()), + // We need to add some headers so that they are stripped before formatting HTML by + // `MsgId::get_html()`, not a part of the actual text. Let's add "Content-Type", it's + // anyway a useful metadata about the stored text. + true => Some( + "Content-Type: text/plain; charset=utf-8\r\n\r\n".to_string() + &msg.text + "\r\n", + ), false => None, }); let new_mime_headers = match new_mime_headers { Some(h) => Some(tokio::task::block_in_place(move || { - buf_compress(new_html_mimepart(h).build().as_string().as_bytes()) + buf_compress(h.as_bytes()) })?), None => None, }; diff --git a/src/mimeparser.rs b/src/mimeparser.rs index 7ada490010..6fcc9603c7 100644 --- a/src/mimeparser.rs +++ b/src/mimeparser.rs @@ -3653,9 +3653,10 @@ On 2020-10-25, Bob wrote: #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_mime_modified_large_plain() -> Result<()> { let t = TestContext::new_alice().await; + let t1 = TestContext::new_alice().await; static REPEAT_TXT: &str = "this text with 42 chars is just repeated.\n"; - static REPEAT_CNT: usize = 2000; // results in a text of 84k, should be more than DC_DESIRED_TEXT_LEN + static REPEAT_CNT: usize = DC_DESIRED_TEXT_LEN / REPEAT_TXT.len() + 2; let long_txt = format!("From: alice@c.de\n\n{}", REPEAT_TXT.repeat(REPEAT_CNT)); assert_eq!(long_txt.matches("just repeated").count(), REPEAT_CNT); assert!(long_txt.len() > DC_DESIRED_TEXT_LEN); @@ -3676,22 +3677,21 @@ On 2020-10-25, Bob wrote: if draft { chat.id.set_draft(&t, Some(&mut msg)).await?; } - t.send_msg(chat.id, &mut msg).await; + let sent_msg = t.send_msg(chat.id, &mut msg).await; let msg = t.get_last_msg_in(chat.id).await; assert!(msg.has_html()); - assert_eq!( - msg.id - .get_html(&t) - .await? - .unwrap() - .matches("just repeated") - .count(), - REPEAT_CNT - ); + let html = msg.id.get_html(&t).await?.unwrap(); + assert_eq!(html.matches("").count(), 1); + assert_eq!(html.matches("just repeated.
").count(), REPEAT_CNT); assert!( - msg.text.matches("just repeated").count() <= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len() + msg.text.matches("just repeated.").count() + <= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len() ); assert!(msg.text.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len()); + + let msg = t1.recv_msg(&sent_msg).await; + assert!(msg.has_html()); + assert_eq!(msg.id.get_html(&t1).await?.unwrap(), html); } t.set_config(Config::Bot, Some("1")).await?;