1 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
3 use serde::{Deserialize, Serialize};
5 use lemmy_api_common::blocking;
7 traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
8 verify::verify_domains_match,
10 use lemmy_db_schema::source::community::Community;
11 use lemmy_utils::LemmyError;
12 use lemmy_websocket::LemmyContext;
16 community::announce::{AnnouncableActivities, AnnounceActivity, GetCommunity},
17 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
19 verify_person_in_community,
22 community_moderators::ApubCommunityModerators,
23 community_outbox::ApubCommunityOutbox,
27 fetcher::object_id::ObjectId,
31 create_apub_tombstone_response,
35 objects::community::ApubCommunity,
36 protocol::collections::group_followers::CommunityFollowers,
39 #[derive(Deserialize)]
40 pub(crate) struct CommunityQuery {
41 community_name: String,
44 /// Return the ActivityPub json representation of a local community over HTTP.
45 pub(crate) async fn get_apub_community_http(
46 info: web::Path<CommunityQuery>,
47 context: web::Data<LemmyContext>,
48 ) -> Result<HttpResponse<Body>, LemmyError> {
49 let community: ApubCommunity = blocking(context.pool(), move |conn| {
50 Community::read_from_name(conn, &info.community_name)
55 if !community.deleted {
56 let apub = community.to_apub(&**context).await?;
58 Ok(create_apub_response(&apub))
60 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
64 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
66 #[activity_handler(LemmyContext)]
67 pub enum GroupInboxActivities {
68 FollowCommunity(FollowCommunity),
69 UndoFollowCommunity(UndoFollowCommunity),
70 AnnouncableActivities(AnnouncableActivities),
74 /// Handler for all incoming receive to community inboxes.
75 pub async fn community_inbox(
78 _path: web::Path<String>,
79 context: web::Data<LemmyContext>,
80 ) -> Result<HttpResponse, LemmyError> {
81 let unparsed = payload_to_string(payload).await?;
82 info!("Received community inbox activity {}", unparsed);
83 let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
85 receive_group_inbox(activity.inner(), request, &context).await?;
87 Ok(HttpResponse::Ok().finish())
90 pub(in crate::http) async fn receive_group_inbox(
91 activity: GroupInboxActivities,
93 context: &LemmyContext,
94 ) -> Result<HttpResponse, LemmyError> {
95 let res = receive_activity(request, activity.clone(), context).await;
97 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
98 let community = announcable.get_community(context, &mut 0).await?;
99 let actor_id = ObjectId::new(announcable.actor().clone());
100 verify_domains_match(&community.actor_id(), announcable.id_unchecked())?;
101 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
103 AnnounceActivity::send(announcable, &community, vec![], context).await?;
110 /// Returns an empty followers collection, only populating the size (for privacy).
111 pub(crate) async fn get_apub_community_followers(
112 info: web::Path<CommunityQuery>,
113 context: web::Data<LemmyContext>,
114 ) -> Result<HttpResponse<Body>, LemmyError> {
115 let community = blocking(context.pool(), move |conn| {
116 Community::read_from_name(conn, &info.community_name)
119 let followers = CommunityFollowers::new(community, &context).await?;
120 Ok(create_apub_response(&followers))
123 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
124 /// activites like votes or comments).
125 pub(crate) async fn get_apub_community_outbox(
126 info: web::Path<CommunityQuery>,
127 context: web::Data<LemmyContext>,
128 ) -> Result<HttpResponse<Body>, LemmyError> {
129 let community = blocking(context.pool(), move |conn| {
130 Community::read_from_name(conn, &info.community_name)
133 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
134 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
135 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
136 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
139 pub(crate) async fn get_apub_community_moderators(
140 info: web::Path<CommunityQuery>,
141 context: web::Data<LemmyContext>,
142 ) -> Result<HttpResponse<Body>, LemmyError> {
143 let community: ApubCommunity = blocking(context.pool(), move |conn| {
144 Community::read_from_name(conn, &info.community_name)
148 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
149 let outbox_data = CommunityContext(community, context.get_ref().clone());
150 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
151 Ok(create_apub_response(
152 &moderators.to_apub(&outbox_data).await?,