2 activities::{community::announce::GetCommunity, verify_person_in_community},
3 activity_lists::GroupInboxActivities,
5 community_moderators::ApubCommunityModerators,
6 community_outbox::ApubCommunityOutbox,
10 fetcher::object_id::ObjectId,
14 create_apub_tombstone_response,
19 objects::community::ApubCommunity,
21 activities::community::announce::AnnounceActivity,
22 collections::group_followers::GroupFollowers,
25 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
26 use lemmy_api_common::blocking;
27 use lemmy_apub_lib::traits::ApubObject;
28 use lemmy_db_schema::source::community::Community;
29 use lemmy_utils::LemmyError;
30 use lemmy_websocket::LemmyContext;
32 use serde::Deserialize;
34 #[derive(Deserialize)]
35 pub(crate) struct CommunityQuery {
36 community_name: String,
39 /// Return the ActivityPub json representation of a local community over HTTP.
40 pub(crate) async fn get_apub_community_http(
41 info: web::Path<CommunityQuery>,
42 context: web::Data<LemmyContext>,
43 ) -> Result<HttpResponse<Body>, LemmyError> {
44 let community: ApubCommunity = blocking(context.pool(), move |conn| {
45 Community::read_from_name(conn, &info.community_name)
50 if !community.deleted {
51 let apub = community.to_apub(&**context).await?;
53 Ok(create_apub_response(&apub))
55 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
59 /// Handler for all incoming receive to community inboxes.
60 pub async fn community_inbox(
63 _path: web::Path<String>,
64 context: web::Data<LemmyContext>,
65 ) -> Result<HttpResponse, LemmyError> {
66 let unparsed = payload_to_string(payload).await?;
67 info!("Received community inbox activity {}", unparsed);
68 let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
69 let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
71 receive_group_inbox(activity.inner(), activity_data, request, &context).await?;
73 Ok(HttpResponse::Ok().finish())
76 pub(in crate::http) async fn receive_group_inbox(
77 activity: GroupInboxActivities,
78 activity_data: ActivityCommonFields,
80 context: &LemmyContext,
81 ) -> Result<HttpResponse, LemmyError> {
82 let actor_id = ObjectId::new(activity_data.actor.clone());
83 let res = receive_activity(request, activity.clone(), activity_data, context).await;
85 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
86 let community = announcable.get_community(context, &mut 0).await?;
87 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
89 AnnounceActivity::send(announcable, &community, vec![], context).await?;
96 /// Returns an empty followers collection, only populating the size (for privacy).
97 pub(crate) async fn get_apub_community_followers(
98 info: web::Path<CommunityQuery>,
99 context: web::Data<LemmyContext>,
100 ) -> Result<HttpResponse<Body>, LemmyError> {
101 let community = blocking(context.pool(), move |conn| {
102 Community::read_from_name(conn, &info.community_name)
105 let followers = GroupFollowers::new(community, &context).await?;
106 Ok(create_apub_response(&followers))
109 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
110 /// activites like votes or comments).
111 pub(crate) async fn get_apub_community_outbox(
112 info: web::Path<CommunityQuery>,
113 context: web::Data<LemmyContext>,
114 ) -> Result<HttpResponse<Body>, LemmyError> {
115 let community = blocking(context.pool(), move |conn| {
116 Community::read_from_name(conn, &info.community_name)
119 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
120 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
121 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
122 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
125 pub(crate) async fn get_apub_community_moderators(
126 info: web::Path<CommunityQuery>,
127 context: web::Data<LemmyContext>,
128 ) -> Result<HttpResponse<Body>, LemmyError> {
129 let community: ApubCommunity = blocking(context.pool(), move |conn| {
130 Community::read_from_name(conn, &info.community_name)
134 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
135 let outbox_data = CommunityContext(community, context.get_ref().clone());
136 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
137 Ok(create_apub_response(
138 &moderators.to_apub(&outbox_data).await?,