Skip to content

Commit 8c9efc6

Browse files
committed
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.
1 parent e694411 commit 8c9efc6

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/chat.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,13 +2104,19 @@ impl Chat {
21042104
} else {
21052105
None
21062106
};
2107+
let new_mime_headers = new_mime_headers.map(|s| new_html_mimepart(s).build().as_string());
21072108
let new_mime_headers = new_mime_headers.or_else(|| match was_truncated {
2108-
true => Some(msg.text.clone()),
2109+
// We need to add some headers so that they are stripped before formatting HTML by
2110+
// `MsgId::get_html()`, not a part of the actual text. Let's add "Content-Type", it's
2111+
// anyway a useful metadata about the stored text.
2112+
true => Some(
2113+
"Content-Type: text/plain; charset=utf-8\r\n\r\n".to_string() + &msg.text + "\r\n",
2114+
),
21092115
false => None,
21102116
});
21112117
let new_mime_headers = match new_mime_headers {
21122118
Some(h) => Some(tokio::task::block_in_place(move || {
2113-
buf_compress(new_html_mimepart(h).build().as_string().as_bytes())
2119+
buf_compress(h.as_bytes())
21142120
})?),
21152121
None => None,
21162122
};

src/mimeparser.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,9 +3653,10 @@ On 2020-10-25, Bob wrote:
36533653
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
36543654
async fn test_mime_modified_large_plain() -> Result<()> {
36553655
let t = TestContext::new_alice().await;
3656+
let t1 = TestContext::new_alice().await;
36563657

36573658
static REPEAT_TXT: &str = "this text with 42 chars is just repeated.\n";
3658-
static REPEAT_CNT: usize = 2000; // results in a text of 84k, should be more than DC_DESIRED_TEXT_LEN
3659+
static REPEAT_CNT: usize = DC_DESIRED_TEXT_LEN / REPEAT_TXT.len() + 2;
36593660
let long_txt = format!("From: alice@c.de\n\n{}", REPEAT_TXT.repeat(REPEAT_CNT));
36603661
assert_eq!(long_txt.matches("just repeated").count(), REPEAT_CNT);
36613662
assert!(long_txt.len() > DC_DESIRED_TEXT_LEN);
@@ -3676,22 +3677,21 @@ On 2020-10-25, Bob wrote:
36763677
if draft {
36773678
chat.id.set_draft(&t, Some(&mut msg)).await?;
36783679
}
3679-
t.send_msg(chat.id, &mut msg).await;
3680+
let sent_msg = t.send_msg(chat.id, &mut msg).await;
36803681
let msg = t.get_last_msg_in(chat.id).await;
36813682
assert!(msg.has_html());
3682-
assert_eq!(
3683-
msg.id
3684-
.get_html(&t)
3685-
.await?
3686-
.unwrap()
3687-
.matches("just repeated")
3688-
.count(),
3689-
REPEAT_CNT
3690-
);
3683+
let html = msg.id.get_html(&t).await?.unwrap();
3684+
assert_eq!(html.matches("<!DOCTYPE html>").count(), 1);
3685+
assert_eq!(html.matches("just repeated.<br/>").count(), REPEAT_CNT);
36913686
assert!(
3692-
msg.text.matches("just repeated").count() <= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len()
3687+
msg.text.matches("just repeated.").count()
3688+
<= DC_DESIRED_TEXT_LEN / REPEAT_TXT.len()
36933689
);
36943690
assert!(msg.text.len() <= DC_DESIRED_TEXT_LEN + DC_ELLIPSIS.len());
3691+
3692+
let msg = t1.recv_msg(&sent_msg).await;
3693+
assert!(msg.has_html());
3694+
assert_eq!(msg.id.get_html(&t1).await?.unwrap(), html);
36953695
}
36963696

36973697
t.set_config(Config::Bot, Some("1")).await?;

0 commit comments

Comments
 (0)