]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Merge different delete activities for better compatibility (fixes #2066) (#2073)
[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   generate_outbox_url,
11   http::{
12     create_apub_response,
13     create_apub_tombstone_response,
14     payload_to_string,
15     receive_activity,
16     ActivityCommonFields,
17   },
18   objects::community::ApubCommunity,
19   protocol::{
20     activities::community::announce::AnnounceActivity,
21     collections::group_followers::GroupFollowers,
22   },
23 };
24 use actix_web::{web, web::Payload, HttpRequest, HttpResponse};
25 use lemmy_api_common::blocking;
26 use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
27 use lemmy_db_schema::{source::community::Community, traits::ApubActor};
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::LemmyContext;
30 use serde::Deserialize;
31 use tracing::info;
32
33 #[derive(Deserialize)]
34 pub(crate) struct CommunityQuery {
35   community_name: String,
36 }
37
38 /// Return the ActivityPub json representation of a local community over HTTP.
39 #[tracing::instrument(skip_all)]
40 pub(crate) async fn get_apub_community_http(
41   info: web::Path<CommunityQuery>,
42   context: web::Data<LemmyContext>,
43 ) -> Result<HttpResponse, LemmyError> {
44   let community: ApubCommunity = blocking(context.pool(), move |conn| {
45     Community::read_from_name(conn, &info.community_name)
46   })
47   .await??
48   .into();
49
50   if !community.deleted {
51     let apub = community.into_apub(&**context).await?;
52
53     Ok(create_apub_response(&apub))
54   } else {
55     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
56   }
57 }
58
59 /// Handler for all incoming receive to community inboxes.
60 #[tracing::instrument(skip_all)]
61 pub async fn community_inbox(
62   request: HttpRequest,
63   payload: Payload,
64   _path: web::Path<String>,
65   context: web::Data<LemmyContext>,
66 ) -> Result<HttpResponse, LemmyError> {
67   let unparsed = payload_to_string(payload).await?;
68   info!("Received community inbox activity {}", unparsed);
69   let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
70   let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
71
72   receive_group_inbox(activity.inner(), activity_data, 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   activity_data: ActivityCommonFields,
80   request: HttpRequest,
81   context: &LemmyContext,
82 ) -> Result<HttpResponse, LemmyError> {
83   let actor_id = ObjectId::new(activity_data.actor.clone());
84   let res = receive_activity(request, activity.clone(), activity_data, context).await?;
85
86   if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
87     // Ignore failures in get_community(). those happen because Delete/PrivateMessage is not in a
88     // community, but looks identical to Delete/Post or Delete/Comment which are in a community.
89     let community = announcable.get_community(context, &mut 0).await;
90     if let Ok(community) = community {
91       if community.local {
92         verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
93         AnnounceActivity::send(*announcable, &community, context).await?;
94       }
95     }
96   }
97
98   Ok(res)
99 }
100
101 /// Returns an empty followers collection, only populating the size (for privacy).
102 pub(crate) async fn get_apub_community_followers(
103   info: web::Path<CommunityQuery>,
104   context: web::Data<LemmyContext>,
105 ) -> Result<HttpResponse, LemmyError> {
106   let community = blocking(context.pool(), move |conn| {
107     Community::read_from_name(conn, &info.community_name)
108   })
109   .await??;
110   let followers = GroupFollowers::new(community, &context).await?;
111   Ok(create_apub_response(&followers))
112 }
113
114 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
115 /// activites like votes or comments).
116 pub(crate) async fn get_apub_community_outbox(
117   info: web::Path<CommunityQuery>,
118   context: web::Data<LemmyContext>,
119 ) -> Result<HttpResponse, LemmyError> {
120   let community = blocking(context.pool(), move |conn| {
121     Community::read_from_name(conn, &info.community_name)
122   })
123   .await??;
124   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
125   let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
126   let outbox: ApubCommunityOutbox = id
127     .dereference(&outbox_data, context.client(), &mut 0)
128     .await?;
129   Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?))
130 }
131
132 #[tracing::instrument(skip_all)]
133 pub(crate) async fn get_apub_community_moderators(
134   info: web::Path<CommunityQuery>,
135   context: web::Data<LemmyContext>,
136 ) -> Result<HttpResponse, LemmyError> {
137   let community: ApubCommunity = blocking(context.pool(), move |conn| {
138     Community::read_from_name(conn, &info.community_name)
139   })
140   .await??
141   .into();
142   let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
143   let outbox_data = CommunityContext(community, context.get_ref().clone());
144   let moderators: ApubCommunityModerators = id
145     .dereference(&outbox_data, context.client(), &mut 0)
146     .await?;
147   Ok(create_apub_response(
148     &moderators.into_apub(&outbox_data).await?,
149   ))
150 }