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::{LocalUserView, PostView, SiteView},
18 use lemmy_db_views_actor::{
19 comment_reply_view::CommentReplyQuery,
20 person_mention_view::PersonMentionQuery,
21 structs::{CommentReplyView, PersonMentionView},
24 cache_header::cache_1hour,
27 utils::markdown::markdown_to_html,
29 use once_cell::sync::Lazy;
31 extension::dublincore::DublinCoreExtensionBuilder,
37 use serde::Deserialize;
38 use std::{collections::BTreeMap, str::FromStr};
40 const RSS_FETCH_LIMIT: i64 = 20;
42 #[derive(Deserialize)]
50 fn sort_type(&self) -> Result<SortType, Error> {
54 .unwrap_or_else(|| SortType::Hot.to_string());
55 SortType::from_str(&sort_query).map_err(ErrorBadRequest)
57 fn get_limit(&self) -> i64 {
58 self.limit.unwrap_or(RSS_FETCH_LIMIT)
60 fn get_page(&self) -> i64 {
61 self.page.unwrap_or(1)
72 pub fn config(cfg: &mut web::ServiceConfig) {
75 .route("/{type}/{name}.xml", web::get().to(get_feed))
76 .route("/all.xml", web::get().to(get_all_feed).wrap(cache_1hour()))
79 web::get().to(get_local_feed).wrap(cache_1hour()),
84 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
85 let mut h = BTreeMap::new();
88 rss::extension::dublincore::NAMESPACE.to_string(),
93 #[tracing::instrument(skip_all)]
94 async fn get_all_feed(
95 info: web::Query<Params>,
96 context: web::Data<LemmyContext>,
97 ) -> Result<HttpResponse, Error> {
110 #[tracing::instrument(skip_all)]
111 async fn get_local_feed(
112 info: web::Query<Params>,
113 context: web::Data<LemmyContext>,
114 ) -> Result<HttpResponse, Error> {
127 #[tracing::instrument(skip_all)]
128 async fn get_feed_data(
129 context: &LemmyContext,
130 listing_type: ListingType,
134 ) -> Result<HttpResponse, LemmyError> {
135 let site_view = SiteView::read_local(&mut context.pool()).await?;
137 let posts = PostQuery {
138 listing_type: (Some(listing_type)),
139 sort: (Some(sort_type)),
140 limit: (Some(limit)),
144 .list(&mut context.pool())
147 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
149 let mut channel_builder = ChannelBuilder::default();
151 .namespaces(RSS_NAMESPACE.clone())
152 .title(&format!("{} - {}", site_view.site.name, listing_type))
153 .link(context.settings().get_protocol_and_hostname())
156 if let Some(site_desc) = site_view.site.description {
157 channel_builder.description(&site_desc);
160 let rss = channel_builder.build().to_string();
163 .content_type("application/rss+xml")
168 #[tracing::instrument(skip_all)]
171 info: web::Query<Params>,
172 context: web::Data<LemmyContext>,
173 ) -> Result<HttpResponse, Error> {
174 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
175 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
177 let request_type = match req_type.as_str() {
178 "u" => RequestType::User,
179 "c" => RequestType::Community,
180 "front" => RequestType::Front,
181 "inbox" => RequestType::Inbox,
182 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
185 let jwt_secret = context.secret().jwt_secret.clone();
186 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
188 let builder = match request_type {
189 RequestType::User => {
196 &protocol_and_hostname,
200 RequestType::Community => {
207 &protocol_and_hostname,
211 RequestType::Front => {
219 &protocol_and_hostname,
223 RequestType::Inbox => {
228 &protocol_and_hostname,
233 .map_err(ErrorBadRequest)?;
235 let rss = builder.build().to_string();
239 .content_type("application/rss+xml")
244 #[tracing::instrument(skip_all)]
245 async fn get_feed_user(
246 pool: &mut DbPool<'_>,
247 sort_type: &SortType,
251 protocol_and_hostname: &str,
252 ) -> Result<ChannelBuilder, LemmyError> {
253 let site_view = SiteView::read_local(pool).await?;
254 let person = Person::read_from_name(pool, user_name, false).await?;
256 let posts = PostQuery {
257 listing_type: (Some(ListingType::All)),
258 sort: (Some(*sort_type)),
259 creator_id: (Some(person.id)),
260 limit: (Some(*limit)),
267 let items = create_post_items(posts, protocol_and_hostname)?;
269 let mut channel_builder = ChannelBuilder::default();
271 .namespaces(RSS_NAMESPACE.clone())
272 .title(&format!("{} - {}", site_view.site.name, person.name))
273 .link(person.actor_id.to_string())
279 #[tracing::instrument(skip_all)]
280 async fn get_feed_community(
281 pool: &mut DbPool<'_>,
282 sort_type: &SortType,
285 community_name: &str,
286 protocol_and_hostname: &str,
287 ) -> Result<ChannelBuilder, LemmyError> {
288 let site_view = SiteView::read_local(pool).await?;
289 let community = Community::read_from_name(pool, community_name, false).await?;
291 let posts = PostQuery {
292 sort: (Some(*sort_type)),
293 community_id: (Some(community.id)),
294 limit: (Some(*limit)),
301 let items = create_post_items(posts, protocol_and_hostname)?;
303 let mut channel_builder = ChannelBuilder::default();
305 .namespaces(RSS_NAMESPACE.clone())
306 .title(&format!("{} - {}", site_view.site.name, community.name))
307 .link(community.actor_id.to_string())
310 if let Some(community_desc) = community.description {
311 channel_builder.description(&community_desc);
317 #[tracing::instrument(skip_all)]
318 async fn get_feed_front(
319 pool: &mut DbPool<'_>,
321 sort_type: &SortType,
325 protocol_and_hostname: &str,
326 ) -> Result<ChannelBuilder, LemmyError> {
327 let site_view = SiteView::read_local(pool).await?;
328 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
329 let local_user = LocalUserView::read(pool, local_user_id).await?;
331 let posts = PostQuery {
332 listing_type: (Some(ListingType::Subscribed)),
333 local_user: (Some(&local_user)),
334 sort: (Some(*sort_type)),
335 limit: (Some(*limit)),
342 let items = create_post_items(posts, protocol_and_hostname)?;
344 let mut channel_builder = ChannelBuilder::default();
346 .namespaces(RSS_NAMESPACE.clone())
347 .title(&format!("{} - Subscribed", site_view.site.name))
348 .link(protocol_and_hostname)
351 if let Some(site_desc) = site_view.site.description {
352 channel_builder.description(&site_desc);
358 #[tracing::instrument(skip_all)]
359 async fn get_feed_inbox(
360 pool: &mut DbPool<'_>,
363 protocol_and_hostname: &str,
364 ) -> Result<ChannelBuilder, LemmyError> {
365 let site_view = SiteView::read_local(pool).await?;
366 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
367 let local_user = LocalUser::read(pool, local_user_id).await?;
368 let person_id = local_user.person_id;
369 let show_bot_accounts = local_user.show_bot_accounts;
371 let sort = CommentSortType::New;
373 let replies = CommentReplyQuery {
374 recipient_id: (Some(person_id)),
375 my_person_id: (Some(person_id)),
376 show_bot_accounts: (Some(show_bot_accounts)),
378 limit: (Some(RSS_FETCH_LIMIT)),
384 let mentions = PersonMentionQuery {
385 recipient_id: (Some(person_id)),
386 my_person_id: (Some(person_id)),
387 show_bot_accounts: (Some(show_bot_accounts)),
389 limit: (Some(RSS_FETCH_LIMIT)),
395 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
397 let mut channel_builder = ChannelBuilder::default();
399 .namespaces(RSS_NAMESPACE.clone())
400 .title(&format!("{} - Inbox", site_view.site.name))
401 .link(format!("{protocol_and_hostname}/inbox",))
404 if let Some(site_desc) = site_view.site.description {
405 channel_builder.description(&site_desc);
411 #[tracing::instrument(skip_all)]
412 fn create_reply_and_mention_items(
413 replies: Vec<CommentReplyView>,
414 mentions: Vec<PersonMentionView>,
415 protocol_and_hostname: &str,
416 ) -> Result<Vec<Item>, LemmyError> {
417 let mut reply_items: Vec<Item> = replies
420 let reply_url = format!("{}/comment/{}", protocol_and_hostname, r.comment.id);
423 &r.comment.published,
426 protocol_and_hostname,
429 .collect::<Result<Vec<Item>, LemmyError>>()?;
431 let mut mention_items: Vec<Item> = mentions
434 let mention_url = format!("{}/comment/{}", protocol_and_hostname, m.comment.id);
437 &m.comment.published,
440 protocol_and_hostname,
443 .collect::<Result<Vec<Item>, LemmyError>>()?;
445 reply_items.append(&mut mention_items);
449 #[tracing::instrument(skip_all)]
452 published: &NaiveDateTime,
455 protocol_and_hostname: &str,
456 ) -> Result<Item, LemmyError> {
457 let mut i = ItemBuilder::default();
458 i.title(format!("Reply from {creator_name}"));
459 let author_url = format!("{protocol_and_hostname}/u/{creator_name}");
461 "/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
463 let dt = DateTime::<Utc>::from_utc(*published, Utc);
464 i.pub_date(dt.to_rfc2822());
465 i.comments(url.to_owned());
466 let guid = GuidBuilder::default().permalink(true).value(url).build();
468 i.link(url.to_owned());
470 let html = markdown_to_html(content);
475 #[tracing::instrument(skip_all)]
476 fn create_post_items(
477 posts: Vec<PostView>,
478 protocol_and_hostname: &str,
479 ) -> Result<Vec<Item>, LemmyError> {
480 let mut items: Vec<Item> = Vec::new();
483 let mut i = ItemBuilder::default();
484 let mut dc_extension = DublinCoreExtensionBuilder::default();
486 i.title(p.post.name);
488 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
490 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
491 i.pub_date(dt.to_rfc2822());
493 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
494 i.comments(post_url.clone());
495 let guid = GuidBuilder::default()
501 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
504 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
513 // If its a url post, add it to the description
514 if let Some(url) = p.post.url {
515 let link_html = format!("<br><a href=\"{url}\">{url}</a>");
516 description.push_str(&link_html);
517 i.link(url.to_string());
519 i.link(post_url.clone());
522 if let Some(body) = p.post.body {
523 let html = markdown_to_html(&body);
524 description.push_str(&html);
527 i.description(description);
529 i.dublin_core_ext(dc_extension.build());
530 items.push(i.build());