]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Rewrite community followers and user outbox to use our own structs
[lemmy.git] / crates / apub / src / http / community.rs
1 use crate::{
2   activities::{
3     community::announce::{AnnouncableActivities, AnnounceActivity, GetCommunity},
4     following::{follow::FollowCommunity, undo::UndoFollowCommunity},
5     report::Report,
6     verify_person_in_community,
7   },
8   collections::{
9     community_followers::CommunityFollowers,
10     community_moderators::ApubCommunityModerators,
11     community_outbox::ApubCommunityOutbox,
12     CommunityContext,
13   },
14   context::WithContext,
15   fetcher::object_id::ObjectId,
16   generate_outbox_url,
17   http::{
18     create_apub_response,
19     create_apub_tombstone_response,
20     payload_to_string,
21     receive_activity,
22   },
23   objects::community::ApubCommunity,
24 };
25 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
26 use lemmy_api_common::blocking;
27 use lemmy_apub_lib::{
28   traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
29   verify::verify_domains_match,
30 };
31 use lemmy_db_schema::source::community::Community;
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::LemmyContext;
34 use log::info;
35 use serde::{Deserialize, Serialize};
36
37 #[derive(Deserialize)]
38 pub(crate) struct CommunityQuery {
39   community_name: String,
40 }
41
42 /// Return the ActivityPub json representation of a local community over HTTP.
43 pub(crate) async fn get_apub_community_http(
44   info: web::Path<CommunityQuery>,
45   context: web::Data<LemmyContext>,
46 ) -> Result<HttpResponse<Body>, LemmyError> {
47   let community: ApubCommunity = blocking(context.pool(), move |conn| {
48     Community::read_from_name(conn, &info.community_name)
49   })
50   .await??
51   .into();
52
53   if !community.deleted {
54     let apub = community.to_apub(&**context).await?;
55
56     Ok(create_apub_response(&apub))
57   } else {
58     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
59   }
60 }
61
62 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
63 #[serde(untagged)]
64 #[activity_handler(LemmyContext)]
65 pub enum GroupInboxActivities {
66   FollowCommunity(FollowCommunity),
67   UndoFollowCommunity(UndoFollowCommunity),
68   AnnouncableActivities(AnnouncableActivities),
69   Report(Report),
70 }
71
72 /// Handler for all incoming receive to community inboxes.
73 pub async fn community_inbox(
74   request: HttpRequest,
75   payload: Payload,
76   _path: web::Path<String>,
77   context: web::Data<LemmyContext>,
78 ) -> Result<HttpResponse, LemmyError> {
79   let unparsed = payload_to_string(payload).await?;
80   info!("Received community inbox activity {}", unparsed);
81   let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
82
83   receive_group_inbox(activity.inner(), request, &context).await?;
84
85   Ok(HttpResponse::Ok().finish())
86 }
87
88 pub(in crate::http) async fn receive_group_inbox(
89   activity: GroupInboxActivities,
90   request: HttpRequest,
91   context: &LemmyContext,
92 ) -> Result<HttpResponse, LemmyError> {
93   let res = receive_activity(request, activity.clone(), context).await;
94
95   if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
96     let community = announcable.get_community(context, &mut 0).await?;
97     let actor_id = ObjectId::new(announcable.actor().clone());
98     verify_domains_match(&community.actor_id(), announcable.id_unchecked())?;
99     verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
100     if community.local {
101       AnnounceActivity::send(announcable, &community, vec![], context).await?;
102     }
103   }
104
105   res
106 }
107
108 /// Returns an empty followers collection, only populating the size (for privacy).
109 pub(crate) async fn get_apub_community_followers(
110   info: web::Path<CommunityQuery>,
111   context: web::Data<LemmyContext>,
112 ) -> Result<HttpResponse<Body>, LemmyError> {
113   let community = blocking(context.pool(), move |conn| {
114     Community::read_from_name(conn, &info.community_name)
115   })
116   .await??;
117   let followers = CommunityFollowers::new(community, &context).await?;
118   Ok(create_apub_response(&followers))
119 }
120
121 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
122 /// activites like votes or comments).
123 pub(crate) async fn get_apub_community_outbox(
124   info: web::Path<CommunityQuery>,
125   context: web::Data<LemmyContext>,
126 ) -> Result<HttpResponse<Body>, LemmyError> {
127   let community = blocking(context.pool(), move |conn| {
128     Community::read_from_name(conn, &info.community_name)
129   })
130   .await??;
131   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
132   let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
133   let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
134   Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
135 }
136
137 pub(crate) async fn get_apub_community_moderators(
138   info: web::Path<CommunityQuery>,
139   context: web::Data<LemmyContext>,
140 ) -> Result<HttpResponse<Body>, LemmyError> {
141   let community: ApubCommunity = blocking(context.pool(), move |conn| {
142     Community::read_from_name(conn, &info.community_name)
143   })
144   .await??
145   .into();
146   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
147   let outbox_data = CommunityContext(community, context.get_ref().clone());
148   let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
149   Ok(create_apub_response(
150     &moderators.to_apub(&outbox_data).await?,
151   ))
152 }