web & server: implement handling for text and html bodies.
This commit is contained in:
@@ -6,6 +6,7 @@ use std::{
|
||||
use async_graphql::{
|
||||
connection::{self, Connection, Edge},
|
||||
Context, EmptyMutation, EmptySubscription, Error, FieldResult, Object, Schema, SimpleObject,
|
||||
Union,
|
||||
};
|
||||
use log::{info, warn};
|
||||
use mailparse::{parse_mail, MailHeaderMap, ParsedMail};
|
||||
@@ -55,6 +56,51 @@ pub struct Message {
|
||||
pub subject: Option<String>,
|
||||
// Parsed Date header, if found and valid
|
||||
pub timestamp: Option<i64>,
|
||||
// The body contents
|
||||
pub body: Body,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UnhandledContentType {
|
||||
text: String,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl UnhandledContentType {
|
||||
async fn contents(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct PlainText {
|
||||
text: String,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl PlainText {
|
||||
async fn contents(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Html {
|
||||
html: String,
|
||||
}
|
||||
|
||||
#[Object]
|
||||
impl Html {
|
||||
async fn contents(&self) -> &str {
|
||||
&self.html
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Union)]
|
||||
pub enum Body {
|
||||
UnhandledContentType(UnhandledContentType),
|
||||
PlainText(PlainText),
|
||||
Html(Html),
|
||||
}
|
||||
|
||||
#[derive(Debug, SimpleObject)]
|
||||
@@ -195,12 +241,23 @@ impl QueryRoot {
|
||||
.headers
|
||||
.get_first_value("date")
|
||||
.and_then(|d| mailparse::dateparse(&d).ok());
|
||||
let body = m.get_body()?;
|
||||
let body = match m.ctype.mimetype.as_str() {
|
||||
"text/plain" => Body::PlainText(PlainText { text: body }),
|
||||
"text/html" => Body::Html(Html { html: body }),
|
||||
_ => {
|
||||
let msg = format!("Unhandled body content type: {}", m.ctype.mimetype);
|
||||
warn!("{}", msg);
|
||||
Body::UnhandledContentType(UnhandledContentType { text: msg })
|
||||
}
|
||||
};
|
||||
messages.push(Message {
|
||||
from,
|
||||
to,
|
||||
cc,
|
||||
subject,
|
||||
timestamp,
|
||||
body,
|
||||
});
|
||||
}
|
||||
messages.reverse();
|
||||
|
||||
Reference in New Issue
Block a user