1 use actix_web::{error::ErrorBadRequest, web, Error, HttpRequest, HttpResponse, Result};
3 use chrono::{DateTime, NaiveDateTime, Utc};
4 use lemmy_api_common::context::LemmyContext;
7 source::{community::Community, local_user::LocalUser, person::Person},
8 traits::{ApubActor, Crud},
16 structs::{PostView, SiteView},
18 use lemmy_db_views_actor::{
19 comment_reply_view::CommentReplyQuery,
20 person_mention_view::PersonMentionQuery,
21 structs::{CommentReplyView, PersonMentionView},
23 use lemmy_utils::{claims::Claims, error::LemmyError, utils::markdown::markdown_to_html};
24 use once_cell::sync::Lazy;
26 extension::dublincore::DublinCoreExtensionBuilder,
32 use serde::Deserialize;
33 use std::{collections::BTreeMap, str::FromStr};
34 use strum::ParseError;
36 const RSS_FETCH_LIMIT: i64 = 20;
38 #[derive(Deserialize)]
50 pub fn config(cfg: &mut web::ServiceConfig) {
52 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
53 .route("/feeds/all.xml", web::get().to(get_all_feed))
54 .route("/feeds/local.xml", web::get().to(get_local_feed));
57 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
58 let mut h = BTreeMap::new();
61 rss::extension::dublincore::NAMESPACE.to_string(),
66 #[tracing::instrument(skip_all)]
67 async fn get_all_feed(
68 info: web::Query<Params>,
69 context: web::Data<LemmyContext>,
70 ) -> Result<HttpResponse, Error> {
71 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
72 Ok(get_feed_data(&context, ListingType::All, sort_type).await?)
75 #[tracing::instrument(skip_all)]
76 async fn get_local_feed(
77 info: web::Query<Params>,
78 context: web::Data<LemmyContext>,
79 ) -> Result<HttpResponse, Error> {
80 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
81 Ok(get_feed_data(&context, ListingType::Local, sort_type).await?)
84 #[tracing::instrument(skip_all)]
85 async fn get_feed_data(
86 context: &LemmyContext,
87 listing_type: ListingType,
89 ) -> Result<HttpResponse, LemmyError> {
90 let site_view = SiteView::read_local(context.pool()).await?;
92 let posts = PostQuery::builder()
94 .listing_type(Some(listing_type))
95 .sort(Some(sort_type))
96 .limit(Some(RSS_FETCH_LIMIT))
101 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
103 let mut channel_builder = ChannelBuilder::default();
105 .namespaces(RSS_NAMESPACE.clone())
106 .title(&format!("{} - {}", site_view.site.name, listing_type))
107 .link(context.settings().get_protocol_and_hostname())
110 if let Some(site_desc) = site_view.site.description {
111 channel_builder.description(&site_desc);
114 let rss = channel_builder.build().to_string();
117 .content_type("application/rss+xml")
122 #[tracing::instrument(skip_all)]
125 info: web::Query<Params>,
126 context: web::Data<LemmyContext>,
127 ) -> Result<HttpResponse, Error> {
128 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
130 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
131 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
133 let request_type = match req_type.as_str() {
134 "u" => RequestType::User,
135 "c" => RequestType::Community,
136 "front" => RequestType::Front,
137 "inbox" => RequestType::Inbox,
138 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
141 let jwt_secret = context.secret().jwt_secret.clone();
142 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
144 let builder = match request_type {
145 RequestType::User => {
146 get_feed_user(context.pool(), &sort_type, ¶m, &protocol_and_hostname).await
148 RequestType::Community => {
149 get_feed_community(context.pool(), &sort_type, ¶m, &protocol_and_hostname).await
151 RequestType::Front => {
157 &protocol_and_hostname,
161 RequestType::Inbox => {
162 get_feed_inbox(context.pool(), &jwt_secret, ¶m, &protocol_and_hostname).await
165 .map_err(ErrorBadRequest)?;
167 let rss = builder.build().to_string();
171 .content_type("application/rss+xml")
176 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
177 let sort_query = info
180 .unwrap_or_else(|| SortType::Hot.to_string());
181 SortType::from_str(&sort_query)
184 #[tracing::instrument(skip_all)]
185 async fn get_feed_user(
187 sort_type: &SortType,
189 protocol_and_hostname: &str,
190 ) -> Result<ChannelBuilder, LemmyError> {
191 let site_view = SiteView::read_local(pool).await?;
192 let person = Person::read_from_name(pool, user_name, false).await?;
194 let posts = PostQuery::builder()
196 .listing_type(Some(ListingType::All))
197 .sort(Some(*sort_type))
198 .creator_id(Some(person.id))
199 .limit(Some(RSS_FETCH_LIMIT))
204 let items = create_post_items(posts, protocol_and_hostname)?;
206 let mut channel_builder = ChannelBuilder::default();
208 .namespaces(RSS_NAMESPACE.clone())
209 .title(&format!("{} - {}", site_view.site.name, person.name))
210 .link(person.actor_id.to_string())
216 #[tracing::instrument(skip_all)]
217 async fn get_feed_community(
219 sort_type: &SortType,
220 community_name: &str,
221 protocol_and_hostname: &str,
222 ) -> Result<ChannelBuilder, LemmyError> {
223 let site_view = SiteView::read_local(pool).await?;
224 let community = Community::read_from_name(pool, community_name, false).await?;
226 let posts = PostQuery::builder()
228 .sort(Some(*sort_type))
229 .community_id(Some(community.id))
230 .limit(Some(RSS_FETCH_LIMIT))
235 let items = create_post_items(posts, protocol_and_hostname)?;
237 let mut channel_builder = ChannelBuilder::default();
239 .namespaces(RSS_NAMESPACE.clone())
240 .title(&format!("{} - {}", site_view.site.name, community.name))
241 .link(community.actor_id.to_string())
244 if let Some(community_desc) = community.description {
245 channel_builder.description(&community_desc);
251 #[tracing::instrument(skip_all)]
252 async fn get_feed_front(
255 sort_type: &SortType,
257 protocol_and_hostname: &str,
258 ) -> Result<ChannelBuilder, LemmyError> {
259 let site_view = SiteView::read_local(pool).await?;
260 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
261 let local_user = LocalUser::read(pool, local_user_id).await?;
263 let posts = PostQuery::builder()
265 .listing_type(Some(ListingType::Subscribed))
266 .local_user(Some(&local_user))
267 .sort(Some(*sort_type))
268 .limit(Some(RSS_FETCH_LIMIT))
273 let items = create_post_items(posts, protocol_and_hostname)?;
275 let mut channel_builder = ChannelBuilder::default();
277 .namespaces(RSS_NAMESPACE.clone())
278 .title(&format!("{} - Subscribed", site_view.site.name))
279 .link(protocol_and_hostname)
282 if let Some(site_desc) = site_view.site.description {
283 channel_builder.description(&site_desc);
289 #[tracing::instrument(skip_all)]
290 async fn get_feed_inbox(
294 protocol_and_hostname: &str,
295 ) -> Result<ChannelBuilder, LemmyError> {
296 let site_view = SiteView::read_local(pool).await?;
297 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
298 let local_user = LocalUser::read(pool, local_user_id).await?;
299 let person_id = local_user.person_id;
300 let show_bot_accounts = local_user.show_bot_accounts;
302 let sort = CommentSortType::New;
304 let replies = CommentReplyQuery::builder()
306 .recipient_id(Some(person_id))
307 .my_person_id(Some(person_id))
308 .show_bot_accounts(Some(show_bot_accounts))
310 .limit(Some(RSS_FETCH_LIMIT))
315 let mentions = PersonMentionQuery::builder()
317 .recipient_id(Some(person_id))
318 .my_person_id(Some(person_id))
319 .show_bot_accounts(Some(show_bot_accounts))
321 .limit(Some(RSS_FETCH_LIMIT))
326 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
328 let mut channel_builder = ChannelBuilder::default();
330 .namespaces(RSS_NAMESPACE.clone())
331 .title(&format!("{} - Inbox", site_view.site.name))
332 .link(format!("{protocol_and_hostname}/inbox",))
335 if let Some(site_desc) = site_view.site.description {
336 channel_builder.description(&site_desc);
342 #[tracing::instrument(skip_all)]
343 fn create_reply_and_mention_items(
344 replies: Vec<CommentReplyView>,
345 mentions: Vec<PersonMentionView>,
346 protocol_and_hostname: &str,
347 ) -> Result<Vec<Item>, LemmyError> {
348 let mut reply_items: Vec<Item> = replies
351 let reply_url = format!(
352 "{}/post/{}/comment/{}",
353 protocol_and_hostname, r.post.id, r.comment.id
357 &r.comment.published,
360 protocol_and_hostname,
363 .collect::<Result<Vec<Item>, LemmyError>>()?;
365 let mut mention_items: Vec<Item> = mentions
368 let mention_url = format!(
369 "{}/post/{}/comment/{}",
370 protocol_and_hostname, m.post.id, m.comment.id
374 &m.comment.published,
377 protocol_and_hostname,
380 .collect::<Result<Vec<Item>, LemmyError>>()?;
382 reply_items.append(&mut mention_items);
386 #[tracing::instrument(skip_all)]
389 published: &NaiveDateTime,
392 protocol_and_hostname: &str,
393 ) -> Result<Item, LemmyError> {
394 let mut i = ItemBuilder::default();
395 i.title(format!("Reply from {creator_name}"));
396 let author_url = format!("{protocol_and_hostname}/u/{creator_name}");
398 "/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
400 let dt = DateTime::<Utc>::from_utc(*published, Utc);
401 i.pub_date(dt.to_rfc2822());
402 i.comments(url.to_owned());
403 let guid = GuidBuilder::default().permalink(true).value(url).build();
405 i.link(url.to_owned());
407 let html = markdown_to_html(content);
412 #[tracing::instrument(skip_all)]
413 fn create_post_items(
414 posts: Vec<PostView>,
415 protocol_and_hostname: &str,
416 ) -> Result<Vec<Item>, LemmyError> {
417 let mut items: Vec<Item> = Vec::new();
420 let mut i = ItemBuilder::default();
421 let mut dc_extension = DublinCoreExtensionBuilder::default();
423 i.title(p.post.name);
425 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
427 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
428 i.pub_date(dt.to_rfc2822());
430 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
431 i.link(post_url.clone());
432 i.comments(post_url.clone());
433 let guid = GuidBuilder::default()
439 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
442 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
451 // If its a url post, add it to the description
452 if let Some(url) = p.post.url {
453 let link_html = format!("<br><a href=\"{url}\">{url}</a>");
454 description.push_str(&link_html);
457 if let Some(body) = p.post.body {
458 let html = markdown_to_html(&body);
459 description.push_str(&html);
462 i.description(description);
464 i.dublin_core_ext(dc_extension.build());
465 items.push(i.build());