3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
9 community_moderators::ApubCommunityModerators, community_outbox::ApubCommunityOutbox,
12 context::lemmy_context,
13 fetcher::object_id::ObjectId,
16 create_apub_response, create_apub_tombstone_response, payload_to_string, receive_activity,
18 objects::community::ApubCommunity,
20 use activitystreams::{
22 collection::{CollectionExt, UnorderedCollection},
24 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
25 use lemmy_api_common::blocking;
26 use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
27 use lemmy_db_schema::source::community::Community;
28 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
29 use lemmy_utils::LemmyError;
30 use lemmy_websocket::LemmyContext;
32 use serde::{Deserialize, Serialize};
34 #[derive(Deserialize)]
35 pub(crate) struct CommunityQuery {
36 community_name: String,
39 /// Return the ActivityPub json representation of a local community over HTTP.
40 pub(crate) async fn get_apub_community_http(
41 info: web::Path<CommunityQuery>,
42 context: web::Data<LemmyContext>,
43 ) -> Result<HttpResponse<Body>, LemmyError> {
44 let community: ApubCommunity = blocking(context.pool(), move |conn| {
45 Community::read_from_name(conn, &info.community_name)
50 if !community.deleted {
51 let apub = community.to_apub(&**context).await?;
53 Ok(create_apub_response(&apub))
55 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
59 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
61 #[activity_handler(LemmyContext)]
62 pub enum GroupInboxActivities {
63 FollowCommunity(FollowCommunity),
64 UndoFollowCommunity(UndoFollowCommunity),
65 AnnouncableActivities(AnnouncableActivities),
69 /// Handler for all incoming receive to community inboxes.
70 pub async fn community_inbox(
73 _path: web::Path<String>,
74 context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse, LemmyError> {
76 let unparsed = payload_to_string(payload).await?;
77 trace!("Received community inbox activity {}", unparsed);
78 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
80 receive_group_inbox(activity.clone(), request, &context).await?;
82 Ok(HttpResponse::Ok().finish())
85 pub(in crate::http) async fn receive_group_inbox(
86 activity: GroupInboxActivities,
88 context: &LemmyContext,
89 ) -> Result<HttpResponse, LemmyError> {
90 let res = receive_activity(request, activity.clone(), context).await;
91 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity.clone() {
92 let community = extract_community(&announcable.cc(), context, &mut 0).await?;
94 AnnounceActivity::send(announcable, &community, vec![], context).await?;
100 /// Returns an empty followers collection, only populating the size (for privacy).
101 pub(crate) async fn get_apub_community_followers(
102 info: web::Path<CommunityQuery>,
103 context: web::Data<LemmyContext>,
104 ) -> Result<HttpResponse<Body>, LemmyError> {
105 let community = blocking(context.pool(), move |conn| {
106 Community::read_from_name(conn, &info.community_name)
110 let community_id = community.id;
111 let community_followers = blocking(context.pool(), move |conn| {
112 CommunityFollowerView::for_community(conn, community_id)
116 let mut collection = UnorderedCollection::new();
118 .set_many_contexts(lemmy_context())
119 .set_id(community.followers_url.into())
120 .set_total_items(community_followers.len() as u64);
121 Ok(create_apub_response(&collection))
124 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
125 /// activites like votes or comments).
126 pub(crate) async fn get_apub_community_outbox(
127 info: web::Path<CommunityQuery>,
128 context: web::Data<LemmyContext>,
129 ) -> Result<HttpResponse<Body>, LemmyError> {
130 let community = blocking(context.pool(), move |conn| {
131 Community::read_from_name(conn, &info.community_name)
134 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
135 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
136 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
137 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
140 pub(crate) async fn get_apub_community_moderators(
141 info: web::Path<CommunityQuery>,
142 context: web::Data<LemmyContext>,
143 ) -> Result<HttpResponse<Body>, LemmyError> {
144 let community: ApubCommunity = blocking(context.pool(), move |conn| {
145 Community::read_from_name(conn, &info.community_name)
149 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
150 let outbox_data = CommunityContext(community, context.get_ref().clone());
151 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
152 Ok(create_apub_response(
153 &moderators.to_apub(&outbox_data).await?,