From a2d80d8f2e05db34b645a9f1d67a8e4fd745fd9a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 20 Nov 2020 15:11:47 +0100 Subject: [PATCH] Generate valid RSS feed (fixes #1274) --- src/lib.rs | 3 ++- src/routes/feeds.rs | 52 ++++++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7e5754d4..e6ea900e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![recursion_limit = "512"] - +#[macro_use] +extern crate lazy_static; pub mod code_migrations; pub mod routes; diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index 303a824b..a9454be4 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -16,9 +16,15 @@ use lemmy_db::{ use lemmy_structs::blocking; use lemmy_utils::{settings::Settings, utils::markdown_to_html, LemmyError}; use lemmy_websocket::LemmyContext; -use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder}; +use rss::{ + extension::dublincore::DublinCoreExtensionBuilder, + ChannelBuilder, + GuidBuilder, + Item, + ItemBuilder, +}; use serde::Deserialize; -use std::str::FromStr; +use std::{collections::HashMap, str::FromStr}; use strum::ParseError; #[derive(Deserialize)] @@ -39,6 +45,17 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route("/feeds/all.xml", web::get().to(get_all_feed)); } +lazy_static! { + static ref RSS_NAMESPACE: HashMap = { + let mut h = HashMap::new(); + h.insert( + "dc".to_string(), + rss::extension::dublincore::NAMESPACE.to_string(), + ); + h + }; +} + async fn get_all_feed( info: web::Query, context: web::Data, @@ -70,6 +87,7 @@ fn get_feed_all_data(conn: &PgConnection, sort_type: &SortType) -> Result Result) -> Result, LemmyError> { for p in posts { let mut i = ItemBuilder::default(); + let mut dc_extension = DublinCoreExtensionBuilder::default(); i.title(p.name); - let author_url = format!( - "{}/u/{}", - Settings::get().get_protocol_and_hostname(), - p.creator_name - ); - i.author(format!( - "/u/{} (link)", - p.creator_name, author_url - )); + dc_extension.creators(vec![p.creator_actor_id.to_owned()]); let dt = DateTime::::from_utc(p.published, Utc); i.pub_date(dt.to_rfc2822()); @@ -345,16 +360,8 @@ fn create_post_items(posts: Vec) -> Result, LemmyError> { p.community_name ); - let category = CategoryBuilder::default() - .name(format!( - "/c/{} (link)", - p.community_name, community_url - )) - .domain(Settings::get().hostname.to_owned()) - .build() - .map_err(|e| anyhow!(e))?; - - i.categories(vec![category]); + // TODO: for category we should just put the name of the category, but then we would have + // to read each community from the db if let Some(url) = p.url { i.link(url); @@ -362,7 +369,7 @@ fn create_post_items(posts: Vec) -> Result, LemmyError> { // TODO add images let mut description = format!("submitted by {} to {}
{} points | {} comments", - author_url, + p.creator_actor_id, p.creator_name, community_url, p.community_name, @@ -377,6 +384,7 @@ fn create_post_items(posts: Vec) -> Result, LemmyError> { i.description(description); + i.dublin_core_ext(dc_extension.build().map_err(|e| anyhow!(e))?); items.push(i.build().map_err(|e| anyhow!(e))?); } -- 2.44.1