From 6438d334cb195af23967f28f55e7bd207e1938db Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 26 Jul 2020 23:12:01 -0400 Subject: fix atom/RSS feeds (#186) * fix atom feeds * also fix RSS feeds * add feeds fixed/flight journal post * fix tests --- src/app.rs | 38 +------------------------------------ src/handlers/feeds.rs | 19 +++++++------------ src/post/mod.rs | 52 +++------------------------------------------------ 3 files changed, 11 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 7b5b377..5ffca7c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,5 @@ use crate::{post::Post, signalboost::Person}; use anyhow::Result; -use atom_syndication as atom; use comrak::{markdown_to_html, ComrakOptions}; use serde::Deserialize; use std::{fs, path::PathBuf}; @@ -65,8 +64,6 @@ pub struct State { pub talks: Vec, pub everything: Vec, pub jf: jsonfeed::Feed, - pub rf: rss::Channel, - pub af: atom::Feed, pub sitemap: Vec, pub patrons: Option, } @@ -93,9 +90,6 @@ pub async fn init(cfg: PathBuf) -> Result { everything.sort(); everything.reverse(); - let mut ri: Vec = vec![]; - let mut ai: Vec = vec![]; - let mut jfb = jsonfeed::Feed::builder() .title("Christine Dodrill's Blog") .description("My blog posts and rants about various technology things.") @@ -114,37 +108,8 @@ pub async fn init(cfg: PathBuf) -> Result { for post in &everything { let post = post.clone(); jfb = jfb.item(post.clone().into()); - ri.push(post.clone().into()); - ai.push(post.clone().into()); } - let af = { - let mut af = atom::FeedBuilder::default(); - af.title("Christine Dodrill's Blog"); - af.id("https://christine.website/blog"); - af.generator({ - let mut generator = atom::Generator::default(); - generator.set_value(env!("CARGO_PKG_NAME")); - generator.set_version(env!("CARGO_PKG_VERSION").to_string()); - generator.set_uri("https://github.com/Xe/site".to_string()); - - generator - }); - af.entries(ai); - - af.build().unwrap() - }; - - let rf = { - let mut rf = rss::ChannelBuilder::default(); - rf.title("Christine Dodrill's Blog"); - rf.link("https://christine.website/blog"); - rf.generator(crate::APPLICATION_NAME.to_string()); - rf.items(ri); - - rf.build().unwrap() - }; - let mut sm: Vec = vec![]; let smw = sitemap::writer::SiteMapWriter::new(&mut sm); let mut urlwriter = smw.start_urlset()?; @@ -173,8 +138,6 @@ pub async fn init(cfg: PathBuf) -> Result { talks: talks, everything: everything, jf: jfb.build(), - af: af, - rf: rf, sitemap: sm, patrons: patrons().await?, }) @@ -185,6 +148,7 @@ mod tests { use anyhow::Result; #[tokio::test] async fn init() -> Result<()> { + let _ = pretty_env_logger::try_init(); super::init("./config.dhall".into()).await?; Ok(()) } diff --git a/src/handlers/feeds.rs b/src/handlers/feeds.rs index 7d7db32..752b08c 100644 --- a/src/handlers/feeds.rs +++ b/src/handlers/feeds.rs @@ -1,7 +1,7 @@ -use crate::app::State; +use crate::{app::State, templates}; use lazy_static::lazy_static; use prometheus::{opts, register_int_counter_vec, IntCounterVec}; -use std::sync::Arc; +use std::{sync::Arc, io}; use warp::{http::Response, Rejection, Reply}; lazy_static! { @@ -20,9 +20,8 @@ pub async fn jsonfeed(state: Arc) -> Result { #[derive(Debug)] pub enum RenderError { - WriteAtom(atom_syndication::Error), - WriteRss(rss::Error), Build(warp::http::Error), + IO(io::Error), } impl warp::reject::Reject for RenderError {} @@ -31,10 +30,8 @@ pub async fn atom(state: Arc) -> Result { HIT_COUNTER.with_label_values(&["atom"]).inc(); let state = state.clone(); let mut buf = Vec::new(); - state - .af - .write_to(&mut buf) - .map_err(RenderError::WriteAtom) + templates::blog_atom_xml(&mut buf, state.everything.clone()) + .map_err(RenderError::IO) .map_err(warp::reject::custom)?; Response::builder() .status(200) @@ -48,10 +45,8 @@ pub async fn rss(state: Arc) -> Result { HIT_COUNTER.with_label_values(&["rss"]).inc(); let state = state.clone(); let mut buf = Vec::new(); - state - .rf - .write_to(&mut buf) - .map_err(RenderError::WriteRss) + templates::blog_rss_xml(&mut buf, state.everything.clone()) + .map_err(RenderError::IO) .map_err(warp::reject::custom)?; Response::builder() .status(200) diff --git a/src/post/mod.rs b/src/post/mod.rs index ca1f3b6..20f18e4 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -1,5 +1,4 @@ use anyhow::{anyhow, Result}; -use atom_syndication as atom; use chrono::prelude::*; use glob::glob; use std::{cmp::Ordering, fs}; @@ -53,54 +52,6 @@ impl Into for Post { } } -impl Into for Post { - fn into(self) -> atom::Entry { - let mut content = atom::ContentBuilder::default(); - - content.src(format!("https://christine.website/{}", self.link)); - content.content_type(Some("text/html;charset=utf-8".into())); - content.value(Some(xml::escape::escape_str_pcdata(&self.body_html).into())); - - let content = content.build().unwrap(); - - let mut result = atom::EntryBuilder::default(); - result.id(format!("https://christine.website/{}", self.link)); - result.contributors({ - let mut me = atom::Person::default(); - - me.set_name("Christine Dodrill"); - me.set_email("me@christine.website".to_string()); - me.set_uri("https://christine.website".to_string()); - - vec![me] - }); - result.title(self.front_matter.title); - let mut link = atom::Link::default(); - link.href = format!("https://christine.website/{}", self.link); - result.links(vec![link]); - result.content(content); - result.published(self.date); - - result.build().unwrap() - } -} - -impl Into for Post { - fn into(self) -> rss::Item { - let mut guid = rss::Guid::default(); - guid.set_value(format!("https://christine.website/{}", self.link)); - let mut result = rss::ItemBuilder::default(); - result.title(Some(self.front_matter.title)); - result.link(format!("https://christine.website/{}", self.link)); - result.guid(guid); - result.author(Some("me@christine.website (Christine Dodrill)".to_string())); - result.content(self.body_html); - result.pub_date(self.date.to_rfc2822()); - - result.build().unwrap() - } -} - impl Ord for Post { fn cmp(&self, other: &Self) -> Ordering { self.partial_cmp(&other).unwrap() @@ -160,18 +111,21 @@ mod tests { #[test] fn blog() -> Result<()> { + let _ = pretty_env_logger::try_init(); load("blog")?; Ok(()) } #[test] fn gallery() -> Result<()> { + let _ = pretty_env_logger::try_init(); load("gallery")?; Ok(()) } #[test] fn talks() -> Result<()> { + let _ = pretty_env_logger::try_init(); load("talks")?; Ok(()) } -- cgit v1.2.3