1 use actix_web::{error::ErrorBadRequest, *};
3 use chrono::{DateTime, NaiveDateTime, Utc};
4 use diesel::PgConnection;
5 use lemmy_api_common::utils::blocking;
8 source::{community::Community, local_user::LocalUser, person::Person},
9 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_to_html};
24 use lemmy_websocket::LemmyContext;
25 use once_cell::sync::Lazy;
27 extension::dublincore::DublinCoreExtensionBuilder,
33 use serde::Deserialize;
34 use std::{collections::BTreeMap, str::FromStr};
35 use strum::ParseError;
37 const RSS_FETCH_LIMIT: i64 = 20;
39 #[derive(Deserialize)]
51 pub fn config(cfg: &mut web::ServiceConfig) {
53 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
54 .route("/feeds/all.xml", web::get().to(get_all_feed))
55 .route("/feeds/local.xml", web::get().to(get_local_feed));
58 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
59 let mut h = BTreeMap::new();
62 rss::extension::dublincore::NAMESPACE.to_string(),
67 #[tracing::instrument(skip_all)]
68 async fn get_all_feed(
69 info: web::Query<Params>,
70 context: web::Data<LemmyContext>,
71 ) -> Result<HttpResponse, Error> {
72 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
73 Ok(get_feed_data(&context, ListingType::All, sort_type).await?)
76 #[tracing::instrument(skip_all)]
77 async fn get_local_feed(
78 info: web::Query<Params>,
79 context: web::Data<LemmyContext>,
80 ) -> Result<HttpResponse, Error> {
81 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
82 Ok(get_feed_data(&context, ListingType::Local, sort_type).await?)
85 #[tracing::instrument(skip_all)]
86 async fn get_feed_data(
87 context: &LemmyContext,
88 listing_type: ListingType,
90 ) -> Result<HttpResponse, LemmyError> {
91 let site_view = blocking(context.pool(), SiteView::read_local).await??;
93 let posts = blocking(context.pool(), move |conn| {
96 .listing_type(Some(listing_type))
97 .sort(Some(sort_type))
98 .limit(Some(RSS_FETCH_LIMIT))
104 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
106 let mut channel_builder = ChannelBuilder::default();
108 .namespaces(RSS_NAMESPACE.to_owned())
109 .title(&format!("{} - {}", site_view.site.name, listing_type))
110 .link(context.settings().get_protocol_and_hostname())
113 if let Some(site_desc) = site_view.site.description {
114 channel_builder.description(&site_desc);
117 let rss = channel_builder.build().to_string();
120 .content_type("application/rss+xml")
125 #[tracing::instrument(skip_all)]
128 info: web::Query<Params>,
129 context: web::Data<LemmyContext>,
130 ) -> Result<HttpResponse, Error> {
131 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
133 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
134 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
136 let request_type = match req_type.as_str() {
137 "u" => RequestType::User,
138 "c" => RequestType::Community,
139 "front" => RequestType::Front,
140 "inbox" => RequestType::Inbox,
141 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
144 let jwt_secret = context.secret().jwt_secret.to_owned();
145 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
147 let builder = blocking(context.pool(), move |conn| match request_type {
148 RequestType::User => get_feed_user(conn, &sort_type, ¶m, &protocol_and_hostname),
149 RequestType::Community => get_feed_community(conn, &sort_type, ¶m, &protocol_and_hostname),
150 RequestType::Front => get_feed_front(
155 &protocol_and_hostname,
157 RequestType::Inbox => get_feed_inbox(conn, &jwt_secret, ¶m, &protocol_and_hostname),
160 .map_err(ErrorBadRequest)?;
162 let rss = builder.build().to_string();
166 .content_type("application/rss+xml")
171 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
172 let sort_query = info
175 .unwrap_or_else(|| SortType::Hot.to_string());
176 SortType::from_str(&sort_query)
179 #[tracing::instrument(skip_all)]
182 sort_type: &SortType,
184 protocol_and_hostname: &str,
185 ) -> Result<ChannelBuilder, LemmyError> {
186 let site_view = SiteView::read_local(conn)?;
187 let person = Person::read_from_name(conn, user_name, false)?;
189 let posts = PostQuery::builder()
191 .listing_type(Some(ListingType::All))
192 .sort(Some(*sort_type))
193 .creator_id(Some(person.id))
194 .limit(Some(RSS_FETCH_LIMIT))
198 let items = create_post_items(posts, protocol_and_hostname)?;
200 let mut channel_builder = ChannelBuilder::default();
202 .namespaces(RSS_NAMESPACE.to_owned())
203 .title(&format!("{} - {}", site_view.site.name, person.name))
204 .link(person.actor_id.to_string())
210 #[tracing::instrument(skip_all)]
211 fn get_feed_community(
213 sort_type: &SortType,
214 community_name: &str,
215 protocol_and_hostname: &str,
216 ) -> Result<ChannelBuilder, LemmyError> {
217 let site_view = SiteView::read_local(conn)?;
218 let community = Community::read_from_name(conn, community_name, false)?;
220 let posts = PostQuery::builder()
222 .sort(Some(*sort_type))
223 .community_id(Some(community.id))
224 .limit(Some(RSS_FETCH_LIMIT))
228 let items = create_post_items(posts, protocol_and_hostname)?;
230 let mut channel_builder = ChannelBuilder::default();
232 .namespaces(RSS_NAMESPACE.to_owned())
233 .title(&format!("{} - {}", site_view.site.name, community.name))
234 .link(community.actor_id.to_string())
237 if let Some(community_desc) = community.description {
238 channel_builder.description(&community_desc);
244 #[tracing::instrument(skip_all)]
248 sort_type: &SortType,
250 protocol_and_hostname: &str,
251 ) -> Result<ChannelBuilder, LemmyError> {
252 let site_view = SiteView::read_local(conn)?;
253 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
254 let local_user = LocalUser::read(conn, local_user_id)?;
256 let posts = PostQuery::builder()
258 .listing_type(Some(ListingType::Subscribed))
259 .my_person_id(Some(local_user.person_id))
260 .show_bot_accounts(Some(local_user.show_bot_accounts))
261 .show_read_posts(Some(local_user.show_read_posts))
262 .sort(Some(*sort_type))
263 .limit(Some(RSS_FETCH_LIMIT))
267 let items = create_post_items(posts, protocol_and_hostname)?;
269 let mut channel_builder = ChannelBuilder::default();
271 .namespaces(RSS_NAMESPACE.to_owned())
272 .title(&format!("{} - Subscribed", site_view.site.name))
273 .link(protocol_and_hostname)
276 if let Some(site_desc) = site_view.site.description {
277 channel_builder.description(&site_desc);
283 #[tracing::instrument(skip_all)]
288 protocol_and_hostname: &str,
289 ) -> Result<ChannelBuilder, LemmyError> {
290 let site_view = SiteView::read_local(conn)?;
291 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
292 let local_user = LocalUser::read(conn, local_user_id)?;
293 let person_id = local_user.person_id;
294 let show_bot_accounts = local_user.show_bot_accounts;
296 let sort = CommentSortType::New;
298 let replies = CommentReplyQuery::builder()
300 .recipient_id(Some(person_id))
301 .my_person_id(Some(person_id))
302 .show_bot_accounts(Some(show_bot_accounts))
304 .limit(Some(RSS_FETCH_LIMIT))
308 let mentions = PersonMentionQuery::builder()
310 .recipient_id(Some(person_id))
311 .my_person_id(Some(person_id))
312 .show_bot_accounts(Some(show_bot_accounts))
314 .limit(Some(RSS_FETCH_LIMIT))
318 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
320 let mut channel_builder = ChannelBuilder::default();
322 .namespaces(RSS_NAMESPACE.to_owned())
323 .title(&format!("{} - Inbox", site_view.site.name))
324 .link(format!("{}/inbox", protocol_and_hostname,))
327 if let Some(site_desc) = site_view.site.description {
328 channel_builder.description(&site_desc);
334 #[tracing::instrument(skip_all)]
335 fn create_reply_and_mention_items(
336 replies: Vec<CommentReplyView>,
337 mentions: Vec<PersonMentionView>,
338 protocol_and_hostname: &str,
339 ) -> Result<Vec<Item>, LemmyError> {
340 let mut reply_items: Vec<Item> = replies
343 let reply_url = format!(
344 "{}/post/{}/comment/{}",
345 protocol_and_hostname, r.post.id, r.comment.id
349 &r.comment.published,
352 protocol_and_hostname,
355 .collect::<Result<Vec<Item>, LemmyError>>()?;
357 let mut mention_items: Vec<Item> = mentions
360 let mention_url = format!(
361 "{}/post/{}/comment/{}",
362 protocol_and_hostname, m.post.id, m.comment.id
366 &m.comment.published,
369 protocol_and_hostname,
372 .collect::<Result<Vec<Item>, LemmyError>>()?;
374 reply_items.append(&mut mention_items);
378 #[tracing::instrument(skip_all)]
381 published: &NaiveDateTime,
384 protocol_and_hostname: &str,
385 ) -> Result<Item, LemmyError> {
386 let mut i = ItemBuilder::default();
387 i.title(format!("Reply from {}", creator_name));
388 let author_url = format!("{}/u/{}", protocol_and_hostname, creator_name);
390 "/u/{} <a href=\"{}\">(link)</a>",
391 creator_name, author_url
393 let dt = DateTime::<Utc>::from_utc(*published, Utc);
394 i.pub_date(dt.to_rfc2822());
395 i.comments(url.to_owned());
396 let guid = GuidBuilder::default().permalink(true).value(url).build();
398 i.link(url.to_owned());
400 let html = markdown_to_html(content);
405 #[tracing::instrument(skip_all)]
406 fn create_post_items(
407 posts: Vec<PostView>,
408 protocol_and_hostname: &str,
409 ) -> Result<Vec<Item>, LemmyError> {
410 let mut items: Vec<Item> = Vec::new();
413 let mut i = ItemBuilder::default();
414 let mut dc_extension = DublinCoreExtensionBuilder::default();
416 i.title(p.post.name);
418 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
420 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
421 i.pub_date(dt.to_rfc2822());
423 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
424 i.link(post_url.to_owned());
425 i.comments(post_url.to_owned());
426 let guid = GuidBuilder::default()
432 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
435 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
444 // If its a url post, add it to the description
445 if let Some(url) = p.post.url {
446 let link_html = format!("<br><a href=\"{url}\">{url}</a>", url = url);
447 description.push_str(&link_html);
450 if let Some(body) = p.post.body {
451 let html = markdown_to_html(&body);
452 description.push_str(&html);
455 i.description(description);
457 i.dublin_core_ext(dc_extension.build());
458 items.push(i.build());