]> Untitled Git - lemmy.git/blob - crates/routes/src/feeds.rs
Add SendActivity trait so that api crates compile in parallel with lemmy_apub
[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::{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::{claims::Claims, error::LemmyError, utils::markdown_to_html};
24 use once_cell::sync::Lazy;
25 use rss::{
26   extension::dublincore::DublinCoreExtensionBuilder,
27   ChannelBuilder,
28   GuidBuilder,
29   Item,
30   ItemBuilder,
31 };
32 use serde::Deserialize;
33 use std::{collections::BTreeMap, str::FromStr};
34 use strum::ParseError;
35
36 const RSS_FETCH_LIMIT: i64 = 20;
37
38 #[derive(Deserialize)]
39 struct Params {
40   sort: Option<String>,
41 }
42
43 enum RequestType {
44   Community,
45   User,
46   Front,
47   Inbox,
48 }
49
50 pub fn config(cfg: &mut web::ServiceConfig) {
51   cfg
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));
55 }
56
57 static RSS_NAMESPACE: Lazy<BTreeMap<String, String>> = Lazy::new(|| {
58   let mut h = BTreeMap::new();
59   h.insert(
60     "dc".to_string(),
61     rss::extension::dublincore::NAMESPACE.to_string(),
62   );
63   h
64 });
65
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?)
73 }
74
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?)
82 }
83
84 #[tracing::instrument(skip_all)]
85 async fn get_feed_data(
86   context: &LemmyContext,
87   listing_type: ListingType,
88   sort_type: SortType,
89 ) -> Result<HttpResponse, LemmyError> {
90   let site_view = SiteView::read_local(context.pool()).await?;
91
92   let posts = PostQuery::builder()
93     .pool(context.pool())
94     .listing_type(Some(listing_type))
95     .sort(Some(sort_type))
96     .limit(Some(RSS_FETCH_LIMIT))
97     .build()
98     .list()
99     .await?;
100
101   let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
102
103   let mut channel_builder = ChannelBuilder::default();
104   channel_builder
105     .namespaces(RSS_NAMESPACE.clone())
106     .title(&format!("{} - {}", site_view.site.name, listing_type))
107     .link(context.settings().get_protocol_and_hostname())
108     .items(items);
109
110   if let Some(site_desc) = site_view.site.description {
111     channel_builder.description(&site_desc);
112   }
113
114   let rss = channel_builder.build().to_string();
115   Ok(
116     HttpResponse::Ok()
117       .content_type("application/rss+xml")
118       .body(rss),
119   )
120 }
121
122 #[tracing::instrument(skip_all)]
123 async fn get_feed(
124   req: HttpRequest,
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)?;
129
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()?;
132
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")))),
139   };
140
141   let jwt_secret = context.secret().jwt_secret.clone();
142   let protocol_and_hostname = context.settings().get_protocol_and_hostname();
143
144   let builder = match request_type {
145     RequestType::User => {
146       get_feed_user(context.pool(), &sort_type, &param, &protocol_and_hostname).await
147     }
148     RequestType::Community => {
149       get_feed_community(context.pool(), &sort_type, &param, &protocol_and_hostname).await
150     }
151     RequestType::Front => {
152       get_feed_front(
153         context.pool(),
154         &jwt_secret,
155         &sort_type,
156         &param,
157         &protocol_and_hostname,
158       )
159       .await
160     }
161     RequestType::Inbox => {
162       get_feed_inbox(context.pool(), &jwt_secret, &param, &protocol_and_hostname).await
163     }
164   }
165   .map_err(ErrorBadRequest)?;
166
167   let rss = builder.build().to_string();
168
169   Ok(
170     HttpResponse::Ok()
171       .content_type("application/rss+xml")
172       .body(rss),
173   )
174 }
175
176 fn get_sort_type(info: web::Query<Params>) -> Result<SortType, ParseError> {
177   let sort_query = info
178     .sort
179     .clone()
180     .unwrap_or_else(|| SortType::Hot.to_string());
181   SortType::from_str(&sort_query)
182 }
183
184 #[tracing::instrument(skip_all)]
185 async fn get_feed_user(
186   pool: &DbPool,
187   sort_type: &SortType,
188   user_name: &str,
189   protocol_and_hostname: &str,
190 ) -> Result<ChannelBuilder, LemmyError> {
191   let site_view = SiteView::read_local(pool).await?;
192   let person = Person::read_from_name(pool, user_name, false).await?;
193
194   let posts = PostQuery::builder()
195     .pool(pool)
196     .listing_type(Some(ListingType::All))
197     .sort(Some(*sort_type))
198     .creator_id(Some(person.id))
199     .limit(Some(RSS_FETCH_LIMIT))
200     .build()
201     .list()
202     .await?;
203
204   let items = create_post_items(posts, protocol_and_hostname)?;
205
206   let mut channel_builder = ChannelBuilder::default();
207   channel_builder
208     .namespaces(RSS_NAMESPACE.clone())
209     .title(&format!("{} - {}", site_view.site.name, person.name))
210     .link(person.actor_id.to_string())
211     .items(items);
212
213   Ok(channel_builder)
214 }
215
216 #[tracing::instrument(skip_all)]
217 async fn get_feed_community(
218   pool: &DbPool,
219   sort_type: &SortType,
220   community_name: &str,
221   protocol_and_hostname: &str,
222 ) -> Result<ChannelBuilder, LemmyError> {
223   let site_view = SiteView::read_local(pool).await?;
224   let community = Community::read_from_name(pool, community_name, false).await?;
225
226   let posts = PostQuery::builder()
227     .pool(pool)
228     .sort(Some(*sort_type))
229     .community_id(Some(community.id))
230     .limit(Some(RSS_FETCH_LIMIT))
231     .build()
232     .list()
233     .await?;
234
235   let items = create_post_items(posts, protocol_and_hostname)?;
236
237   let mut channel_builder = ChannelBuilder::default();
238   channel_builder
239     .namespaces(RSS_NAMESPACE.clone())
240     .title(&format!("{} - {}", site_view.site.name, community.name))
241     .link(community.actor_id.to_string())
242     .items(items);
243
244   if let Some(community_desc) = community.description {
245     channel_builder.description(&community_desc);
246   }
247
248   Ok(channel_builder)
249 }
250
251 #[tracing::instrument(skip_all)]
252 async fn get_feed_front(
253   pool: &DbPool,
254   jwt_secret: &str,
255   sort_type: &SortType,
256   jwt: &str,
257   protocol_and_hostname: &str,
258 ) -> Result<ChannelBuilder, LemmyError> {
259   let site_view = SiteView::read_local(pool).await?;
260   let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
261   let local_user = LocalUser::read(pool, local_user_id).await?;
262
263   let posts = PostQuery::builder()
264     .pool(pool)
265     .listing_type(Some(ListingType::Subscribed))
266     .local_user(Some(&local_user))
267     .sort(Some(*sort_type))
268     .limit(Some(RSS_FETCH_LIMIT))
269     .build()
270     .list()
271     .await?;
272
273   let items = create_post_items(posts, protocol_and_hostname)?;
274
275   let mut channel_builder = ChannelBuilder::default();
276   channel_builder
277     .namespaces(RSS_NAMESPACE.clone())
278     .title(&format!("{} - Subscribed", site_view.site.name))
279     .link(protocol_and_hostname)
280     .items(items);
281
282   if let Some(site_desc) = site_view.site.description {
283     channel_builder.description(&site_desc);
284   }
285
286   Ok(channel_builder)
287 }
288
289 #[tracing::instrument(skip_all)]
290 async fn get_feed_inbox(
291   pool: &DbPool,
292   jwt_secret: &str,
293   jwt: &str,
294   protocol_and_hostname: &str,
295 ) -> Result<ChannelBuilder, LemmyError> {
296   let site_view = SiteView::read_local(pool).await?;
297   let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
298   let local_user = LocalUser::read(pool, local_user_id).await?;
299   let person_id = local_user.person_id;
300   let show_bot_accounts = local_user.show_bot_accounts;
301
302   let sort = CommentSortType::New;
303
304   let replies = CommentReplyQuery::builder()
305     .pool(pool)
306     .recipient_id(Some(person_id))
307     .my_person_id(Some(person_id))
308     .show_bot_accounts(Some(show_bot_accounts))
309     .sort(Some(sort))
310     .limit(Some(RSS_FETCH_LIMIT))
311     .build()
312     .list()
313     .await?;
314
315   let mentions = PersonMentionQuery::builder()
316     .pool(pool)
317     .recipient_id(Some(person_id))
318     .my_person_id(Some(person_id))
319     .show_bot_accounts(Some(show_bot_accounts))
320     .sort(Some(sort))
321     .limit(Some(RSS_FETCH_LIMIT))
322     .build()
323     .list()
324     .await?;
325
326   let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;
327
328   let mut channel_builder = ChannelBuilder::default();
329   channel_builder
330     .namespaces(RSS_NAMESPACE.clone())
331     .title(&format!("{} - Inbox", site_view.site.name))
332     .link(format!("{}/inbox", protocol_and_hostname,))
333     .items(items);
334
335   if let Some(site_desc) = site_view.site.description {
336     channel_builder.description(&site_desc);
337   }
338
339   Ok(channel_builder)
340 }
341
342 #[tracing::instrument(skip_all)]
343 fn create_reply_and_mention_items(
344   replies: Vec<CommentReplyView>,
345   mentions: Vec<PersonMentionView>,
346   protocol_and_hostname: &str,
347 ) -> Result<Vec<Item>, LemmyError> {
348   let mut reply_items: Vec<Item> = replies
349     .iter()
350     .map(|r| {
351       let reply_url = format!(
352         "{}/post/{}/comment/{}",
353         protocol_and_hostname, r.post.id, r.comment.id
354       );
355       build_item(
356         &r.creator.name,
357         &r.comment.published,
358         &reply_url,
359         &r.comment.content,
360         protocol_and_hostname,
361       )
362     })
363     .collect::<Result<Vec<Item>, LemmyError>>()?;
364
365   let mut mention_items: Vec<Item> = mentions
366     .iter()
367     .map(|m| {
368       let mention_url = format!(
369         "{}/post/{}/comment/{}",
370         protocol_and_hostname, m.post.id, m.comment.id
371       );
372       build_item(
373         &m.creator.name,
374         &m.comment.published,
375         &mention_url,
376         &m.comment.content,
377         protocol_and_hostname,
378       )
379     })
380     .collect::<Result<Vec<Item>, LemmyError>>()?;
381
382   reply_items.append(&mut mention_items);
383   Ok(reply_items)
384 }
385
386 #[tracing::instrument(skip_all)]
387 fn build_item(
388   creator_name: &str,
389   published: &NaiveDateTime,
390   url: &str,
391   content: &str,
392   protocol_and_hostname: &str,
393 ) -> Result<Item, LemmyError> {
394   let mut i = ItemBuilder::default();
395   i.title(format!("Reply from {}", creator_name));
396   let author_url = format!("{}/u/{}", protocol_and_hostname, creator_name);
397   i.author(format!(
398     "/u/{} <a href=\"{}\">(link)</a>",
399     creator_name, author_url
400   ));
401   let dt = DateTime::<Utc>::from_utc(*published, Utc);
402   i.pub_date(dt.to_rfc2822());
403   i.comments(url.to_owned());
404   let guid = GuidBuilder::default().permalink(true).value(url).build();
405   i.guid(guid);
406   i.link(url.to_owned());
407   // TODO add images
408   let html = markdown_to_html(content);
409   i.description(html);
410   Ok(i.build())
411 }
412
413 #[tracing::instrument(skip_all)]
414 fn create_post_items(
415   posts: Vec<PostView>,
416   protocol_and_hostname: &str,
417 ) -> Result<Vec<Item>, LemmyError> {
418   let mut items: Vec<Item> = Vec::new();
419
420   for p in posts {
421     let mut i = ItemBuilder::default();
422     let mut dc_extension = DublinCoreExtensionBuilder::default();
423
424     i.title(p.post.name);
425
426     dc_extension.creators(vec![p.creator.actor_id.to_string()]);
427
428     let dt = DateTime::<Utc>::from_utc(p.post.published, Utc);
429     i.pub_date(dt.to_rfc2822());
430
431     let post_url = format!("{}/post/{}", protocol_and_hostname, p.post.id);
432     i.link(post_url.clone());
433     i.comments(post_url.clone());
434     let guid = GuidBuilder::default()
435       .permalink(true)
436       .value(&post_url)
437       .build();
438     i.guid(guid);
439
440     let community_url = format!("{}/c/{}", protocol_and_hostname, p.community.name);
441
442     // TODO add images
443     let mut description = format!("submitted by <a href=\"{}\">{}</a> to <a href=\"{}\">{}</a><br>{} points | <a href=\"{}\">{} comments</a>",
444     p.creator.actor_id,
445     p.creator.name,
446     community_url,
447     p.community.name,
448     p.counts.score,
449     post_url,
450     p.counts.comments);
451
452     // If its a url post, add it to the description
453     if let Some(url) = p.post.url {
454       let link_html = format!("<br><a href=\"{url}\">{url}</a>", url = url);
455       description.push_str(&link_html);
456     }
457
458     if let Some(body) = p.post.body {
459       let html = markdown_to_html(&body);
460       description.push_str(&html);
461     }
462
463     i.description(description);
464
465     i.dublin_core_ext(dc_extension.build());
466     items.push(i.build());
467   }
468
469   Ok(items)
470 }