3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
9 community_moderators::ApubCommunityModerators,
10 community_outbox::ApubCommunityOutbox,
13 context::lemmy_context,
14 fetcher::object_id::ObjectId,
18 create_apub_tombstone_response,
22 objects::community::ApubCommunity,
24 use activitystreams::{
26 collection::{CollectionExt, UnorderedCollection},
28 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
29 use lemmy_api_common::blocking;
30 use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
31 use lemmy_db_schema::source::community::Community;
32 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
33 use lemmy_utils::LemmyError;
34 use lemmy_websocket::LemmyContext;
36 use serde::{Deserialize, Serialize};
38 #[derive(Deserialize)]
39 pub(crate) struct CommunityQuery {
40 community_name: String,
43 /// Return the ActivityPub json representation of a local community over HTTP.
44 pub(crate) async fn get_apub_community_http(
45 info: web::Path<CommunityQuery>,
46 context: web::Data<LemmyContext>,
47 ) -> Result<HttpResponse<Body>, LemmyError> {
48 let community: ApubCommunity = blocking(context.pool(), move |conn| {
49 Community::read_from_name(conn, &info.community_name)
54 if !community.deleted {
55 let apub = community.to_apub(&**context).await?;
57 Ok(create_apub_response(&apub))
59 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
63 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
65 #[activity_handler(LemmyContext)]
66 pub enum GroupInboxActivities {
67 FollowCommunity(FollowCommunity),
68 UndoFollowCommunity(UndoFollowCommunity),
69 AnnouncableActivities(AnnouncableActivities),
73 /// Handler for all incoming receive to community inboxes.
74 pub async fn community_inbox(
77 _path: web::Path<String>,
78 context: web::Data<LemmyContext>,
79 ) -> Result<HttpResponse, LemmyError> {
80 let unparsed = payload_to_string(payload).await?;
81 trace!("Received community inbox activity {}", unparsed);
82 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
84 receive_group_inbox(activity.clone(), request, &context).await?;
86 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
87 let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
89 AnnounceActivity::send(announcable, &community, vec![], &context).await?;
92 Ok(HttpResponse::Ok().finish())
95 pub(in crate::http) async fn receive_group_inbox(
96 activity: GroupInboxActivities,
98 context: &LemmyContext,
99 ) -> Result<HttpResponse, LemmyError> {
100 receive_activity(request, activity.clone(), context).await
103 /// Returns an empty followers collection, only populating the size (for privacy).
104 pub(crate) async fn get_apub_community_followers(
105 info: web::Path<CommunityQuery>,
106 context: web::Data<LemmyContext>,
107 ) -> Result<HttpResponse<Body>, LemmyError> {
108 let community = blocking(context.pool(), move |conn| {
109 Community::read_from_name(conn, &info.community_name)
113 let community_id = community.id;
114 let community_followers = blocking(context.pool(), move |conn| {
115 CommunityFollowerView::for_community(conn, community_id)
119 let mut collection = UnorderedCollection::new();
121 .set_many_contexts(lemmy_context())
122 .set_id(community.followers_url.into())
123 .set_total_items(community_followers.len() as u64);
124 Ok(create_apub_response(&collection))
127 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
128 /// activites like votes or comments).
129 pub(crate) async fn get_apub_community_outbox(
130 info: web::Path<CommunityQuery>,
131 context: web::Data<LemmyContext>,
132 ) -> Result<HttpResponse<Body>, LemmyError> {
133 let community = blocking(context.pool(), move |conn| {
134 Community::read_from_name(conn, &info.community_name)
137 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
138 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
139 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
140 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
143 pub(crate) async fn get_apub_community_moderators(
144 info: web::Path<CommunityQuery>,
145 context: web::Data<LemmyContext>,
146 ) -> Result<HttpResponse<Body>, LemmyError> {
147 let community: ApubCommunity = blocking(context.pool(), move |conn| {
148 Community::read_from_name(conn, &info.community_name)
152 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
153 let outbox_data = CommunityContext(community, context.get_ref().clone());
154 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
155 Ok(create_apub_response(
156 &moderators.to_apub(&outbox_data).await?,