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},
10 utils::{ListingType, SortType},
13 comment_view::CommentQueryBuilder,
14 post_view::PostQueryBuilder,
15 structs::{CommentView, PostView, SiteView},
17 use lemmy_db_views_actor::{
18 person_mention_view::PersonMentionQueryBuilder,
19 structs::PersonMentionView,
21 use lemmy_utils::{claims::Claims, utils::markdown_to_html, LemmyError};
22 use lemmy_websocket::LemmyContext;
23 use once_cell::sync::Lazy;
25 extension::dublincore::DublinCoreExtensionBuilder,
31 use serde::Deserialize;
32 use std::{collections::BTreeMap, str::FromStr};
33 use strum::ParseError;
35 #[derive(Deserialize)]
47 pub fn config(cfg: &mut web::ServiceConfig) {
49 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
50 .route("/feeds/all.xml", web::get().to(get_all_feed))
51 .route("/feeds/local.xml", web::get().to(get_local_feed));
54 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
55 let mut h = BTreeMap::new();
58 rss::extension::dublincore::NAMESPACE.to_string(),
63 #[tracing::instrument(skip_all)]
64 async fn get_all_feed(
65 info: web::Query<Params>,
66 context: web::Data<LemmyContext>,
67 ) -> Result<HttpResponse, Error> {
68 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
69 Ok(get_feed_data(&context, ListingType::All, sort_type).await?)
72 #[tracing::instrument(skip_all)]
73 async fn get_local_feed(
74 info: web::Query<Params>,
75 context: web::Data<LemmyContext>,
76 ) -> Result<HttpResponse, Error> {
77 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
78 Ok(get_feed_data(&context, ListingType::Local, sort_type).await?)
81 #[tracing::instrument(skip_all)]
82 async fn get_feed_data(
83 context: &LemmyContext,
84 listing_type: ListingType,
86 ) -> Result<HttpResponse, LemmyError> {
87 let site_view = blocking(context.pool(), SiteView::read_local).await??;
89 let posts = blocking(context.pool(), move |conn| {
90 PostQueryBuilder::create(conn)
91 .listing_type(listing_type)
97 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
99 let mut channel_builder = ChannelBuilder::default();
101 .namespaces(RSS_NAMESPACE.to_owned())
102 .title(&format!("{} - {}", site_view.site.name, listing_type))
103 .link(context.settings().get_protocol_and_hostname())
106 if let Some(site_desc) = site_view.site.description {
107 channel_builder.description(&site_desc);
110 let rss = channel_builder.build().to_string();
113 .content_type("application/rss+xml")
118 #[tracing::instrument(skip_all)]
121 info: web::Query<Params>,
122 context: web::Data<LemmyContext>,
123 ) -> Result<HttpResponse, Error> {
124 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
126 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
127 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
129 let request_type = match req_type.as_str() {
130 "u" => RequestType::User,
131 "c" => RequestType::Community,
132 "front" => RequestType::Front,
133 "inbox" => RequestType::Inbox,
134 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
137 let jwt_secret = context.secret().jwt_secret.to_owned();
138 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
140 let builder = blocking(context.pool(), move |conn| match request_type {
141 RequestType::User => get_feed_user(conn, &sort_type, ¶m, &protocol_and_hostname),
142 RequestType::Community => get_feed_community(conn, &sort_type, ¶m, &protocol_and_hostname),
143 RequestType::Front => get_feed_front(
148 &protocol_and_hostname,
150 RequestType::Inbox => get_feed_inbox(conn, &jwt_secret, ¶m, &protocol_and_hostname),
153 .map_err(ErrorBadRequest)?;
155 let rss = builder.build().to_string();
159 .content_type("application/rss+xml")
164 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
165 let sort_query = info
168 .unwrap_or_else(|| SortType::Hot.to_string());
169 SortType::from_str(&sort_query)
172 #[tracing::instrument(skip_all)]
175 sort_type: &SortType,
177 protocol_and_hostname: &str,
178 ) -> Result<ChannelBuilder, LemmyError> {
179 let site_view = SiteView::read_local(conn)?;
180 let person = Person::read_from_name(conn, user_name)?;
182 let posts = PostQueryBuilder::create(conn)
183 .listing_type(ListingType::All)
185 .creator_id(person.id)
188 let items = create_post_items(posts, protocol_and_hostname)?;
190 let mut channel_builder = ChannelBuilder::default();
192 .namespaces(RSS_NAMESPACE.to_owned())
193 .title(&format!("{} - {}", site_view.site.name, person.name))
194 .link(person.actor_id.to_string())
200 #[tracing::instrument(skip_all)]
201 fn get_feed_community(
203 sort_type: &SortType,
204 community_name: &str,
205 protocol_and_hostname: &str,
206 ) -> Result<ChannelBuilder, LemmyError> {
207 let site_view = SiteView::read_local(conn)?;
208 let community = Community::read_from_name(conn, community_name)?;
210 let posts = PostQueryBuilder::create(conn)
211 .listing_type(ListingType::Community)
213 .community_id(community.id)
216 let items = create_post_items(posts, protocol_and_hostname)?;
218 let mut channel_builder = ChannelBuilder::default();
220 .namespaces(RSS_NAMESPACE.to_owned())
221 .title(&format!("{} - {}", site_view.site.name, community.name))
222 .link(community.actor_id.to_string())
225 if let Some(community_desc) = community.description {
226 channel_builder.description(&community_desc);
232 #[tracing::instrument(skip_all)]
236 sort_type: &SortType,
238 protocol_and_hostname: &str,
239 ) -> Result<ChannelBuilder, LemmyError> {
240 let site_view = SiteView::read_local(conn)?;
241 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
242 let local_user = LocalUser::read(conn, local_user_id)?;
244 let posts = PostQueryBuilder::create(conn)
245 .listing_type(ListingType::Subscribed)
246 .my_person_id(local_user.person_id)
247 .show_bot_accounts(local_user.show_bot_accounts)
248 .show_read_posts(local_user.show_read_posts)
252 let items = create_post_items(posts, protocol_and_hostname)?;
254 let mut channel_builder = ChannelBuilder::default();
256 .namespaces(RSS_NAMESPACE.to_owned())
257 .title(&format!("{} - Subscribed", site_view.site.name))
258 .link(protocol_and_hostname)
261 if let Some(site_desc) = site_view.site.description {
262 channel_builder.description(&site_desc);
268 #[tracing::instrument(skip_all)]
273 protocol_and_hostname: &str,
274 ) -> Result<ChannelBuilder, LemmyError> {
275 let site_view = SiteView::read_local(conn)?;
276 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
277 let local_user = LocalUser::read(conn, local_user_id)?;
278 let person_id = local_user.person_id;
279 let show_bot_accounts = local_user.show_bot_accounts;
281 let sort = SortType::New;
283 let replies = CommentQueryBuilder::create(conn)
284 .recipient_id(person_id)
285 .my_person_id(person_id)
286 .show_bot_accounts(show_bot_accounts)
290 let mentions = PersonMentionQueryBuilder::create(conn)
291 .recipient_id(person_id)
292 .my_person_id(person_id)
296 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
298 let mut channel_builder = ChannelBuilder::default();
300 .namespaces(RSS_NAMESPACE.to_owned())
301 .title(&format!("{} - Inbox", site_view.site.name))
302 .link(format!("{}/inbox", protocol_and_hostname,))
305 if let Some(site_desc) = site_view.site.description {
306 channel_builder.description(&site_desc);
312 #[tracing::instrument(skip_all)]
313 fn create_reply_and_mention_items(
314 replies: Vec<CommentView>,
315 mentions: Vec<PersonMentionView>,
316 protocol_and_hostname: &str,
317 ) -> Result<Vec<Item>, LemmyError> {
318 let mut reply_items: Vec<Item> = replies
321 let reply_url = format!(
322 "{}/post/{}/comment/{}",
323 protocol_and_hostname, r.post.id, r.comment.id
327 &r.comment.published,
330 protocol_and_hostname,
333 .collect::<Result<Vec<Item>, LemmyError>>()?;
335 let mut mention_items: Vec<Item> = mentions
338 let mention_url = format!(
339 "{}/post/{}/comment/{}",
340 protocol_and_hostname, m.post.id, m.comment.id
344 &m.comment.published,
347 protocol_and_hostname,
350 .collect::<Result<Vec<Item>, LemmyError>>()?;
352 reply_items.append(&mut mention_items);
356 #[tracing::instrument(skip_all)]
359 published: &NaiveDateTime,
362 protocol_and_hostname: &str,
363 ) -> Result<Item, LemmyError> {
364 let mut i = ItemBuilder::default();
365 i.title(format!("Reply from {}", creator_name));
366 let author_url = format!("{}/u/{}", protocol_and_hostname, creator_name);
368 "/u/{} <a href=\"{}\">(link)</a>",
369 creator_name, author_url
371 let dt = DateTime::<Utc>::from_utc(*published, Utc);
372 i.pub_date(dt.to_rfc2822());
373 i.comments(url.to_owned());
374 let guid = GuidBuilder::default().permalink(true).value(url).build();
376 i.link(url.to_owned());
378 let html = markdown_to_html(content);
383 #[tracing::instrument(skip_all)]
384 fn create_post_items(
385 posts: Vec<PostView>,
386 protocol_and_hostname: &str,
387 ) -> Result<Vec<Item>, LemmyError> {
388 let mut items: Vec<Item> = Vec::new();
391 let mut i = ItemBuilder::default();
392 let mut dc_extension = DublinCoreExtensionBuilder::default();
394 i.title(p.post.name);
396 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
398 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
399 i.pub_date(dt.to_rfc2822());
401 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
402 i.link(post_url.to_owned());
403 i.comments(post_url.to_owned());
404 let guid = GuidBuilder::default()
410 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
413 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
422 // If its a url post, add it to the description
423 if let Some(url) = p.post.url {
424 let link_html = format!("<br><a href=\"{url}\">{url}</a>", url = url);
425 description.push_str(&link_html);
428 if let Some(body) = p.post.body {
429 let html = markdown_to_html(&body);
430 description.push_str(&html);
433 i.description(description);
435 i.dublin_core_ext(dc_extension.build());
436 items.push(i.build());