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