123 lines
3.3 KiB
Rust
123 lines
3.3 KiB
Rust
use crate::Message;
|
|
|
|
use atom_syndication::{
|
|
ContentBuilder, Entry, EntryBuilder, Feed, FeedBuilder, Generator, LinkBuilder, Person,
|
|
};
|
|
use chrono::{DateTime, TimeZone, Utc};
|
|
|
|
use mail_parser::HeaderValue;
|
|
|
|
pub(crate) fn add_entry_to_feed(
|
|
feed: &mut Feed,
|
|
message: &Message,
|
|
processed_html: &String,
|
|
hostname: &String,
|
|
include_html: bool,
|
|
) {
|
|
let parsed = message.get_parsed().unwrap();
|
|
let date = parsed.get_date().expect("Could not extract date");
|
|
let from = match parsed.get_from() {
|
|
HeaderValue::Address(e) => e,
|
|
_ => return,
|
|
};
|
|
|
|
let path = crate::get_path(&parsed, message);
|
|
|
|
let url = format!("https://{}/{}", hostname, &path);
|
|
|
|
let mut entry: Entry = Newsletter {
|
|
author: Person {
|
|
name: match &from.name {
|
|
Some(n) => n.to_string(),
|
|
_ => match &from.address {
|
|
Some(e) => e.to_string(),
|
|
_ => "".to_string(),
|
|
},
|
|
},
|
|
email: match &from.address {
|
|
Some(e) => Some(e.to_string()),
|
|
_ => None,
|
|
},
|
|
uri: None,
|
|
},
|
|
title: parsed
|
|
.get_subject()
|
|
.expect("Expected a subject")
|
|
.to_string(),
|
|
content: Some(processed_html.clone()),
|
|
id: url.clone(),
|
|
published: Utc.timestamp(date.to_timestamp(), 0), //(format!("{}{}", &date.to_iso8601(), "+00:00").as_str()).`unwrap(),
|
|
url: match include_html {
|
|
true => url,
|
|
false => "".to_string(),
|
|
},
|
|
}
|
|
.into();
|
|
entry.set_updated(Utc.timestamp(date.to_timestamp(), 0));
|
|
feed.entries.push(entry);
|
|
}
|
|
|
|
pub(crate) fn build_atom_feed(hostname: &String) -> Feed {
|
|
FeedBuilder::default()
|
|
.title("JJKiers Newsletters")
|
|
.id(format!("https://{}/feed.atom", hostname))
|
|
.link(
|
|
LinkBuilder::default()
|
|
.href(format!("https://{}/", hostname))
|
|
.rel("alternate")
|
|
.build(),
|
|
)
|
|
.link(
|
|
LinkBuilder::default()
|
|
.href(format!("https://{}/feed.atom", hostname))
|
|
.rel("self")
|
|
.build(),
|
|
)
|
|
.generator(Generator {
|
|
value: String::from("newsletter-to-web"),
|
|
uri: None,
|
|
version: Some(String::from("0.0.1")),
|
|
})
|
|
.build()
|
|
}
|
|
|
|
//#[derive(Serialize, Deserialize, Debug)]
|
|
pub(crate) struct Newsletter {
|
|
id: String,
|
|
url: String,
|
|
title: String,
|
|
content: Option<String>,
|
|
author: Person,
|
|
published: DateTime<Utc>,
|
|
}
|
|
|
|
impl From<Newsletter> for Entry {
|
|
fn from(post: Newsletter) -> Self {
|
|
let content = post.content.map(|v| {
|
|
ContentBuilder::default()
|
|
.value(v)
|
|
.content_type(Some("html".to_string()))
|
|
.build()
|
|
});
|
|
|
|
let mut eb = EntryBuilder::default();
|
|
|
|
eb.title(post.title)
|
|
.id(post.id)
|
|
.published(Some(post.published.clone().into()))
|
|
.author(post.author.into())
|
|
.content(content);
|
|
|
|
if post.url.len() > 1 {
|
|
eb.link(
|
|
LinkBuilder::default()
|
|
.href(post.url)
|
|
.rel("alternate")
|
|
.build(),
|
|
);
|
|
}
|
|
|
|
eb.build()
|
|
}
|
|
}
|