]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Three instance inbox bug (#1866)
[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   collections::{
9     community_moderators::ApubCommunityModerators,
10     community_outbox::ApubCommunityOutbox,
11     CommunityContext,
12   },
13   context::lemmy_context,
14   fetcher::object_id::ObjectId,
15   generate_outbox_url,
16   http::{
17     create_apub_response,
18     create_apub_tombstone_response,
19     payload_to_string,
20     receive_activity,
21   },
22   objects::community::ApubCommunity,
23 };
24 use activitystreams::{
25   base::BaseExt,
26   collection::{CollectionExt, UnorderedCollection},
27 };
28 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
29 use lemmy_api_common::blocking;
30 use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
31 use lemmy_db_schema::source::community::Community;
32 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
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: ApubCommunity = blocking(context.pool(), move |conn| {
49     Community::read_from_name(conn, &info.community_name)
50   })
51   .await??
52   .into();
53
54   if !community.deleted {
55     let apub = community.to_apub(&**context).await?;
56
57     Ok(create_apub_response(&apub))
58   } else {
59     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
60   }
61 }
62
63 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
64 #[serde(untagged)]
65 #[activity_handler(LemmyContext)]
66 pub enum GroupInboxActivities {
67   FollowCommunity(FollowCommunity),
68   UndoFollowCommunity(UndoFollowCommunity),
69   AnnouncableActivities(AnnouncableActivities),
70   Report(Report),
71 }
72
73 /// Handler for all incoming receive to community inboxes.
74 pub async fn community_inbox(
75   request: HttpRequest,
76   payload: Payload,
77   _path: web::Path<String>,
78   context: web::Data<LemmyContext>,
79 ) -> Result<HttpResponse, LemmyError> {
80   let unparsed = payload_to_string(payload).await?;
81   trace!("Received community inbox activity {}", unparsed);
82   let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
83
84   receive_group_inbox(activity.clone(), request, &context).await?;
85
86   Ok(HttpResponse::Ok().finish())
87 }
88
89 pub(in crate::http) async fn receive_group_inbox(
90   activity: GroupInboxActivities,
91   request: HttpRequest,
92   context: &LemmyContext,
93 ) -> Result<HttpResponse, LemmyError> {
94   let res = receive_activity(request, activity.clone(), context).await;
95   if let GroupInboxActivities::AnnouncableActivities(announcable) = activity.clone() {
96     let community = extract_community(&announcable.cc(), context, &mut 0).await?;
97     if community.local {
98       AnnounceActivity::send(announcable, &community, vec![], context).await?;
99     }
100   }
101   res
102 }
103
104 /// Returns an empty followers collection, only populating the size (for privacy).
105 pub(crate) async fn get_apub_community_followers(
106   info: web::Path<CommunityQuery>,
107   context: web::Data<LemmyContext>,
108 ) -> Result<HttpResponse<Body>, LemmyError> {
109   let community = blocking(context.pool(), move |conn| {
110     Community::read_from_name(conn, &info.community_name)
111   })
112   .await??;
113
114   let community_id = community.id;
115   let community_followers = blocking(context.pool(), move |conn| {
116     CommunityFollowerView::for_community(conn, community_id)
117   })
118   .await??;
119
120   let mut collection = UnorderedCollection::new();
121   collection
122     .set_many_contexts(lemmy_context())
123     .set_id(community.followers_url.into())
124     .set_total_items(community_followers.len() as u64);
125   Ok(create_apub_response(&collection))
126 }
127
128 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
129 /// activites like votes or comments).
130 pub(crate) async fn get_apub_community_outbox(
131   info: web::Path<CommunityQuery>,
132   context: web::Data<LemmyContext>,
133 ) -> Result<HttpResponse<Body>, LemmyError> {
134   let community = blocking(context.pool(), move |conn| {
135     Community::read_from_name(conn, &info.community_name)
136   })
137   .await??;
138   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
139   let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
140   let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
141   Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
142 }
143
144 pub(crate) async fn get_apub_community_moderators(
145   info: web::Path<CommunityQuery>,
146   context: web::Data<LemmyContext>,
147 ) -> Result<HttpResponse<Body>, LemmyError> {
148   let community: ApubCommunity = blocking(context.pool(), move |conn| {
149     Community::read_from_name(conn, &info.community_name)
150   })
151   .await??
152   .into();
153   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
154   let outbox_data = CommunityContext(community, context.get_ref().clone());
155   let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
156   Ok(create_apub_response(
157     &moderators.to_apub(&outbox_data).await?,
158   ))
159 }