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::CommunityFollowers,
24 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
25 use lemmy_api_common::blocking;
27 traits::{ActivityFields, ActorType, ApubObject},
28 verify::verify_domains_match,
30 use lemmy_db_schema::source::community::Community;
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
34 use serde::Deserialize;
36 #[derive(Deserialize)]
37 pub(crate) struct CommunityQuery {
38 community_name: String,
41 /// Return the ActivityPub json representation of a local community over HTTP.
42 pub(crate) async fn get_apub_community_http(
43 info: web::Path<CommunityQuery>,
44 context: web::Data<LemmyContext>,
45 ) -> Result<HttpResponse<Body>, LemmyError> {
46 let community: ApubCommunity = blocking(context.pool(), move |conn| {
47 Community::read_from_name(conn, &info.community_name)
52 if !community.deleted {
53 let apub = community.to_apub(&**context).await?;
55 Ok(create_apub_response(&apub))
57 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
61 /// Handler for all incoming receive to community inboxes.
62 pub async fn community_inbox(
65 _path: web::Path<String>,
66 context: web::Data<LemmyContext>,
67 ) -> Result<HttpResponse, LemmyError> {
68 let unparsed = payload_to_string(payload).await?;
69 info!("Received community inbox activity {}", unparsed);
70 let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
72 receive_group_inbox(activity.inner(), request, &context).await?;
74 Ok(HttpResponse::Ok().finish())
77 pub(in crate::http) async fn receive_group_inbox(
78 activity: GroupInboxActivities,
80 context: &LemmyContext,
81 ) -> Result<HttpResponse, LemmyError> {
82 let res = receive_activity(request, activity.clone(), context).await;
84 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
85 let community = announcable.get_community(context, &mut 0).await?;
86 let actor_id = ObjectId::new(announcable.actor().clone());
87 verify_domains_match(&community.actor_id(), announcable.id_unchecked())?;
88 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
90 AnnounceActivity::send(announcable, &community, vec![], 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<Body>, LemmyError> {
102 let community = blocking(context.pool(), move |conn| {
103 Community::read_from_name(conn, &info.community_name)
106 let followers = CommunityFollowers::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<Body>, 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)?.into_inner());
121 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
122 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
123 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
126 pub(crate) async fn get_apub_community_moderators(
127 info: web::Path<CommunityQuery>,
128 context: web::Data<LemmyContext>,
129 ) -> Result<HttpResponse<Body>, LemmyError> {
130 let community: ApubCommunity = blocking(context.pool(), move |conn| {
131 Community::read_from_name(conn, &info.community_name)
135 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
136 let outbox_data = CommunityContext(community, context.get_ref().clone());
137 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
138 Ok(create_apub_response(
139 &moderators.to_apub(&outbox_data).await?,