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},
14 comment_view::CommentQueryBuilder,
15 post_view::PostQueryBuilder,
16 structs::{CommentView, PostView, SiteView},
18 use lemmy_db_views_actor::{
19 person_mention_view::PersonMentionQueryBuilder,
20 structs::PersonMentionView,
22 use lemmy_utils::{claims::Claims, error::LemmyError, utils::markdown_to_html};
23 use lemmy_websocket::LemmyContext;
24 use once_cell::sync::Lazy;
26 extension::dublincore::DublinCoreExtensionBuilder,
32 use serde::Deserialize;
33 use std::{collections::BTreeMap, str::FromStr};
34 use strum::ParseError;
36 const RSS_FETCH_LIMIT: i64 = 20;
38 #[derive(Deserialize)]
50 pub fn config(cfg: &mut web::ServiceConfig) {
52 .route("/feeds/{type}/{name}.xml", web::get().to(get_feed))
53 .route("/feeds/all.xml", web::get().to(get_all_feed))
54 .route("/feeds/local.xml", web::get().to(get_local_feed));
57 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
58 let mut h = BTreeMap::new();
61 rss::extension::dublincore::NAMESPACE.to_string(),
66 #[tracing::instrument(skip_all)]
67 async fn get_all_feed(
68 info: web::Query<Params>,
69 context: web::Data<LemmyContext>,
70 ) -> Result<HttpResponse, Error> {
71 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
72 Ok(get_feed_data(&context, ListingType::All, sort_type).await?)
75 #[tracing::instrument(skip_all)]
76 async fn get_local_feed(
77 info: web::Query<Params>,
78 context: web::Data<LemmyContext>,
79 ) -> Result<HttpResponse, Error> {
80 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
81 Ok(get_feed_data(&context, ListingType::Local, sort_type).await?)
84 #[tracing::instrument(skip_all)]
85 async fn get_feed_data(
86 context: &LemmyContext,
87 listing_type: ListingType,
89 ) -> Result<HttpResponse, LemmyError> {
90 let site_view = blocking(context.pool(), SiteView::read_local).await??;
92 let posts = blocking(context.pool(), move |conn| {
93 PostQueryBuilder::create(conn)
94 .listing_type(listing_type)
96 .limit(RSS_FETCH_LIMIT)
101 let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
103 let mut channel_builder = ChannelBuilder::default();
105 .namespaces(RSS_NAMESPACE.to_owned())
106 .title(&format!("{} - {}", site_view.site.name, listing_type))
107 .link(context.settings().get_protocol_and_hostname())
110 if let Some(site_desc) = site_view.site.description {
111 channel_builder.description(&site_desc);
114 let rss = channel_builder.build().to_string();
117 .content_type("application/rss+xml")
122 #[tracing::instrument(skip_all)]
125 info: web::Query<Params>,
126 context: web::Data<LemmyContext>,
127 ) -> Result<HttpResponse, Error> {
128 let sort_type = get_sort_type(info).map_err(ErrorBadRequest)?;
130 let req_type: String = req.match_info().get("type").unwrap_or("none").parse()?;
131 let param: String = req.match_info().get("name").unwrap_or("none").parse()?;
133 let request_type = match req_type.as_str() {
134 "u" => RequestType::User,
135 "c" => RequestType::Community,
136 "front" => RequestType::Front,
137 "inbox" => RequestType::Inbox,
138 _ => return Err(ErrorBadRequest(LemmyError::from(anyhow!("wrong_type")))),
141 let jwt_secret = context.secret().jwt_secret.to_owned();
142 let protocol_and_hostname = context.settings().get_protocol_and_hostname();
144 let builder = blocking(context.pool(), move |conn| match request_type {
145 RequestType::User => get_feed_user(conn, &sort_type, ¶m, &protocol_and_hostname),
146 RequestType::Community => get_feed_community(conn, &sort_type, ¶m, &protocol_and_hostname),
147 RequestType::Front => get_feed_front(
152 &protocol_and_hostname,
154 RequestType::Inbox => get_feed_inbox(conn, &jwt_secret, ¶m, &protocol_and_hostname),
157 .map_err(ErrorBadRequest)?;
159 let rss = builder.build().to_string();
163 .content_type("application/rss+xml")
168 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
169 let sort_query = info
172 .unwrap_or_else(|| SortType::Hot.to_string());
173 SortType::from_str(&sort_query)
176 #[tracing::instrument(skip_all)]
179 sort_type: &SortType,
181 protocol_and_hostname: &str,
182 ) -> Result<ChannelBuilder, LemmyError> {
183 let site_view = SiteView::read_local(conn)?;
184 let person = Person::read_from_name(conn, user_name, false)?;
186 let posts = PostQueryBuilder::create(conn)
187 .listing_type(ListingType::All)
189 .creator_id(person.id)
190 .limit(RSS_FETCH_LIMIT)
193 let items = create_post_items(posts, protocol_and_hostname)?;
195 let mut channel_builder = ChannelBuilder::default();
197 .namespaces(RSS_NAMESPACE.to_owned())
198 .title(&format!("{} - {}", site_view.site.name, person.name))
199 .link(person.actor_id.to_string())
205 #[tracing::instrument(skip_all)]
206 fn get_feed_community(
208 sort_type: &SortType,
209 community_name: &str,
210 protocol_and_hostname: &str,
211 ) -> Result<ChannelBuilder, LemmyError> {
212 let site_view = SiteView::read_local(conn)?;
213 let community = Community::read_from_name(conn, community_name, false)?;
215 let posts = PostQueryBuilder::create(conn)
217 .community_id(community.id)
218 .limit(RSS_FETCH_LIMIT)
221 let items = create_post_items(posts, protocol_and_hostname)?;
223 let mut channel_builder = ChannelBuilder::default();
225 .namespaces(RSS_NAMESPACE.to_owned())
226 .title(&format!("{} - {}", site_view.site.name, community.name))
227 .link(community.actor_id.to_string())
230 if let Some(community_desc) = community.description {
231 channel_builder.description(&community_desc);
237 #[tracing::instrument(skip_all)]
241 sort_type: &SortType,
243 protocol_and_hostname: &str,
244 ) -> Result<ChannelBuilder, LemmyError> {
245 let site_view = SiteView::read_local(conn)?;
246 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
247 let local_user = LocalUser::read(conn, local_user_id)?;
249 let posts = PostQueryBuilder::create(conn)
250 .listing_type(ListingType::Subscribed)
251 .my_person_id(local_user.person_id)
252 .show_bot_accounts(local_user.show_bot_accounts)
253 .show_read_posts(local_user.show_read_posts)
255 .limit(RSS_FETCH_LIMIT)
258 let items = create_post_items(posts, protocol_and_hostname)?;
260 let mut channel_builder = ChannelBuilder::default();
262 .namespaces(RSS_NAMESPACE.to_owned())
263 .title(&format!("{} - Subscribed", site_view.site.name))
264 .link(protocol_and_hostname)
267 if let Some(site_desc) = site_view.site.description {
268 channel_builder.description(&site_desc);
274 #[tracing::instrument(skip_all)]
279 protocol_and_hostname: &str,
280 ) -> Result<ChannelBuilder, LemmyError> {
281 let site_view = SiteView::read_local(conn)?;
282 let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
283 let local_user = LocalUser::read(conn, local_user_id)?;
284 let person_id = local_user.person_id;
285 let show_bot_accounts = local_user.show_bot_accounts;
287 let sort = SortType::New;
289 let replies = CommentQueryBuilder::create(conn)
290 .recipient_id(person_id)
291 .my_person_id(person_id)
292 .show_bot_accounts(show_bot_accounts)
294 .limit(RSS_FETCH_LIMIT)
297 let mentions = PersonMentionQueryBuilder::create(conn)
298 .recipient_id(person_id)
299 .my_person_id(person_id)
301 .limit(RSS_FETCH_LIMIT)
304 let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
306 let mut channel_builder = ChannelBuilder::default();
308 .namespaces(RSS_NAMESPACE.to_owned())
309 .title(&format!("{} - Inbox", site_view.site.name))
310 .link(format!("{}/inbox", protocol_and_hostname,))
313 if let Some(site_desc) = site_view.site.description {
314 channel_builder.description(&site_desc);
320 #[tracing::instrument(skip_all)]
321 fn create_reply_and_mention_items(
322 replies: Vec<CommentView>,
323 mentions: Vec<PersonMentionView>,
324 protocol_and_hostname: &str,
325 ) -> Result<Vec<Item>, LemmyError> {
326 let mut reply_items: Vec<Item> = replies
329 let reply_url = format!(
330 "{}/post/{}/comment/{}",
331 protocol_and_hostname, r.post.id, r.comment.id
335 &r.comment.published,
338 protocol_and_hostname,
341 .collect::<Result<Vec<Item>, LemmyError>>()?;
343 let mut mention_items: Vec<Item> = mentions
346 let mention_url = format!(
347 "{}/post/{}/comment/{}",
348 protocol_and_hostname, m.post.id, m.comment.id
352 &m.comment.published,
355 protocol_and_hostname,
358 .collect::<Result<Vec<Item>, LemmyError>>()?;
360 reply_items.append(&mut mention_items);
364 #[tracing::instrument(skip_all)]
367 published: &NaiveDateTime,
370 protocol_and_hostname: &str,
371 ) -> Result<Item, LemmyError> {
372 let mut i = ItemBuilder::default();
373 i.title(format!("Reply from {}", creator_name));
374 let author_url = format!("{}/u/{}", protocol_and_hostname, creator_name);
376 "/u/{} <a href=\"{}\">(link)</a>",
377 creator_name, author_url
379 let dt = DateTime::<Utc>::from_utc(*published, Utc);
380 i.pub_date(dt.to_rfc2822());
381 i.comments(url.to_owned());
382 let guid = GuidBuilder::default().permalink(true).value(url).build();
384 i.link(url.to_owned());
386 let html = markdown_to_html(content);
391 #[tracing::instrument(skip_all)]
392 fn create_post_items(
393 posts: Vec<PostView>,
394 protocol_and_hostname: &str,
395 ) -> Result<Vec<Item>, LemmyError> {
396 let mut items: Vec<Item> = Vec::new();
399 let mut i = ItemBuilder::default();
400 let mut dc_extension = DublinCoreExtensionBuilder::default();
402 i.title(p.post.name);
404 dc_extension.creators(vec![p.creator.actor_id.to_string()]);
406 let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
407 i.pub_date(dt.to_rfc2822());
409 let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
410 i.link(post_url.to_owned());
411 i.comments(post_url.to_owned());
412 let guid = GuidBuilder::default()
418 let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
421 let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
430 // If its a url post, add it to the description
431 if let Some(url) = p.post.url {
432 let link_html = format!("<br><a href=\"{url}\">{url}</a>", url = url);
433 description.push_str(&link_html);
436 if let Some(body) = p.post.body {
437 let html = markdown_to_html(&body);
438 description.push_str(&html);
441 i.description(description);
443 i.dublin_core_ext(dc_extension.build());
444 items.push(i.build());