mail-parser's body_html() synthesises HTML from a plain-text body when a message has no HTML part, so every plain-text mail was stored — and shown by both clients — as HTML. Only a genuine text/html part is kept now. Bodies stored before this that may be affected are flagged by migration 0011 and re-parsed from their raw message once at startup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b718bc4c3a
commit
e5cba55989
5 files changed
+110
-1
No files matched your search
@@ -0,0 +1,6 @@
|
||||
-- Plain-text mail used to be stored with an HTML body the parser had
|
||||
-- synthesised from the text (see src/imap_sync/parse.rs), so clients showed
|
||||
-- it as HTML. Rows that may be affected are flagged here and re-parsed from
|
||||
-- their stored raw message once, at startup (src/imap_sync/body_backfill.rs).
|
||||
ALTER TABLE bodies ADD COLUMN html_checked BOOLEAN NOT NULL DEFAULT true;
|
||||
UPDATE bodies SET html_checked = false WHERE html_body LIKE '<html><body>%';
|
||||
@@ -0,0 +1,63 @@
|
||||
//! One-time repair for bodies stored before plain-text mail stopped getting
|
||||
//! a synthesised HTML body (migration 0011): re-parses each flagged message
|
||||
//! from its stored raw copy and keeps HTML only if it really had some.
|
||||
|
||||
use crate::imap_sync::parse::parse_raw_message;
|
||||
use crate::storage::BlobStore;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
const BATCH: i64 = 200;
|
||||
|
||||
pub async fn run(pool: PgPool, blobs: BlobStore) {
|
||||
match repair(&pool, &blobs).await {
|
||||
Ok(0) => {}
|
||||
Ok(n) => tracing::info!(repaired = n, "re-checked stored HTML bodies"),
|
||||
Err(e) => tracing::error!(error = %e, "HTML body backfill failed"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn repair(pool: &PgPool, blobs: &BlobStore) -> anyhow::Result<usize> {
|
||||
let mut total = 0;
|
||||
loop {
|
||||
let batch: Vec<(Uuid, Option<String>)> = sqlx::query_as(
|
||||
"SELECT b.message_id, m.raw_blob_ref FROM bodies b JOIN messages m ON m.id = b.message_id \
|
||||
WHERE NOT b.html_checked LIMIT $1",
|
||||
)
|
||||
.bind(BATCH)
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
if batch.is_empty() {
|
||||
return Ok(total);
|
||||
}
|
||||
for (id, raw_ref) in batch {
|
||||
// Without the raw message the stored body is the best there is.
|
||||
let html = match raw_ref {
|
||||
Some(r) => match blobs.read(&r).await.and_then(|raw| parse_raw_message(&raw)) {
|
||||
Ok(parsed) => Some(parsed.html_body),
|
||||
Err(e) => {
|
||||
tracing::warn!(message = %id, error = %e, "couldn't re-parse; leaving body as is");
|
||||
None
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
};
|
||||
match html {
|
||||
Some(html) => sqlx::query(
|
||||
"UPDATE bodies SET html_body = $2, html_checked = true WHERE message_id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(html)
|
||||
.execute(pool)
|
||||
.await?,
|
||||
None => {
|
||||
sqlx::query("UPDATE bodies SET html_checked = true WHERE message_id = $1")
|
||||
.bind(id)
|
||||
.execute(pool)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
total += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod backoff;
|
||||
pub mod body_backfill;
|
||||
pub mod connection;
|
||||
pub mod manager;
|
||||
pub mod parse;
|
||||
|
||||
+38
-1
@@ -36,7 +36,14 @@ pub fn parse_raw_message(raw: &[u8]) -> anyhow::Result<ParsedMessage> {
|
||||
.map(|d| d.with_timezone(&Utc));
|
||||
|
||||
let text_body = message.body_text(0).map(|s| s.to_string());
|
||||
let html_body = message.body_html(0).map(|s| s.to_string());
|
||||
// Only a real text/html part: mail-parser's body_html() otherwise
|
||||
// synthesises HTML from a plain-text body, which made every plain
|
||||
// message look like HTML mail to the clients.
|
||||
let html_body = message
|
||||
.html_part(0)
|
||||
.filter(|p| p.is_text_html())
|
||||
.and_then(|_| message.body_html(0))
|
||||
.map(|s| s.to_string());
|
||||
|
||||
let attachments = message
|
||||
.attachments()
|
||||
@@ -131,4 +138,34 @@ body\r\n";
|
||||
assert_eq!(get("x-spam-score"), vec!["5.3"]);
|
||||
assert_eq!(get("from"), vec!["Bob <bob@example.com>"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plain_text_mail_has_no_html_body() {
|
||||
let plain = parse_raw_message(
|
||||
b"From: a@example.com\r\nSubject: hi\r\nContent-Type: text/plain\r\n\r\nJust text.\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
plain.text_body.as_deref().map(str::trim),
|
||||
Some("Just text.")
|
||||
);
|
||||
assert_eq!(plain.html_body, None, "no HTML synthesised from text");
|
||||
|
||||
let both = parse_raw_message(
|
||||
b"From: a@example.com\r\nSubject: hi\r\nMIME-Version: 1.0\r\n\
|
||||
Content-Type: multipart/alternative; boundary=b\r\n\r\n\
|
||||
--b\r\nContent-Type: text/plain\r\n\r\nPlain\r\n\
|
||||
--b\r\nContent-Type: text/html\r\n\r\n<p>Rich</p>\r\n--b--\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(both.html_body.unwrap().contains("<p>Rich</p>"));
|
||||
|
||||
// HTML-only mail still gets a text version (notifications, rules).
|
||||
let html_only = parse_raw_message(
|
||||
b"From: a@example.com\r\nSubject: hi\r\nContent-Type: text/html\r\n\r\n<p>Only <b>rich</b></p>\r\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(html_only.html_body.is_some());
|
||||
assert!(html_only.text_body.unwrap().contains("rich"));
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
events.clone(),
|
||||
));
|
||||
|
||||
tokio::spawn(imap_sync::body_backfill::run(pool.clone(), blobs.clone()));
|
||||
|
||||
tokio::spawn(tern_server::retention::run(
|
||||
pool.clone(),
|
||||
blobs.clone(),
|
||||
|
||||
Reference in new issue
Block a user