web & server: plumb debugging info for content type hierarchy.

Also cleanup Email trait.
This commit is contained in:
2023-11-27 13:47:02 -08:00
parent 87dfe4ace7
commit 48466808d3
4 changed files with 128 additions and 43 deletions

View File

@@ -77,6 +77,7 @@ impl UnhandledContentType {
#[derive(Debug)]
pub struct PlainText {
text: String,
content_tree: String,
}
#[Object]
@@ -84,11 +85,15 @@ impl PlainText {
async fn contents(&self) -> &str {
&self.text
}
async fn content_tree(&self) -> &str {
&self.content_tree
}
}
#[derive(Debug)]
pub struct Html {
html: String,
content_tree: String,
}
#[Object]
@@ -96,6 +101,9 @@ impl Html {
async fn contents(&self) -> &str {
&self.html
}
async fn content_tree(&self) -> &str {
&self.content_tree
}
}
#[derive(Debug, Union)]
@@ -105,6 +113,21 @@ pub enum Body {
Html(Html),
}
impl Body {
fn html(html: String) -> Body {
Body::Html(Html {
html,
content_tree: "".to_string(),
})
}
fn text(text: String) -> Body {
Body::PlainText(PlainText {
text,
content_tree: "".to_string(),
})
}
}
#[derive(Debug, SimpleObject)]
pub struct Email {
pub name: Option<String>,
@@ -216,6 +239,12 @@ impl QueryRoot {
// TODO(wathiede): normalize all email addresses through an address book with preferred
// display names (that default to the most commonly seen name).
let nm = ctx.data_unchecked::<Notmuch>();
let debug_content_tree = ctx
.look_ahead()
.field("messages")
.field("body")
.field("contentTree")
.exists();
let mut messages = Vec::new();
for path in nm.files(&thread_id)? {
let path = path?;
@@ -243,8 +272,21 @@ impl QueryRoot {
.get_first_value("date")
.and_then(|d| mailparse::dateparse(&d).ok());
let body = match extract_body(&m)? {
Body::Html(Html { html }) => Body::Html(Html {
Body::PlainText(PlainText { text, content_tree }) => Body::PlainText(PlainText {
text,
content_tree: if debug_content_tree {
render_content_type_tree(&m)
} else {
content_tree
},
}),
Body::Html(Html { html, content_tree }) => Body::Html(Html {
html: ammonia::clean(&html),
content_tree: if debug_content_tree {
render_content_type_tree(&m)
} else {
content_tree
},
}),
b => b,
};
@@ -274,8 +316,8 @@ impl QueryRoot {
fn extract_body(m: &ParsedMail) -> Result<Body, Error> {
let body = m.get_body()?;
let ret = match m.ctype.mimetype.as_str() {
"text/plain" => return Ok(Body::PlainText(PlainText { text: body })),
"text/html" => return Ok(Body::Html(Html { html: body })),
"text/plain" => return Ok(Body::text(body)),
"text/html" => return Ok(Body::html(body)),
"multipart/mixed" => extract_mixed(m),
"multipart/alternative" => extract_alternative(m),
_ => extract_unhandled(m),
@@ -301,13 +343,13 @@ fn extract_alternative(m: &ParsedMail) -> Result<Body, Error> {
for sp in &m.subparts {
if sp.ctype.mimetype == "text/html" {
let body = sp.get_body()?;
return Ok(Body::Html(Html { html: body }));
return Ok(Body::html(body));
}
}
for sp in &m.subparts {
if sp.ctype.mimetype == "text/plain" {
let body = sp.get_body()?;
return Ok(Body::PlainText(PlainText { text: body }));
return Ok(Body::text(body));
}
}
Err("extract_alternative".into())
@@ -327,8 +369,8 @@ fn extract_mixed(m: &ParsedMail) -> Result<Body, Error> {
for sp in &m.subparts {
let body = sp.get_body()?;
match sp.ctype.mimetype.as_str() {
"text/plain" => return Ok(Body::PlainText(PlainText { text: body })),
"text/html" => return Ok(Body::Html(Html { html: body })),
"text/plain" => return Ok(Body::text(body)),
"text/html" => return Ok(Body::html(body)),
_ => (),
}
}
@@ -340,13 +382,13 @@ fn extract_related(m: &ParsedMail) -> Result<Body, Error> {
for sp in &m.subparts {
if sp.ctype.mimetype == "text/html" {
let body = sp.get_body()?;
return Ok(Body::Html(Html { html: body }));
return Ok(Body::html(body));
}
}
for sp in &m.subparts {
if sp.ctype.mimetype == "text/plain" {
let body = sp.get_body()?;
return Ok(Body::PlainText(PlainText { text: body }));
return Ok(Body::text(body));
}
}
Err("extract_related".into())