3 community::announce::{AnnouncableActivities, AnnounceActivity, GetCommunity},
4 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
6 verify_person_in_community,
9 community_followers::CommunityFollowers,
10 community_moderators::ApubCommunityModerators,
11 community_outbox::ApubCommunityOutbox,
15 fetcher::object_id::ObjectId,
19 create_apub_tombstone_response,
23 objects::community::ApubCommunity,
25 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
26 use lemmy_api_common::blocking;
28 traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
29 verify::verify_domains_match,
31 use lemmy_db_schema::source::community::Community;
32 use lemmy_utils::LemmyError;
33 use lemmy_websocket::LemmyContext;
35 use serde::{Deserialize, Serialize};
37 #[derive(Deserialize)]
38 pub(crate) struct CommunityQuery {
39 community_name: String,
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)
53 if !community.deleted {
54 let apub = community.to_apub(&**context).await?;
56 Ok(create_apub_response(&apub))
58 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
62 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
64 #[activity_handler(LemmyContext)]
65 pub enum GroupInboxActivities {
66 FollowCommunity(FollowCommunity),
67 UndoFollowCommunity(UndoFollowCommunity),
68 AnnouncableActivities(AnnouncableActivities),
72 /// Handler for all incoming receive to community inboxes.
73 pub async fn community_inbox(
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)?;
83 receive_group_inbox(activity.inner(), request, &context).await?;
85 Ok(HttpResponse::Ok().finish())
88 pub(in crate::http) async fn receive_group_inbox(
89 activity: GroupInboxActivities,
91 context: &LemmyContext,
92 ) -> Result<HttpResponse, LemmyError> {
93 let res = receive_activity(request, activity.clone(), context).await;
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?;
101 AnnounceActivity::send(announcable, &community, vec![], context).await?;
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)
117 let followers = CommunityFollowers::new(community, &context).await?;
118 Ok(create_apub_response(&followers))
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)
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?))
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)
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?,