Skip to content

Commit

Permalink
add date to the error post
Browse files Browse the repository at this point in the history
  • Loading branch information
shouya committed Sep 20, 2024
1 parent e1c3d43 commit 41c5594
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
27 changes: 3 additions & 24 deletions src/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,34 +257,13 @@ impl Feed {
}

#[allow(clippy::field_reassign_with_default)]
pub fn add_item(&mut self, title: String, body: String, link: String) {
let guid = link.clone();

pub fn add_post(&mut self, post_preview: PostPreview) {
match self {
Feed::Rss(channel) => {
let mut item = rss::Item::default();
item.title = Some(title);
item.description = Some(body);
item.link = Some(link);
item.guid = Some(rss::Guid {
value: guid,
..Default::default()
});
channel.items.push(item);
channel.items.push(post_preview.into_rss_item());
}
Feed::Atom(feed) => {
let mut entry = atom_syndication::Entry::default();
entry.title = atom_syndication::Text::plain(title);
entry.content = Some(atom_syndication::Content {
value: Some(body),
..Default::default()
});
entry.links.push(atom_syndication::Link {
href: link,
..Default::default()
});
entry.id = guid;
feed.entries.push(entry);
feed.entries.push(post_preview.into_atom_entry());
}
};
}
Expand Down
51 changes: 49 additions & 2 deletions src/feed/preview.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
use chrono::{DateTime, FixedOffset};
use serde::Serialize;

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Default)]
pub struct FeedPreview {
pub title: String,
pub link: String,
pub description: Option<String>,
pub posts: Vec<PostPreview>,
}

#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
#[derive(Debug, Serialize, PartialEq, Eq, Hash, Default)]
pub struct PostPreview {
pub title: String,
pub author: Option<String>,
pub link: String,
pub body: Option<String>,
pub date: Option<DateTime<FixedOffset>>,
}

impl PostPreview {
pub fn into_rss_item(self) -> rss::Item {
let guid = rss::Guid {
value: self.link.clone(),
permalink: true,
};

rss::Item {
title: Some(self.title),
link: Some(self.link),
description: self.body,
pub_date: self.date.map(|d| d.to_rfc3339()),
author: self.author,
guid: Some(guid),
..Default::default()
}
}

pub fn into_atom_entry(self) -> atom_syndication::Entry {
atom_syndication::Entry {
title: atom_syndication::Text::plain(self.title),
id: self.link.clone(),
links: vec![atom_syndication::Link {
href: self.link,
..Default::default()
}],
authors: self
.author
.into_iter()
.map(|a| atom_syndication::Person {
name: a,
..Default::default()
})
.collect(),
updated: self
.date
.unwrap_or_else(|| chrono::Utc::now().fixed_offset()),
published: self.date,
content: self.body.map(|b| atom_syndication::Content {
value: Some(b),
..Default::default()
}),
..Default::default()
}
}
}
44 changes: 30 additions & 14 deletions src/filter/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::client::{Client, ClientConfig};
use crate::feed::Feed;
use crate::feed::{Feed, PostPreview};
use crate::filter_pipeline::{FilterPipeline, FilterPipelineConfig};
use crate::source::{SimpleSourceConfig, Source};
use crate::util::{ConfigError, Error, Result, SingleOrVec};
Expand Down Expand Up @@ -140,26 +140,42 @@ impl FeedFilter for Merge {
}

for (source, error) in errors {
let source_url = source.full_url(ctx).map(|u| u.to_string());
let title = match source_url.as_ref() {
Some(url) => format!("Error fetching source: {}", url),
None => "Error: Failed fetching source".to_owned(),
};
let source_desc = source_url
.clone()
.unwrap_or_else(|| format!("{:?}", source));

let body = format!(
"<p><b>Source:</b><br>{source_desc}</p><p><b>Error:</b><br>{error}</p>"
);
feed.add_item(title, body, source_url.unwrap_or_default());
let post = post_from_error(source, error, ctx);
feed.add_post(post);
}

feed.reorder();
Ok(feed)
}
}

fn post_from_error(
source: Source,
error: Error,
ctx: &FilterContext,
) -> PostPreview {
let source_url = source.full_url(ctx).map(|u| u.to_string());
let title = match source_url.as_ref() {
Some(url) => format!("Error fetching source: {}", url),
None => "Error: Failed fetching source".to_owned(),
};
let source_desc = source_url
.clone()
.unwrap_or_else(|| format!("{:?}", source));

let body = format!(
"<p><b>Source:</b><br>{source_desc}</p><p><b>Error:</b><br>{error}</p>"
);

PostPreview {
title,
link: source_url.unwrap_or_default(),
author: Some("rss-funnel".to_owned()),
body: Some(body),
date: Some(chrono::Utc::now().fixed_offset()),
}
}

// return Err(e) only if all results are Err.
// otherwise return a Vec<T> with failed results
#[allow(clippy::type_complexity)]
Expand Down

0 comments on commit 41c5594

Please sign in to comment.