From 8a6a86c1bb53a27dcde9f3675946993a59ba14c5 Mon Sep 17 00:00:00 2001 From: Lcchy Date: Tue, 13 Feb 2024 09:46:46 +0000 Subject: [PATCH] Add support for RSS media enclosures in feeds (#4442) * Add support for RSS media enclosures in feeds * Use post.url_content_type --- crates/routes/src/feeds.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 9695401df..490fce4ae 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -29,6 +29,7 @@ use once_cell::sync::Lazy; use rss::{ extension::{dublincore::DublinCoreExtension, ExtensionBuilder, ExtensionMap}, Channel, + EnclosureBuilder, Guid, Item, }; @@ -495,7 +496,6 @@ fn create_post_items( let mut items: Vec = Vec::new(); for p in posts { - // TODO add images let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id); let community_url = format!( "{}/c/{}", @@ -520,12 +520,21 @@ fn create_post_items( p.counts.comments); // If its a url post, add it to the description - let link = Some(if let Some(url) = p.post.url { + // and see if we can parse it as a media enclosure. + let enclosure_opt = p.post.url.map(|url| { let link_html = format!("
{url}"); description.push_str(&link_html); - url.to_string() - } else { - post_url.clone() + + let mime_type = p + .post + .url_content_type + .unwrap_or_else(|| "application/octet-stream".to_string()); + let mut enclosure_bld = EnclosureBuilder::default(); + + enclosure_bld.url(url.as_str().to_string()); + enclosure_bld.mime_type(mime_type); + enclosure_bld.length("0".to_string()); + enclosure_bld.build() }); if let Some(body) = p.post.body { @@ -558,8 +567,9 @@ fn create_post_items( guid, description: Some(sanitize_xml(description)), dublin_core_ext, - link, + link: Some(post_url.clone()), extensions, + enclosure: enclosure_opt, ..Default::default() };