Fixing rust-analyzer suggestions on rss feeds. (#4210)

* Fixing rust-analyzer suggestions on rss feeds.

* Get rid of other pointless builders.

* More cleanup.
This commit is contained in:
Dessalines 2023-11-29 05:00:06 -05:00 committed by GitHub
parent 6fa3b59d25
commit a986db1a00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,13 +25,7 @@ use lemmy_utils::{
utils::markdown::{markdown_to_html, sanitize_html}, utils::markdown::{markdown_to_html, sanitize_html},
}; };
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rss::{ use rss::{extension::dublincore::DublinCoreExtension, Channel, Guid, Item};
extension::dublincore::DublinCoreExtensionBuilder,
ChannelBuilder,
GuidBuilder,
Item,
ItemBuilder,
};
use serde::Deserialize; use serde::Deserialize;
use std::{collections::BTreeMap, str::FromStr}; use std::{collections::BTreeMap, str::FromStr};
@ -146,18 +140,19 @@ async fn get_feed_data(
let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?; let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
let mut channel_builder = ChannelBuilder::default(); let mut channel = Channel {
channel_builder namespaces: RSS_NAMESPACE.clone(),
.namespaces(RSS_NAMESPACE.clone()) title: format!("{} - {}", site_view.site.name, listing_type),
.title(&format!("{} - {}", site_view.site.name, listing_type)) link: context.settings().get_protocol_and_hostname(),
.link(context.settings().get_protocol_and_hostname()) items,
.items(items); ..Default::default()
};
if let Some(site_desc) = site_view.site.description { if let Some(site_desc) = site_view.site.description {
channel_builder.description(&site_desc); channel.set_description(&site_desc);
} }
let rss = channel_builder.build().to_string(); let rss = channel.to_string();
Ok( Ok(
HttpResponse::Ok() HttpResponse::Ok()
.content_type("application/rss+xml") .content_type("application/rss+xml")
@ -217,7 +212,7 @@ async fn get_feed(
} }
.map_err(ErrorBadRequest)?; .map_err(ErrorBadRequest)?;
let rss = builder.build().to_string(); let rss = builder.to_string();
Ok( Ok(
HttpResponse::Ok() HttpResponse::Ok()
@ -233,7 +228,7 @@ async fn get_feed_user(
limit: &i64, limit: &i64,
page: &i64, page: &i64,
user_name: &str, user_name: &str,
) -> Result<ChannelBuilder, LemmyError> { ) -> Result<Channel, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let person = Person::read_from_name(&mut context.pool(), user_name, false).await?; let person = Person::read_from_name(&mut context.pool(), user_name, false).await?;
@ -252,14 +247,15 @@ async fn get_feed_user(
let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?; let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
let mut channel_builder = ChannelBuilder::default(); let channel = Channel {
channel_builder namespaces: RSS_NAMESPACE.clone(),
.namespaces(RSS_NAMESPACE.clone()) title: format!("{} - {}", site_view.site.name, person.name),
.title(&format!("{} - {}", site_view.site.name, person.name)) link: person.actor_id.to_string(),
.link(person.actor_id.to_string()) items,
.items(items); ..Default::default()
};
Ok(channel_builder) Ok(channel)
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
@ -269,7 +265,7 @@ async fn get_feed_community(
limit: &i64, limit: &i64,
page: &i64, page: &i64,
community_name: &str, community_name: &str,
) -> Result<ChannelBuilder, LemmyError> { ) -> Result<Channel, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let community = Community::read_from_name(&mut context.pool(), community_name, false).await?; let community = Community::read_from_name(&mut context.pool(), community_name, false).await?;
@ -287,18 +283,19 @@ async fn get_feed_community(
let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?; let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
let mut channel_builder = ChannelBuilder::default(); let mut channel = Channel {
channel_builder namespaces: RSS_NAMESPACE.clone(),
.namespaces(RSS_NAMESPACE.clone()) title: format!("{} - {}", site_view.site.name, community.name),
.title(&format!("{} - {}", site_view.site.name, community.name)) link: community.actor_id.to_string(),
.link(community.actor_id.to_string()) items,
.items(items); ..Default::default()
};
if let Some(community_desc) = community.description { if let Some(community_desc) = community.description {
channel_builder.description(markdown_to_html(&community_desc)); channel.set_description(markdown_to_html(&community_desc));
} }
Ok(channel_builder) Ok(channel)
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
@ -308,7 +305,7 @@ async fn get_feed_front(
limit: &i64, limit: &i64,
page: &i64, page: &i64,
jwt: &str, jwt: &str,
) -> Result<ChannelBuilder, LemmyError> { ) -> Result<Channel, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_user = local_user_view_from_jwt(jwt, context).await?; let local_user = local_user_view_from_jwt(jwt, context).await?;
@ -328,22 +325,23 @@ async fn get_feed_front(
let protocol_and_hostname = context.settings().get_protocol_and_hostname(); let protocol_and_hostname = context.settings().get_protocol_and_hostname();
let items = create_post_items(posts, &protocol_and_hostname)?; let items = create_post_items(posts, &protocol_and_hostname)?;
let mut channel_builder = ChannelBuilder::default(); let mut channel = Channel {
channel_builder namespaces: RSS_NAMESPACE.clone(),
.namespaces(RSS_NAMESPACE.clone()) title: format!("{} - Subscribed", site_view.site.name),
.title(&format!("{} - Subscribed", site_view.site.name)) link: protocol_and_hostname,
.link(protocol_and_hostname) items,
.items(items); ..Default::default()
};
if let Some(site_desc) = site_view.site.description { if let Some(site_desc) = site_view.site.description {
channel_builder.description(markdown_to_html(&site_desc)); channel.set_description(markdown_to_html(&site_desc));
} }
Ok(channel_builder) Ok(channel)
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
async fn get_feed_inbox(context: &LemmyContext, jwt: &str) -> Result<ChannelBuilder, LemmyError> { async fn get_feed_inbox(context: &LemmyContext, jwt: &str) -> Result<Channel, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let local_user = local_user_view_from_jwt(jwt, context).await?; let local_user = local_user_view_from_jwt(jwt, context).await?;
let person_id = local_user.local_user.person_id; let person_id = local_user.local_user.person_id;
@ -378,18 +376,19 @@ async fn get_feed_inbox(context: &LemmyContext, jwt: &str) -> Result<ChannelBuil
let protocol_and_hostname = context.settings().get_protocol_and_hostname(); let protocol_and_hostname = context.settings().get_protocol_and_hostname();
let items = create_reply_and_mention_items(replies, mentions, &protocol_and_hostname)?; let items = create_reply_and_mention_items(replies, mentions, &protocol_and_hostname)?;
let mut channel_builder = ChannelBuilder::default(); let mut channel = Channel {
channel_builder namespaces: RSS_NAMESPACE.clone(),
.namespaces(RSS_NAMESPACE.clone()) title: format!("{} - Inbox", site_view.site.name),
.title(&format!("{} - Inbox", site_view.site.name)) link: format!("{protocol_and_hostname}/inbox"),
.link(format!("{protocol_and_hostname}/inbox",)) items,
.items(items); ..Default::default()
};
if let Some(site_desc) = site_view.site.description { if let Some(site_desc) = site_view.site.description {
channel_builder.description(&site_desc); channel.set_description(&site_desc);
} }
Ok(channel_builder) Ok(channel)
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
@ -438,22 +437,26 @@ fn build_item(
content: &str, content: &str,
protocol_and_hostname: &str, protocol_and_hostname: &str,
) -> Result<Item, LemmyError> { ) -> Result<Item, LemmyError> {
let mut i = ItemBuilder::default();
i.title(format!("Reply from {creator_name}"));
let author_url = format!("{protocol_and_hostname}/u/{creator_name}");
i.author(format!(
"/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
));
let dt = published;
i.pub_date(dt.to_rfc2822());
i.comments(url.to_owned());
let guid = GuidBuilder::default().permalink(true).value(url).build();
i.guid(guid);
i.link(url.to_owned());
// TODO add images // TODO add images
let html = markdown_to_html(content); let author_url = format!("{protocol_and_hostname}/u/{creator_name}");
i.description(html); let guid = Some(Guid {
Ok(i.build()) permalink: true,
value: url.to_owned(),
});
let description = Some(markdown_to_html(content));
Ok(Item {
title: Some(format!("Reply from {creator_name}")),
author: Some(format!(
"/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
)),
pub_date: Some(published.to_rfc2822()),
comments: Some(url.to_owned()),
link: Some(url.to_owned()),
guid,
description,
..Default::default()
})
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
@ -464,31 +467,21 @@ fn create_post_items(
let mut items: Vec<Item> = Vec::new(); let mut items: Vec<Item> = Vec::new();
for p in posts { for p in posts {
let mut i = ItemBuilder::default(); // TODO add images
let mut dc_extension = DublinCoreExtensionBuilder::default();
i.title(sanitize_html(&p.post.name));
dc_extension.creators(vec![p.creator.actor_id.to_string()]);
let dt = p.post.published;
i.pub_date(dt.to_rfc2822());
let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id); let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
i.comments(post_url.clone());
let guid = GuidBuilder::default()
.permalink(true)
.value(&post_url)
.build();
i.guid(guid);
let community_url = format!( let community_url = format!(
"{}/c/{}", "{}/c/{}",
protocol_and_hostname, protocol_and_hostname,
sanitize_html(&p.community.name) sanitize_html(&p.community.name)
); );
let dublin_core_ext = Some(DublinCoreExtension {
// TODO add images creators: vec![p.creator.actor_id.to_string()],
..DublinCoreExtension::default()
});
let guid = Some(Guid {
permalink: true,
value: post_url.clone(),
});
let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>", let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
p.creator.actor_id, p.creator.actor_id,
sanitize_html(&p.creator.name), sanitize_html(&p.creator.name),
@ -499,23 +492,31 @@ fn create_post_items(
p.counts.comments); p.counts.comments);
// If its a url post, add it to the description // If its a url post, add it to the description
if let Some(url) = p.post.url { let link = Some(if let Some(url) = p.post.url {
let link_html = format!("<br><a href=\"{url}\">{url}</a>"); let link_html = format!("<br><a href=\"{url}\">{url}</a>");
description.push_str(&link_html); description.push_str(&link_html);
i.link(url.to_string()); url.to_string()
} else { } else {
i.link(post_url.clone()); post_url.clone()
} });
if let Some(body) = p.post.body { if let Some(body) = p.post.body {
let html = markdown_to_html(&body); let html = markdown_to_html(&body);
description.push_str(&html); description.push_str(&html);
} }
i.description(description); let i = Item {
title: Some(sanitize_html(&p.post.name)),
pub_date: Some(p.post.published.to_rfc2822()),
comments: Some(post_url.clone()),
guid,
description: Some(description),
dublin_core_ext,
link,
..Default::default()
};
i.dublin_core_ext(dc_extension.build()); items.push(i);
items.push(i.build());
} }
Ok(items) Ok(items)