]> Untitled Git - lemmy.git/blob - lemmy_apub/src/http/community.rs
Merge branch 'main' into move_views_to_diesel
[lemmy.git] / lemmy_apub / src / http / community.rs
1 use crate::{
2   extensions::context::lemmy_context,
3   http::{create_apub_response, create_apub_tombstone_response},
4   objects::ToApub,
5   ActorType,
6 };
7 use activitystreams::{
8   base::{AnyBase, BaseExt, ExtendsExt},
9   collection::{CollectionExt, OrderedCollection, UnorderedCollection},
10 };
11 use actix_web::{body::Body, web, HttpResponse};
12 use lemmy_db::{
13   community::Community,
14   post::Post,
15   views::community_follower_view::CommunityFollowerView,
16 };
17 use lemmy_structs::blocking;
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use serde::Deserialize;
21
22 #[derive(Deserialize)]
23 pub struct CommunityQuery {
24   community_name: String,
25 }
26
27 /// Return the ActivityPub json representation of a local community over HTTP.
28 pub async fn get_apub_community_http(
29   info: web::Path<CommunityQuery>,
30   context: web::Data<LemmyContext>,
31 ) -> Result<HttpResponse<Body>, LemmyError> {
32   let community = blocking(context.pool(), move |conn| {
33     Community::read_from_name(conn, &info.community_name)
34   })
35   .await??;
36
37   if !community.deleted {
38     let apub = community.to_apub(context.pool()).await?;
39
40     Ok(create_apub_response(&apub))
41   } else {
42     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
43   }
44 }
45
46 /// Returns an empty followers collection, only populating the size (for privacy).
47 pub async fn get_apub_community_followers(
48   info: web::Path<CommunityQuery>,
49   context: web::Data<LemmyContext>,
50 ) -> Result<HttpResponse<Body>, LemmyError> {
51   let community = blocking(context.pool(), move |conn| {
52     Community::read_from_name(&conn, &info.community_name)
53   })
54   .await??;
55
56   let community_id = community.id;
57   let community_followers = blocking(context.pool(), move |conn| {
58     CommunityFollowerView::for_community(&conn, community_id)
59   })
60   .await??;
61
62   let mut collection = UnorderedCollection::new();
63   collection
64     .set_many_contexts(lemmy_context()?)
65     .set_id(community.get_followers_url()?)
66     .set_total_items(community_followers.len() as u64);
67   Ok(create_apub_response(&collection))
68 }
69
70 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
71 /// activites like votes or comments).
72 pub async fn get_apub_community_outbox(
73   info: web::Path<CommunityQuery>,
74   context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse<Body>, LemmyError> {
76   let community = blocking(context.pool(), move |conn| {
77     Community::read_from_name(&conn, &info.community_name)
78   })
79   .await??;
80
81   let community_id = community.id;
82   let posts = blocking(context.pool(), move |conn| {
83     Post::list_for_community(conn, community_id)
84   })
85   .await??;
86
87   let mut pages: Vec<AnyBase> = vec![];
88   for p in posts {
89     pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
90   }
91
92   let len = pages.len();
93   let mut collection = OrderedCollection::new();
94   collection
95     .set_many_items(pages)
96     .set_many_contexts(lemmy_context()?)
97     .set_id(community.get_outbox_url()?)
98     .set_total_items(len as u64);
99   Ok(create_apub_response(&collection))
100 }