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,
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::traits::{ActivityFields, 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.to_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 = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
69 receive_group_inbox(activity.inner(), request, &context).await?;
71 Ok(HttpResponse::Ok().finish())
74 pub(in crate::http) async fn receive_group_inbox(
75 activity: GroupInboxActivities,
77 context: &LemmyContext,
78 ) -> Result<HttpResponse, LemmyError> {
79 let res = receive_activity(request, activity.clone(), context).await;
81 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
82 let community = announcable.get_community(context, &mut 0).await?;
83 let actor_id = ObjectId::new(announcable.actor().clone());
84 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
86 AnnounceActivity::send(announcable, &community, vec![], context).await?;
93 /// Returns an empty followers collection, only populating the size (for privacy).
94 pub(crate) async fn get_apub_community_followers(
95 info: web::Path<CommunityQuery>,
96 context: web::Data<LemmyContext>,
97 ) -> Result<HttpResponse<Body>, LemmyError> {
98 let community = blocking(context.pool(), move |conn| {
99 Community::read_from_name(conn, &info.community_name)
102 let followers = GroupFollowers::new(community, &context).await?;
103 Ok(create_apub_response(&followers))
106 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
107 /// activites like votes or comments).
108 pub(crate) async fn get_apub_community_outbox(
109 info: web::Path<CommunityQuery>,
110 context: web::Data<LemmyContext>,
111 ) -> Result<HttpResponse<Body>, LemmyError> {
112 let community = blocking(context.pool(), move |conn| {
113 Community::read_from_name(conn, &info.community_name)
116 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
117 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
118 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
119 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
122 pub(crate) async fn get_apub_community_moderators(
123 info: web::Path<CommunityQuery>,
124 context: web::Data<LemmyContext>,
125 ) -> Result<HttpResponse<Body>, LemmyError> {
126 let community: ApubCommunity = blocking(context.pool(), move |conn| {
127 Community::read_from_name(conn, &info.community_name)
131 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
132 let outbox_data = CommunityContext(community, context.get_ref().clone());
133 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
134 Ok(create_apub_response(
135 &moderators.to_apub(&outbox_data).await?,