1 use actix_web::{error::ErrorBadRequest, *};
3 use chrono::{DateTime, NaiveDateTime, Utc};
4 use diesel::PgConnection;
5 use lemmy_api_common::blocking;
8 source::{community::Community, local_user::LocalUser, person::Person},
9 traits::{ApubActor, Crud},
14 comment_view::{CommentQueryBuilder, CommentView},
15 post_view::{PostQueryBuilder, PostView},
18 use lemmy_db_views_actor::person_mention_view::{PersonMentionQueryBuilder, PersonMentionView};
19 use lemmy_utils::{claims::Claims, utils::markdown_to_html, LemmyError};
20 use lemmy_websocket::LemmyContext;
21 use once_cell::sync::Lazy;
23 extension::dublincore::DublinCoreExtensionBuilder,
29 use serde::Deserialize;
30 use std::{collections::BTreeMap, str::FromStr};
31 use strum::ParseError;
33 #[derive(Deserialize)]
45 pub fn config(cfg: &mut web::ServiceConfig) {
47 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
48 .route("/feeds/all.xml", web::get().to(get_all_feed))
49 .route("/feeds/local.xml", web::get().to(get_local_feed));
52 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
53 let mut h = BTreeMap::new();
56 rss::extension::dublincore::NAMESPACE.to_string(),
61 #[tracing::instrument(skip_all)]
62 async fn get_all_feed(
63 info: web::Query<Params>,
64 context: web::Data<LemmyContext>,
65 ) -> Result<HttpResponse, Error> {
66 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
67 Ok(get_feed_data(&context, ListingType::All, sort_type).await?)
70 #[tracing::instrument(skip_all)]
71 async fn get_local_feed(
72 info: web::Query<Params>,
73 context: web::Data<LemmyContext>,
74 ) -> Result<HttpResponse, Error> {
75 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
76 Ok(get_feed_data(&context, ListingType::Local, sort_type).await?)
79 #[tracing::instrument(skip_all)]
80 async fn get_feed_data(
81 context: &LemmyContext,
82 listing_type: ListingType,
84 ) -> Result<HttpResponse, LemmyError> {
85 let site_view = blocking(context.pool(), SiteView::read_local).await??;
87 let posts = blocking(context.pool(), move |conn| {
88 PostQueryBuilder::create(conn)
89 .listing_type(listing_type)
95 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
97 let mut channel_builder = ChannelBuilder::default();
99 .namespaces(RSS_NAMESPACE.to_owned())
100 .title(&format!("{} - {}", site_view.site.name, listing_type))
101 .link(context.settings().get_protocol_and_hostname())
104 if let Some(site_desc) = site_view.site.description {
105 channel_builder.description(&site_desc);
108 let rss = channel_builder.build().to_string();
111 .content_type("application/rss+xml")
116 #[tracing::instrument(skip_all)]
119 info: web::Query<Params>,
120 context: web::Data<LemmyContext>,
121 ) -> Result<HttpResponse, Error> {
122 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
124 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
125 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
127 let request_type = match req_type.as_str() {
128 "u" => RequestType::User,
129 "c" => RequestType::Community,
130 "front" => RequestType::Front,
131 "inbox" => RequestType::Inbox,
132 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
135 let jwt_secret = context.secret().jwt_secret.to_owned();
136 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
138 let builder = blocking(context.pool(), move |conn| match request_type {
139 RequestType::User => get_feed_user(conn, &sort_type, ¶m, &protocol_and_hostname),
140 RequestType::Community => get_feed_community(conn, &sort_type, ¶m, &protocol_and_hostname),
141 RequestType::Front => get_feed_front(
146 &protocol_and_hostname,
148 RequestType::Inbox => get_feed_inbox(conn, &jwt_secret, ¶m, &protocol_and_hostname),
151 .map_err(ErrorBadRequest)?;
153 let rss = builder.build().to_string();
157 .content_type("application/rss+xml")
162 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
163 let sort_query = info
166 .unwrap_or_else(|| SortType::Hot.to_string());
167 SortType::from_str(&sort_query)
170 #[tracing::instrument(skip_all)]
173 sort_type: &SortType,
175 protocol_and_hostname: &str,
176 ) -> Result<ChannelBuilder, LemmyError> {
177 let site_view = SiteView::read_local(conn)?;
178 let person = Person::read_from_name(conn, user_name)?;
180 let posts = PostQueryBuilder::create(conn)
181 .listing_type(ListingType::All)
183 .creator_id(person.id)
186 let items = create_post_items(posts, protocol_and_hostname)?;
188 let mut channel_builder = ChannelBuilder::default();
190 .namespaces(RSS_NAMESPACE.to_owned())
191 .title(&format!("{} - {}", site_view.site.name, person.name))
192 .link(person.actor_id.to_string())
198 #[tracing::instrument(skip_all)]
199 fn get_feed_community(
201 sort_type: &SortType,
202 community_name: &str,
203 protocol_and_hostname: &str,
204 ) -> Result<ChannelBuilder, LemmyError> {
205 let site_view = SiteView::read_local(conn)?;
206 let community = Community::read_from_name(conn, community_name)?;
208 let posts = PostQueryBuilder::create(conn)
209 .listing_type(ListingType::Community)
211 .community_id(community.id)
214 let items = create_post_items(posts, protocol_and_hostname)?;
216 let mut channel_builder = ChannelBuilder::default();
218 .namespaces(RSS_NAMESPACE.to_owned())
219 .title(&format!("{} - {}", site_view.site.name, community.name))
220 .link(community.actor_id.to_string())
223 if let Some(community_desc) = community.description {
224 channel_builder.description(&community_desc);
230 #[tracing::instrument(skip_all)]
234 sort_type: &SortType,
236 protocol_and_hostname: &str,
237 ) -> Result<ChannelBuilder, LemmyError> {
238 let site_view = SiteView::read_local(conn)?;
239 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
240 let local_user = LocalUser::read(conn, local_user_id)?;
242 let posts = PostQueryBuilder::create(conn)
243 .listing_type(ListingType::Subscribed)
244 .my_person_id(local_user.person_id)
245 .show_bot_accounts(local_user.show_bot_accounts)
246 .show_read_posts(local_user.show_read_posts)
250 let items = create_post_items(posts, protocol_and_hostname)?;
252 let mut channel_builder = ChannelBuilder::default();
254 .namespaces(RSS_NAMESPACE.to_owned())
255 .title(&format!("{} - Subscribed", site_view.site.name))
256 .link(protocol_and_hostname)
259 if let Some(site_desc) = site_view.site.description {
260 channel_builder.description(&site_desc);
266 #[tracing::instrument(skip_all)]
271 protocol_and_hostname: &str,
272 ) -> Result<ChannelBuilder, LemmyError> {
273 let site_view = SiteView::read_local(conn)?;
274 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
275 let local_user = LocalUser::read(conn, local_user_id)?;
276 let person_id = local_user.person_id;
277 let show_bot_accounts = local_user.show_bot_accounts;
279 let sort = SortType::New;
281 let replies = CommentQueryBuilder::create(conn)
282 .recipient_id(person_id)
283 .my_person_id(person_id)
284 .show_bot_accounts(show_bot_accounts)
288 let mentions = PersonMentionQueryBuilder::create(conn)
289 .recipient_id(person_id)
290 .my_person_id(person_id)
294 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
296 let mut channel_builder = ChannelBuilder::default();
298 .namespaces(RSS_NAMESPACE.to_owned())
299 .title(&format!("{} - Inbox", site_view.site.name))
300 .link(format!("{}/inbox", protocol_and_hostname,))
303 if let Some(site_desc) = site_view.site.description {
304 channel_builder.description(&site_desc);
310 #[tracing::instrument(skip_all)]
311 fn create_reply_and_mention_items(
312 replies: Vec<CommentView>,
313 mentions: Vec<PersonMentionView>,
314 protocol_and_hostname: &str,
315 ) -> Result<Vec<Item>, LemmyError> {
316 let mut reply_items: Vec<Item> = replies
319 let reply_url = format!(
320 "{}/post/{}/comment/{}",
321 protocol_and_hostname, r.post.id, r.comment.id
325 &r.comment.published,
328 protocol_and_hostname,
331 .collect::<Result<Vec<Item>, LemmyError>>()?;
333 let mut mention_items: Vec<Item> = mentions
336 let mention_url = format!(
337 "{}/post/{}/comment/{}",
338 protocol_and_hostname, m.post.id, m.comment.id
342 &m.comment.published,
345 protocol_and_hostname,
348 .collect::<Result<Vec<Item>, LemmyError>>()?;
350 reply_items.append(&mut mention_items);
354 #[tracing::instrument(skip_all)]
357 published: &NaiveDateTime,
360 protocol_and_hostname: &str,
361 ) -> Result<Item, LemmyError> {
362 let mut i = ItemBuilder::default();
363 i.title(format!("Reply from {}", creator_name));
364 let author_url = format!("{}/u/{}", protocol_and_hostname, creator_name);
366 "/u/{} <a href=\"{}\">(link)</a>",
367 creator_name, author_url
369 let dt = DateTime::<Utc>::from_utc(*published, Utc);
370 i.pub_date(dt.to_rfc2822());
371 i.comments(url.to_owned());
372 let guid = GuidBuilder::default().permalink(true).value(url).build();
374 i.link(url.to_owned());
376 let html = markdown_to_html(&content.to_string());
381 #[tracing::instrument(skip_all)]
382 fn create_post_items(
383 posts: Vec<PostView>,
384 protocol_and_hostname: &str,
385 ) -> Result<Vec<Item>, LemmyError> {
386 let mut items: Vec<Item> = Vec::new();
389 let mut i = ItemBuilder::default();
390 let mut dc_extension = DublinCoreExtensionBuilder::default();
392 i.title(p.post.name);
394 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
396 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
397 i.pub_date(dt.to_rfc2822());
399 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
400 i.link(post_url.to_owned());
401 i.comments(post_url.to_owned());
402 let guid = GuidBuilder::default()
408 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
411 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
420 // If its a url post, add it to the description
421 if let Some(url) = p.post.url {
422 let link_html = format!("<br><a href=\"{url}\">{url}</a>", url = url);
423 description.push_str(&link_html);
426 if let Some(body) = p.post.body {
427 let html = markdown_to_html(&body);
428 description.push_str(&html);
431 i.description(description);
433 i.dublin_core_ext(dc_extension.build());
434 items.push(i.build());