]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Move object and collection structs to protocol folder
[lemmy.git] / crates / apub / src / http / community.rs
1 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
2 use log::info;
3 use serde::{Deserialize, Serialize};
4
5 use lemmy_api_common::blocking;
6 use lemmy_apub_lib::{
7   traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
8   verify::verify_domains_match,
9 };
10 use lemmy_db_schema::source::community::Community;
11 use lemmy_utils::LemmyError;
12 use lemmy_websocket::LemmyContext;
13
14 use crate::{
15   activities::{
16     community::announce::{AnnouncableActivities, AnnounceActivity, GetCommunity},
17     following::{follow::FollowCommunity, undo::UndoFollowCommunity},
18     report::Report,
19     verify_person_in_community,
20   },
21   collections::{
22     community_moderators::ApubCommunityModerators,
23     community_outbox::ApubCommunityOutbox,
24     CommunityContext,
25   },
26   context::WithContext,
27   fetcher::object_id::ObjectId,
28   generate_outbox_url,
29   http::{
30     create_apub_response,
31     create_apub_tombstone_response,
32     payload_to_string,
33     receive_activity,
34   },
35   objects::community::ApubCommunity,
36   protocol::collections::group_followers::CommunityFollowers,
37 };
38
39 #[derive(Deserialize)]
40 pub(crate) struct CommunityQuery {
41   community_name: String,
42 }
43
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)
51   })
52   .await??
53   .into();
54
55   if !community.deleted {
56     let apub = community.to_apub(&**context).await?;
57
58     Ok(create_apub_response(&apub))
59   } else {
60     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
61   }
62 }
63
64 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
65 #[serde(untagged)]
66 #[activity_handler(LemmyContext)]
67 pub enum GroupInboxActivities {
68   FollowCommunity(FollowCommunity),
69   UndoFollowCommunity(UndoFollowCommunity),
70   AnnouncableActivities(AnnouncableActivities),
71   Report(Report),
72 }
73
74 /// Handler for all incoming receive to community inboxes.
75 pub async fn community_inbox(
76   request: HttpRequest,
77   payload: Payload,
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)?;
84
85   receive_group_inbox(activity.inner(), request, &context).await?;
86
87   Ok(HttpResponse::Ok().finish())
88 }
89
90 pub(in crate::http) async fn receive_group_inbox(
91   activity: GroupInboxActivities,
92   request: HttpRequest,
93   context: &LemmyContext,
94 ) -> Result<HttpResponse, LemmyError> {
95   let res = receive_activity(request, activity.clone(), context).await;
96
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?;
102     if community.local {
103       AnnounceActivity::send(announcable, &community, vec![], context).await?;
104     }
105   }
106
107   res
108 }
109
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)
117   })
118   .await??;
119   let followers = CommunityFollowers::new(community, &context).await?;
120   Ok(create_apub_response(&followers))
121 }
122
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)
131   })
132   .await??;
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?))
137 }
138
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)
145   })
146   .await??
147   .into();
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?,
153   ))
154 }