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