]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Move activity structs to protocol folder
[lemmy.git] / crates / apub / src / http / community.rs
1 use crate::{
2   activities::{community::announce::GetCommunity, verify_person_in_community},
3   activity_lists::GroupInboxActivities,
4   collections::{
5     community_moderators::ApubCommunityModerators,
6     community_outbox::ApubCommunityOutbox,
7     CommunityContext,
8   },
9   context::WithContext,
10   fetcher::object_id::ObjectId,
11   generate_outbox_url,
12   http::{
13     create_apub_response,
14     create_apub_tombstone_response,
15     payload_to_string,
16     receive_activity,
17   },
18   objects::community::ApubCommunity,
19   protocol::{
20     activities::community::announce::AnnounceActivity,
21     collections::group_followers::CommunityFollowers,
22   },
23 };
24 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
25 use lemmy_api_common::blocking;
26 use lemmy_apub_lib::{
27   traits::{ActivityFields, ActorType, ApubObject},
28   verify::verify_domains_match,
29 };
30 use lemmy_db_schema::source::community::Community;
31 use lemmy_utils::LemmyError;
32 use lemmy_websocket::LemmyContext;
33 use log::info;
34 use serde::Deserialize;
35
36 #[derive(Deserialize)]
37 pub(crate) struct CommunityQuery {
38   community_name: String,
39 }
40
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)
48   })
49   .await??
50   .into();
51
52   if !community.deleted {
53     let apub = community.to_apub(&**context).await?;
54
55     Ok(create_apub_response(&apub))
56   } else {
57     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
58   }
59 }
60
61 /// Handler for all incoming receive to community inboxes.
62 pub async fn community_inbox(
63   request: HttpRequest,
64   payload: Payload,
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)?;
71
72   receive_group_inbox(activity.inner(), request, &context).await?;
73
74   Ok(HttpResponse::Ok().finish())
75 }
76
77 pub(in crate::http) async fn receive_group_inbox(
78   activity: GroupInboxActivities,
79   request: HttpRequest,
80   context: &LemmyContext,
81 ) -> Result<HttpResponse, LemmyError> {
82   let res = receive_activity(request, activity.clone(), context).await;
83
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?;
89     if community.local {
90       AnnounceActivity::send(announcable, &community, vec![], context).await?;
91     }
92   }
93
94   res
95 }
96
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)
104   })
105   .await??;
106   let followers = CommunityFollowers::new(community, &context).await?;
107   Ok(create_apub_response(&followers))
108 }
109
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)
118   })
119   .await??;
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?))
124 }
125
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)
132   })
133   .await??
134   .into();
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?,
140   ))
141 }