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 // Ignore failures in get_community(). those happen because Delete/PrivateMessage is not in a
88 // community, but looks identical to Delete/Post or Delete/Comment which are in a community.
89 let community = announcable.get_community(context, &mut 0).await;
90 if let Ok(community) = community {
92 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
93 AnnounceActivity::send(*announcable, &community, context).await?;
101 /// Returns an empty followers collection, only populating the size (for privacy).
102 pub(crate) async fn get_apub_community_followers(
103 info: web::Path<CommunityQuery>,
104 context: web::Data<LemmyContext>,
105 ) -> Result<HttpResponse, LemmyError> {
106 let community = blocking(context.pool(), move |conn| {
107 Community::read_from_name(conn, &info.community_name)
110 let followers = GroupFollowers::new(community, &context).await?;
111 Ok(create_apub_response(&followers))
114 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
115 /// activites like votes or comments).
116 pub(crate) async fn get_apub_community_outbox(
117 info: web::Path<CommunityQuery>,
118 context: web::Data<LemmyContext>,
119 ) -> Result<HttpResponse, LemmyError> {
120 let community = blocking(context.pool(), move |conn| {
121 Community::read_from_name(conn, &info.community_name)
124 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
125 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
126 let outbox: ApubCommunityOutbox = id
127 .dereference(&outbox_data, context.client(), &mut 0)
129 Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?))
132 #[tracing::instrument(skip_all)]
133 pub(crate) async fn get_apub_community_moderators(
134 info: web::Path<CommunityQuery>,
135 context: web::Data<LemmyContext>,
136 ) -> Result<HttpResponse, LemmyError> {
137 let community: ApubCommunity = blocking(context.pool(), move |conn| {
138 Community::read_from_name(conn, &info.community_name)
142 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
143 let outbox_data = CommunityContext(community, context.get_ref().clone());
144 let moderators: ApubCommunityModerators = id
145 .dereference(&outbox_data, context.client(), &mut 0)
147 Ok(create_apub_response(
148 &moderators.into_apub(&outbox_data).await?,