]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
fcf20748a1462687119c474548e4ccc467f4680f
[lemmy.git] / crates / apub / src / http / community.rs
1 use crate::{
2   extensions::context::lemmy_context,
3   generate_moderators_url,
4   http::{create_apub_response, create_apub_tombstone_response},
5   objects::ToApub,
6   ActorType,
7 };
8 use activitystreams::{
9   base::{AnyBase, BaseExt},
10   collection::{CollectionExt, OrderedCollection, UnorderedCollection},
11   url::Url,
12 };
13 use actix_web::{body::Body, web, HttpResponse};
14 use lemmy_api_structs::blocking;
15 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
16 use lemmy_db_schema::source::{activity::Activity, community::Community};
17 use lemmy_db_views_actor::{
18   community_follower_view::CommunityFollowerView,
19   community_moderator_view::CommunityModeratorView,
20 };
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::LemmyContext;
23 use serde::Deserialize;
24
25 #[derive(Deserialize)]
26 pub(crate) struct CommunityQuery {
27   community_name: String,
28 }
29
30 /// Return the ActivityPub json representation of a local community over HTTP.
31 pub(crate) async fn get_apub_community_http(
32   info: web::Path<CommunityQuery>,
33   context: web::Data<LemmyContext>,
34 ) -> Result<HttpResponse<Body>, LemmyError> {
35   let community = blocking(context.pool(), move |conn| {
36     Community::read_from_name(conn, &info.community_name)
37   })
38   .await??;
39
40   if !community.deleted {
41     let apub = community.to_apub(context.pool()).await?;
42
43     Ok(create_apub_response(&apub))
44   } else {
45     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
46   }
47 }
48
49 /// Returns an empty followers collection, only populating the size (for privacy).
50 pub(crate) async fn get_apub_community_followers(
51   info: web::Path<CommunityQuery>,
52   context: web::Data<LemmyContext>,
53 ) -> Result<HttpResponse<Body>, LemmyError> {
54   let community = blocking(context.pool(), move |conn| {
55     Community::read_from_name(&conn, &info.community_name)
56   })
57   .await??;
58
59   let community_id = community.id;
60   let community_followers = blocking(context.pool(), move |conn| {
61     CommunityFollowerView::for_community(&conn, community_id)
62   })
63   .await??;
64
65   let mut collection = UnorderedCollection::new();
66   collection
67     .set_many_contexts(lemmy_context()?)
68     .set_id(community.followers_url.into())
69     .set_total_items(community_followers.len() as u64);
70   Ok(create_apub_response(&collection))
71 }
72
73 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
74 /// activites like votes or comments).
75 pub(crate) async fn get_apub_community_outbox(
76   info: web::Path<CommunityQuery>,
77   context: web::Data<LemmyContext>,
78 ) -> Result<HttpResponse<Body>, LemmyError> {
79   let community = blocking(context.pool(), move |conn| {
80     Community::read_from_name(&conn, &info.community_name)
81   })
82   .await??;
83
84   let community_actor_id = community.actor_id.to_owned();
85   let activities = blocking(context.pool(), move |conn| {
86     Activity::read_community_outbox(conn, &community_actor_id)
87   })
88   .await??;
89
90   let activities = activities
91     .iter()
92     .map(AnyBase::from_arbitrary_json)
93     .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
94   let len = activities.len();
95   let mut collection = OrderedCollection::new();
96   collection
97     .set_many_items(activities)
98     .set_many_contexts(lemmy_context()?)
99     .set_id(community.get_outbox_url()?)
100     .set_total_items(len as u64);
101   Ok(create_apub_response(&collection))
102 }
103
104 pub(crate) async fn get_apub_community_inbox(
105   info: web::Path<CommunityQuery>,
106   context: web::Data<LemmyContext>,
107 ) -> Result<HttpResponse<Body>, LemmyError> {
108   let community = blocking(context.pool(), move |conn| {
109     Community::read_from_name(&conn, &info.community_name)
110   })
111   .await??;
112
113   let mut collection = OrderedCollection::new();
114   collection
115     .set_id(community.inbox_url.into())
116     .set_many_contexts(lemmy_context()?);
117   Ok(create_apub_response(&collection))
118 }
119
120 pub(crate) async fn get_apub_community_moderators(
121   info: web::Path<CommunityQuery>,
122   context: web::Data<LemmyContext>,
123 ) -> Result<HttpResponse<Body>, LemmyError> {
124   let community = blocking(context.pool(), move |conn| {
125     Community::read_from_name(&conn, &info.community_name)
126   })
127   .await??;
128
129   // The attributed to, is an ordered vector with the creator actor_ids first,
130   // then the rest of the moderators
131   // TODO Technically the instance admins can mod the community, but lets
132   // ignore that for now
133   let cid = community.id;
134   let moderators = blocking(context.pool(), move |conn| {
135     CommunityModeratorView::for_community(&conn, cid)
136   })
137   .await??;
138
139   let moderators: Vec<Url> = moderators
140     .into_iter()
141     .map(|m| m.moderator.actor_id.into_inner())
142     .collect();
143   let mut collection = OrderedCollection::new();
144   collection
145     .set_id(generate_moderators_url(&community.actor_id)?.into())
146     .set_total_items(moderators.len() as u64)
147     .set_many_items(moderators)
148     .set_many_contexts(lemmy_context()?);
149   Ok(create_apub_response(&collection))
150 }