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