]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Merge crates db_schema and db_queries
[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_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 #[activity_handler(LemmyContext)]
64 pub enum GroupInboxActivities {
65   FollowCommunity(FollowCommunity),
66   UndoFollowCommunity(UndoFollowCommunity),
67   AnnouncableActivities(AnnouncableActivities),
68   Report(Report),
69 }
70
71 /// Handler for all incoming receive to community inboxes.
72 pub async fn community_inbox(
73   request: HttpRequest,
74   payload: Payload,
75   _path: web::Path<String>,
76   context: web::Data<LemmyContext>,
77 ) -> Result<HttpResponse, LemmyError> {
78   let unparsed = payload_to_string(payload).await?;
79   trace!("Received community inbox activity {}", unparsed);
80   let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
81
82   receive_group_inbox(activity.clone(), request, &context).await?;
83
84   if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
85     let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
86     if community.local {
87       AnnounceActivity::send(announcable, &community, vec![], &context).await?;
88     }
89   }
90   Ok(HttpResponse::Ok().finish())
91 }
92
93 pub(in crate::http) async fn receive_group_inbox(
94   activity: GroupInboxActivities,
95   request: HttpRequest,
96   context: &LemmyContext,
97 ) -> Result<HttpResponse, LemmyError> {
98   receive_activity(request, activity.clone(), context).await
99 }
100
101 /// Returns an empty followers collection, only populating the size (for privacy).
102 pub(crate) async fn get_apub_community_followers(
103   info: web::Path<CommunityQuery>,
104   context: web::Data<LemmyContext>,
105 ) -> Result<HttpResponse<Body>, LemmyError> {
106   let community = blocking(context.pool(), move |conn| {
107     Community::read_from_name(conn, &info.community_name)
108   })
109   .await??;
110
111   let community_id = community.id;
112   let community_followers = blocking(context.pool(), move |conn| {
113     CommunityFollowerView::for_community(conn, community_id)
114   })
115   .await??;
116
117   let mut collection = UnorderedCollection::new();
118   collection
119     .set_many_contexts(lemmy_context())
120     .set_id(community.followers_url.into())
121     .set_total_items(community_followers.len() as u64);
122   Ok(create_apub_response(&collection))
123 }
124
125 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
126 /// activites like votes or comments).
127 pub(crate) async fn get_apub_community_outbox(
128   info: web::Path<CommunityQuery>,
129   context: web::Data<LemmyContext>,
130 ) -> Result<HttpResponse<Body>, LemmyError> {
131   let community = blocking(context.pool(), move |conn| {
132     Community::read_from_name(conn, &info.community_name)
133   })
134   .await??;
135
136   let community_actor_id = community.actor_id.to_owned();
137   let activities = blocking(context.pool(), move |conn| {
138     Activity::read_community_outbox(conn, &community_actor_id)
139   })
140   .await??;
141
142   let activities = activities
143     .iter()
144     .map(AnyBase::from_arbitrary_json)
145     .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
146   let len = activities.len();
147   let mut collection = OrderedCollection::new();
148   collection
149     .set_many_items(activities)
150     .set_many_contexts(lemmy_context())
151     .set_id(generate_outbox_url(&community.actor_id)?.into())
152     .set_total_items(len as u64);
153   Ok(create_apub_response(&collection))
154 }
155
156 pub(crate) async fn get_apub_community_inbox(
157   info: web::Path<CommunityQuery>,
158   context: web::Data<LemmyContext>,
159 ) -> Result<HttpResponse<Body>, LemmyError> {
160   let community = blocking(context.pool(), move |conn| {
161     Community::read_from_name(conn, &info.community_name)
162   })
163   .await??;
164
165   let mut collection = OrderedCollection::new();
166   collection
167     .set_id(community.inbox_url.into())
168     .set_many_contexts(lemmy_context());
169   Ok(create_apub_response(&collection))
170 }
171
172 pub(crate) async fn get_apub_community_moderators(
173   info: web::Path<CommunityQuery>,
174   context: web::Data<LemmyContext>,
175 ) -> Result<HttpResponse<Body>, LemmyError> {
176   let community = blocking(context.pool(), move |conn| {
177     Community::read_from_name(conn, &info.community_name)
178   })
179   .await??;
180
181   // The attributed to, is an ordered vector with the creator actor_ids first,
182   // then the rest of the moderators
183   // TODO Technically the instance admins can mod the community, but lets
184   // ignore that for now
185   let cid = community.id;
186   let moderators = blocking(context.pool(), move |conn| {
187     CommunityModeratorView::for_community(conn, cid)
188   })
189   .await??;
190
191   let moderators: Vec<Url> = moderators
192     .into_iter()
193     .map(|m| m.moderator.actor_id.into())
194     .collect();
195   let mut collection = OrderedCollection::new();
196   collection
197     .set_id(generate_moderators_url(&community.actor_id)?.into())
198     .set_total_items(moderators.len() as u64)
199     .set_many_items(moderators)
200     .set_many_contexts(lemmy_context());
201   Ok(create_apub_response(&collection))
202 }