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