]> Untitled Git - lemmy.git/blob - crates/routes/src/feeds.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[lemmy.git] / crates / routes / src / feeds.rs
1 use actix_web::{error::ErrorBadRequest, web, Error, HttpRequest, HttpResponse, Result};
2 use anyhow::anyhow;
3 use chrono::{DateTime, NaiveDateTime, Utc};
4 use lemmy_api_common::context::LemmyContext;
5 use lemmy_db_schema::{
6   newtypes::LocalUserId,
7   source::{community::Community, local_user::LocalUser, person::Person},
8   traits::{ApubActor, Crud},
9   utils::DbPool,
10   CommentSortType,
11   ListingType,
12   SortType,
13 };
14 use lemmy_db_views::{
15   post_view::PostQuery,
16   structs::{LocalUserView, PostView, SiteView},
17 };
18 use lemmy_db_views_actor::{
19   comment_reply_view::CommentReplyQuery,
20   person_mention_view::PersonMentionQuery,
21   structs::{CommentReplyView, PersonMentionView},
22 };
23 use lemmy_utils::{
24   cache_header::cache_1hour,
25   claims::Claims,
26   error::LemmyError,
27   utils::markdown::markdown_to_html,
28 };
29 use once_cell::sync::Lazy;
30 use rss::{
31   extension::dublincore::DublinCoreExtensionBuilder,
32   ChannelBuilder,
33   GuidBuilder,
34   Item,
35   ItemBuilder,
36 };
37 use serde::Deserialize;
38 use std::{collections::BTreeMap, str::FromStr};
39
40 const RSS_FETCH_LIMIT: i64 = 20;
41
42 #[derive(Deserialize)]
43 struct Params {
44   sort: Option<String>,
45   limit: Option<i64>,
46   page: Option<i64>,
47 }
48
49 impl Params {
50   fn sort_type(&self) -> Result<SortType, Error> {
51     let sort_query = self
52       .sort
53       .clone()
54       .unwrap_or_else(|| SortType::Hot.to_string());
55     SortType::from_str(&sort_query).map_err(ErrorBadRequest)
56   }
57   fn get_limit(&self) -> i64 {
58     self.limit.unwrap_or(RSS_FETCH_LIMIT)
59   }
60   fn get_page(&self) -> i64 {
61     self.page.unwrap_or(1)
62   }
63 }
64
65 enum RequestType {
66   Community,
67   User,
68   Front,
69   Inbox,
70 }
71
72 pub fn config(cfg: &mut web::ServiceConfig) {
73   cfg.service(
74     web::scope("/feeds")
75       .route("/{type}/{name}.xml", web::get().to(get_feed))
76       .route("/all.xml", web::get().to(get_all_feed).wrap(cache_1hour()))
77       .route(
78         "/local.xml",
79         web::get().to(get_local_feed).wrap(cache_1hour()),
80       ),
81   );
82 }
83
84 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
85   let mut h = BTreeMap::new();
86   h.insert(
87     "dc".to_string(),
88     rss::extension::dublincore::NAMESPACE.to_string(),
89   );
90   h
91 });
92
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> {
98   Ok(
99     get_feed_data(
100       &context,
101       ListingType::All,
102       info.sort_type()?,
103       info.get_limit(),
104       info.get_page(),
105     )
106     .await?,
107   )
108 }
109
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> {
115   Ok(
116     get_feed_data(
117       &context,
118       ListingType::Local,
119       info.sort_type()?,
120       info.get_limit(),
121       info.get_page(),
122     )
123     .await?,
124   )
125 }
126
127 #[tracing::instrument(skip_all)]
128 async fn get_feed_data(
129   context: &LemmyContext,
130   listing_type: ListingType,
131   sort_type: SortType,
132   limit: i64,
133   page: i64,
134 ) -> Result<HttpResponse, LemmyError> {
135   let site_view = SiteView::read_local(&mut context.pool()).await?;
136
137   let posts = PostQuery {
138     listing_type: (Some(listing_type)),
139     sort: (Some(sort_type)),
140     limit: (Some(limit)),
141     page: (Some(page)),
142     ..Default::default()
143   }
144   .list(&mut context.pool())
145   .await?;
146
147   let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
148
149   let mut channel_builder = ChannelBuilder::default();
150   channel_builder
151     .namespaces(RSS_NAMESPACE.clone())
152     .title(&format!("{} - {}", site_view.site.name, listing_type))
153     .link(context.settings().get_protocol_and_hostname())
154     .items(items);
155
156   if let Some(site_desc) = site_view.site.description {
157     channel_builder.description(&site_desc);
158   }
159
160   let rss = channel_builder.build().to_string();
161   Ok(
162     HttpResponse::Ok()
163       .content_type("application/rss+xml")
164       .body(rss),
165   )
166 }
167
168 #[tracing::instrument(skip_all)]
169 async fn get_feed(
170   req: HttpRequest,
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()?;
176
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")))),
183   };
184
185   let jwt_secret = context.secret().jwt_secret.clone();
186   let protocol_and_hostname = context.settings().get_protocol_and_hostname();
187
188   let builder = match request_type {
189     RequestType::User => {
190       get_feed_user(
191         &mut context.pool(),
192         &info.sort_type()?,
193         &info.get_limit(),
194         &info.get_page(),
195         &param,
196         &protocol_and_hostname,
197       )
198       .await
199     }
200     RequestType::Community => {
201       get_feed_community(
202         &mut context.pool(),
203         &info.sort_type()?,
204         &info.get_limit(),
205         &info.get_page(),
206         &param,
207         &protocol_and_hostname,
208       )
209       .await
210     }
211     RequestType::Front => {
212       get_feed_front(
213         &mut context.pool(),
214         &jwt_secret,
215         &info.sort_type()?,
216         &info.get_limit(),
217         &info.get_page(),
218         &param,
219         &protocol_and_hostname,
220       )
221       .await
222     }
223     RequestType::Inbox => {
224       get_feed_inbox(
225         &mut context.pool(),
226         &jwt_secret,
227         &param,
228         &protocol_and_hostname,
229       )
230       .await
231     }
232   }
233   .map_err(ErrorBadRequest)?;
234
235   let rss = builder.build().to_string();
236
237   Ok(
238     HttpResponse::Ok()
239       .content_type("application/rss+xml")
240       .body(rss),
241   )
242 }
243
244 #[tracing::instrument(skip_all)]
245 async fn get_feed_user(
246   pool: &mut DbPool<'_>,
247   sort_type: &SortType,
248   limit: &i64,
249   page: &i64,
250   user_name: &str,
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?;
255
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)),
261     page: (Some(*page)),
262     ..Default::default()
263   }
264   .list(pool)
265   .await?;
266
267   let items = create_post_items(posts, protocol_and_hostname)?;
268
269   let mut channel_builder = ChannelBuilder::default();
270   channel_builder
271     .namespaces(RSS_NAMESPACE.clone())
272     .title(&format!("{} - {}", site_view.site.name, person.name))
273     .link(person.actor_id.to_string())
274     .items(items);
275
276   Ok(channel_builder)
277 }
278
279 #[tracing::instrument(skip_all)]
280 async fn get_feed_community(
281   pool: &mut DbPool<'_>,
282   sort_type: &SortType,
283   limit: &i64,
284   page: &i64,
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?;
290
291   let posts = PostQuery {
292     sort: (Some(*sort_type)),
293     community_id: (Some(community.id)),
294     limit: (Some(*limit)),
295     page: (Some(*page)),
296     ..Default::default()
297   }
298   .list(pool)
299   .await?;
300
301   let items = create_post_items(posts, protocol_and_hostname)?;
302
303   let mut channel_builder = ChannelBuilder::default();
304   channel_builder
305     .namespaces(RSS_NAMESPACE.clone())
306     .title(&format!("{} - {}", site_view.site.name, community.name))
307     .link(community.actor_id.to_string())
308     .items(items);
309
310   if let Some(community_desc) = community.description {
311     channel_builder.description(&community_desc);
312   }
313
314   Ok(channel_builder)
315 }
316
317 #[tracing::instrument(skip_all)]
318 async fn get_feed_front(
319   pool: &mut DbPool<'_>,
320   jwt_secret: &str,
321   sort_type: &SortType,
322   limit: &i64,
323   page: &i64,
324   jwt: &str,
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?;
330
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)),
336     page: (Some(*page)),
337     ..Default::default()
338   }
339   .list(pool)
340   .await?;
341
342   let items = create_post_items(posts, protocol_and_hostname)?;
343
344   let mut channel_builder = ChannelBuilder::default();
345   channel_builder
346     .namespaces(RSS_NAMESPACE.clone())
347     .title(&format!("{} - Subscribed", site_view.site.name))
348     .link(protocol_and_hostname)
349     .items(items);
350
351   if let Some(site_desc) = site_view.site.description {
352     channel_builder.description(&site_desc);
353   }
354
355   Ok(channel_builder)
356 }
357
358 #[tracing::instrument(skip_all)]
359 async fn get_feed_inbox(
360   pool: &mut DbPool<'_>,
361   jwt_secret: &str,
362   jwt: &str,
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;
370
371   let sort = CommentSortType::New;
372
373   let replies = CommentReplyQuery {
374     recipient_id: (Some(person_id)),
375     my_person_id: (Some(person_id)),
376     show_bot_accounts: (show_bot_accounts),
377     sort: (Some(sort)),
378     limit: (Some(RSS_FETCH_LIMIT)),
379     ..Default::default()
380   }
381   .list(pool)
382   .await?;
383
384   let mentions = PersonMentionQuery {
385     recipient_id: (Some(person_id)),
386     my_person_id: (Some(person_id)),
387     show_bot_accounts: (show_bot_accounts),
388     sort: (Some(sort)),
389     limit: (Some(RSS_FETCH_LIMIT)),
390     ..Default::default()
391   }
392   .list(pool)
393   .await?;
394
395   let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
396
397   let mut channel_builder = ChannelBuilder::default();
398   channel_builder
399     .namespaces(RSS_NAMESPACE.clone())
400     .title(&format!("{} - Inbox", site_view.site.name))
401     .link(format!("{protocol_and_hostname}/inbox",))
402     .items(items);
403
404   if let Some(site_desc) = site_view.site.description {
405     channel_builder.description(&site_desc);
406   }
407
408   Ok(channel_builder)
409 }
410
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
418     .iter()
419     .map(|r| {
420       let reply_url = format!("{}/comment/{}", protocol_and_hostname, r.comment.id);
421       build_item(
422         &r.creator.name,
423         &r.comment.published,
424         &reply_url,
425         &r.comment.content,
426         protocol_and_hostname,
427       )
428     })
429     .collect::<Result<Vec<Item>, LemmyError>>()?;
430
431   let mut mention_items: Vec<Item> = mentions
432     .iter()
433     .map(|m| {
434       let mention_url = format!("{}/comment/{}", protocol_and_hostname, m.comment.id);
435       build_item(
436         &m.creator.name,
437         &m.comment.published,
438         &mention_url,
439         &m.comment.content,
440         protocol_and_hostname,
441       )
442     })
443     .collect::<Result<Vec<Item>, LemmyError>>()?;
444
445   reply_items.append(&mut mention_items);
446   Ok(reply_items)
447 }
448
449 #[tracing::instrument(skip_all)]
450 fn build_item(
451   creator_name: &str,
452   published: &NaiveDateTime,
453   url: &str,
454   content: &str,
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}");
460   i.author(format!(
461     "/u/{creator_name} <a href=\"{author_url}\">(link)</a>"
462   ));
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();
467   i.guid(guid);
468   i.link(url.to_owned());
469   // TODO add images
470   let html = markdown_to_html(content);
471   i.description(html);
472   Ok(i.build())
473 }
474
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();
481
482   for p in posts {
483     let mut i = ItemBuilder::default();
484     let mut dc_extension = DublinCoreExtensionBuilder::default();
485
486     i.title(p.post.name);
487
488     dc_extension.creators(vec![p.creator.actor_id.to_string()]);
489
490     let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
491     i.pub_date(dt.to_rfc2822());
492
493     let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
494     i.comments(post_url.clone());
495     let guid = GuidBuilder::default()
496       .permalink(true)
497       .value(&post_url)
498       .build();
499     i.guid(guid);
500
501     let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
502
503     // TODO add images
504     let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
505     p.creator.actor_id,
506     p.creator.name,
507     community_url,
508     p.community.name,
509     p.counts.score,
510     post_url,
511     p.counts.comments);
512
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());
518     } else {
519       i.link(post_url.clone());
520     }
521
522     if let Some(body) = p.post.body {
523       let html = markdown_to_html(&body);
524       description.push_str(&html);
525     }
526
527     i.description(description);
528
529     i.dublin_core_ext(dc_extension.build());
530     items.push(i.build());
531   }
532
533   Ok(items)
534 }