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};
35 const RSS_FETCH_LIMIT: i64 = 20;
37 #[derive(Deserialize)]
45 fn sort_type(&self) -> Result<SortType, Error> {
49 .unwrap_or_else(|| SortType::Hot.to_string());
50 SortType::from_str(&sort_query).map_err(ErrorBadRequest)
52 fn get_limit(&self) -> i64 {
53 self.limit.unwrap_or(RSS_FETCH_LIMIT)
55 fn get_page(&self) -> i64 {
56 self.page.unwrap_or(1)
67 pub fn config(cfg: &mut web::ServiceConfig) {
69 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
70 .route("/feeds/all.xml", web::get().to(get_all_feed))
71 .route("/feeds/local.xml", web::get().to(get_local_feed));
74 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
75 let mut h = BTreeMap::new();
78 rss::extension::dublincore::NAMESPACE.to_string(),
83 #[tracing::instrument(skip_all)]
84 async fn get_all_feed(
85 info: web::Query<Params>,
86 context: web::Data<LemmyContext>,
87 ) -> Result<HttpResponse, Error> {
100 #[tracing::instrument(skip_all)]
101 async fn get_local_feed(
102 info: web::Query<Params>,
103 context: web::Data<LemmyContext>,
104 ) -> Result<HttpResponse, Error> {
117 #[tracing::instrument(skip_all)]
118 async fn get_feed_data(
119 context: &LemmyContext,
120 listing_type: ListingType,
124 ) -> Result<HttpResponse, LemmyError> {
125 let site_view = SiteView::read_local(&mut context.pool()).await?;
127 let posts = PostQuery {
128 listing_type: (Some(listing_type)),
129 sort: (Some(sort_type)),
130 limit: (Some(limit)),
134 .list(&mut context.pool())
137 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
139 let mut channel_builder = ChannelBuilder::default();
141 .namespaces(RSS_NAMESPACE.clone())
142 .title(&format!("{} - {}", site_view.site.name, listing_type))
143 .link(context.settings().get_protocol_and_hostname())
146 if let Some(site_desc) = site_view.site.description {
147 channel_builder.description(&site_desc);
150 let rss = channel_builder.build().to_string();
153 .content_type("application/rss+xml")
158 #[tracing::instrument(skip_all)]
161 info: web::Query<Params>,
162 context: web::Data<LemmyContext>,
163 ) -> Result<HttpResponse, Error> {
164 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
165 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
167 let request_type = match req_type.as_str() {
168 "u" => RequestType::User,
169 "c" => RequestType::Community,
170 "front" => RequestType::Front,
171 "inbox" => RequestType::Inbox,
172 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
175 let jwt_secret = context.secret().jwt_secret.clone();
176 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
178 let builder = match request_type {
179 RequestType::User => {
186 &protocol_and_hostname,
190 RequestType::Community => {
197 &protocol_and_hostname,
201 RequestType::Front => {
209 &protocol_and_hostname,
213 RequestType::Inbox => {
218 &protocol_and_hostname,
223 .map_err(ErrorBadRequest)?;
225 let rss = builder.build().to_string();
229 .content_type("application/rss+xml")
234 #[tracing::instrument(skip_all)]
235 async fn get_feed_user(
236 pool: &mut DbPool<'_>,
237 sort_type: &SortType,
241 protocol_and_hostname: &str,
242 ) -> Result<ChannelBuilder, LemmyError> {
243 let site_view = SiteView::read_local(pool).await?;
244 let person = Person::read_from_name(pool, user_name, false).await?;
246 let posts = PostQuery {
247 listing_type: (Some(ListingType::All)),
248 sort: (Some(*sort_type)),
249 creator_id: (Some(person.id)),
250 limit: (Some(*limit)),
257 let items = create_post_items(posts, protocol_and_hostname)?;
259 let mut channel_builder = ChannelBuilder::default();
261 .namespaces(RSS_NAMESPACE.clone())
262 .title(&format!("{} - {}", site_view.site.name, person.name))
263 .link(person.actor_id.to_string())
269 #[tracing::instrument(skip_all)]
270 async fn get_feed_community(
271 pool: &mut DbPool<'_>,
272 sort_type: &SortType,
275 community_name: &str,
276 protocol_and_hostname: &str,
277 ) -> Result<ChannelBuilder, LemmyError> {
278 let site_view = SiteView::read_local(pool).await?;
279 let community = Community::read_from_name(pool, community_name, false).await?;
281 let posts = PostQuery {
282 sort: (Some(*sort_type)),
283 community_id: (Some(community.id)),
284 limit: (Some(*limit)),
291 let items = create_post_items(posts, protocol_and_hostname)?;
293 let mut channel_builder = ChannelBuilder::default();
295 .namespaces(RSS_NAMESPACE.clone())
296 .title(&format!("{} - {}", site_view.site.name, community.name))
297 .link(community.actor_id.to_string())
300 if let Some(community_desc) = community.description {
301 channel_builder.description(&community_desc);
307 #[tracing::instrument(skip_all)]
308 async fn get_feed_front(
309 pool: &mut DbPool<'_>,
311 sort_type: &SortType,
315 protocol_and_hostname: &str,
316 ) -> Result<ChannelBuilder, LemmyError> {
317 let site_view = SiteView::read_local(pool).await?;
318 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
319 let local_user = LocalUser::read(pool, local_user_id).await?;
321 let posts = PostQuery {
322 listing_type: (Some(ListingType::Subscribed)),
323 local_user: (Some(&local_user)),
324 sort: (Some(*sort_type)),
325 limit: (Some(*limit)),
332 let items = create_post_items(posts, protocol_and_hostname)?;
334 let mut channel_builder = ChannelBuilder::default();
336 .namespaces(RSS_NAMESPACE.clone())
337 .title(&format!("{} - Subscribed", site_view.site.name))
338 .link(protocol_and_hostname)
341 if let Some(site_desc) = site_view.site.description {
342 channel_builder.description(&site_desc);
348 #[tracing::instrument(skip_all)]
349 async fn get_feed_inbox(
350 pool: &mut DbPool<'_>,
353 protocol_and_hostname: &str,
354 ) -> Result<ChannelBuilder, LemmyError> {
355 let site_view = SiteView::read_local(pool).await?;
356 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
357 let local_user = LocalUser::read(pool, local_user_id).await?;
358 let person_id = local_user.person_id;
359 let show_bot_accounts = local_user.show_bot_accounts;
361 let sort = CommentSortType::New;
363 let replies = CommentReplyQuery {
364 recipient_id: (Some(person_id)),
365 my_person_id: (Some(person_id)),
366 show_bot_accounts: (Some(show_bot_accounts)),
368 limit: (Some(RSS_FETCH_LIMIT)),
374 let mentions = PersonMentionQuery {
375 recipient_id: (Some(person_id)),
376 my_person_id: (Some(person_id)),
377 show_bot_accounts: (Some(show_bot_accounts)),
379 limit: (Some(RSS_FETCH_LIMIT)),
385 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
387 let mut channel_builder = ChannelBuilder::default();
389 .namespaces(RSS_NAMESPACE.clone())
390 .title(&format!("{} - Inbox", site_view.site.name))
391 .link(format!("{protocol_and_hostname}/inbox",))
394 if let Some(site_desc) = site_view.site.description {
395 channel_builder.description(&site_desc);
401 #[tracing::instrument(skip_all)]
402 fn create_reply_and_mention_items(
403 replies: Vec<CommentReplyView>,
404 mentions: Vec<PersonMentionView>,
405 protocol_and_hostname: &str,
406 ) -> Result<Vec<Item>, LemmyError> {
407 let mut reply_items: Vec<Item> = replies
410 let reply_url = format!("{}/comment/{}", protocol_and_hostname, r.comment.id);
413 &r.comment.published,
416 protocol_and_hostname,
419 .collect::<Result<Vec<Item>, LemmyError>>()?;
421 let mut mention_items: Vec<Item> = mentions
424 let mention_url = format!("{}/comment/{}", protocol_and_hostname, m.comment.id);
427 &m.comment.published,
430 protocol_and_hostname,
433 .collect::<Result<Vec<Item>, LemmyError>>()?;
435 reply_items.append(&mut mention_items);
439 #[tracing::instrument(skip_all)]
442 published: &NaiveDateTime,
445 protocol_and_hostname: &str,
446 ) -> Result<Item, LemmyError> {
447 let mut i = ItemBuilder::default();
448 i.title(format!("Reply from {creator_name}"));
449 let author_url = format!("{protocol_and_hostname}/u/{creator_name}");
451 "/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
453 let dt = DateTime::<Utc>::from_utc(*published, Utc);
454 i.pub_date(dt.to_rfc2822());
455 i.comments(url.to_owned());
456 let guid = GuidBuilder::default().permalink(true).value(url).build();
458 i.link(url.to_owned());
460 let html = markdown_to_html(content);
465 #[tracing::instrument(skip_all)]
466 fn create_post_items(
467 posts: Vec<PostView>,
468 protocol_and_hostname: &str,
469 ) -> Result<Vec<Item>, LemmyError> {
470 let mut items: Vec<Item> = Vec::new();
473 let mut i = ItemBuilder::default();
474 let mut dc_extension = DublinCoreExtensionBuilder::default();
476 i.title(p.post.name);
478 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
480 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
481 i.pub_date(dt.to_rfc2822());
483 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
484 i.comments(post_url.clone());
485 let guid = GuidBuilder::default()
491 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
494 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
503 // If its a url post, add it to the description
504 if let Some(url) = p.post.url {
505 let link_html = format!("<br><a href=\"{url}\">{url}</a>");
506 description.push_str(&link_html);
507 i.link(url.to_string());
509 i.link(post_url.clone());
512 if let Some(body) = p.post.body {
513 let html = markdown_to_html(&body);
514 description.push_str(&html);
517 i.description(description);
519 i.dublin_core_ext(dc_extension.build());
520 items.push(i.build());