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::{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, traits::ApubActor};
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::LemmyContext;
30 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 #[tracing::instrument(skip_all)]
40 pub(crate) async fn get_apub_community_http(
41 info: web::Path<CommunityQuery>,
42 context: web::Data<LemmyContext>,
43 ) -> Result<HttpResponse, 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.into_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 #[tracing::instrument(skip_all)]
61 pub async fn community_inbox(
64 _path: web::Path<String>,
65 context: web::Data<LemmyContext>,
66 ) -> Result<HttpResponse, LemmyError> {
67 let unparsed = payload_to_string(payload).await?;
68 info!("Received community inbox activity {}", unparsed);
69 let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
70 let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
72 receive_group_inbox(activity.inner(), activity_data, request, &context).await?;
74 Ok(HttpResponse::Ok().finish())
77 pub(in crate::http) async fn receive_group_inbox(
78 activity: GroupInboxActivities,
79 activity_data: ActivityCommonFields,
81 context: &LemmyContext,
82 ) -> Result<HttpResponse, LemmyError> {
83 let actor_id = ObjectId::new(activity_data.actor.clone());
84 let res = receive_activity(request, activity.clone(), activity_data, context).await?;
86 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
87 let community = announcable.get_community(context, &mut 0).await?;
88 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
90 AnnounceActivity::send(*announcable, &community, context).await?;
97 /// Returns an empty followers collection, only populating the size (for privacy).
98 pub(crate) async fn get_apub_community_followers(
99 info: web::Path<CommunityQuery>,
100 context: web::Data<LemmyContext>,
101 ) -> Result<HttpResponse, LemmyError> {
102 let community = blocking(context.pool(), move |conn| {
103 Community::read_from_name(conn, &info.community_name)
106 let followers = GroupFollowers::new(community, &context).await?;
107 Ok(create_apub_response(&followers))
110 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
111 /// activites like votes or comments).
112 pub(crate) async fn get_apub_community_outbox(
113 info: web::Path<CommunityQuery>,
114 context: web::Data<LemmyContext>,
115 ) -> Result<HttpResponse, LemmyError> {
116 let community = blocking(context.pool(), move |conn| {
117 Community::read_from_name(conn, &info.community_name)
120 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
121 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
122 let outbox: ApubCommunityOutbox = id
123 .dereference(&outbox_data, context.client(), &mut 0)
125 Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?))
128 #[tracing::instrument(skip_all)]
129 pub(crate) async fn get_apub_community_moderators(
130 info: web::Path<CommunityQuery>,
131 context: web::Data<LemmyContext>,
132 ) -> Result<HttpResponse, LemmyError> {
133 let community: ApubCommunity = blocking(context.pool(), move |conn| {
134 Community::read_from_name(conn, &info.community_name)
138 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
139 let outbox_data = CommunityContext(community, context.get_ref().clone());
140 let moderators: ApubCommunityModerators = id
141 .dereference(&outbox_data, context.client(), &mut 0)
143 Ok(create_apub_response(
144 &moderators.into_apub(&outbox_data).await?,