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 Ok(HttpResponse::Ok().finish())
89 pub(in crate::http) async fn receive_group_inbox(
90 activity: GroupInboxActivities,
92 context: &LemmyContext,
93 ) -> Result<HttpResponse, LemmyError> {
94 let res = receive_activity(request, activity.clone(), context).await;
95 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity.clone() {
96 let community = extract_community(&announcable.cc(), context, &mut 0).await?;
98 AnnounceActivity::send(announcable, &community, vec![], context).await?;
104 /// Returns an empty followers collection, only populating the size (for privacy).
105 pub(crate) async fn get_apub_community_followers(
106 info: web::Path<CommunityQuery>,
107 context: web::Data<LemmyContext>,
108 ) -> Result<HttpResponse<Body>, LemmyError> {
109 let community = blocking(context.pool(), move |conn| {
110 Community::read_from_name(conn, &info.community_name)
114 let community_id = community.id;
115 let community_followers = blocking(context.pool(), move |conn| {
116 CommunityFollowerView::for_community(conn, community_id)
120 let mut collection = UnorderedCollection::new();
122 .set_many_contexts(lemmy_context())
123 .set_id(community.followers_url.into())
124 .set_total_items(community_followers.len() as u64);
125 Ok(create_apub_response(&collection))
128 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
129 /// activites like votes or comments).
130 pub(crate) async fn get_apub_community_outbox(
131 info: web::Path<CommunityQuery>,
132 context: web::Data<LemmyContext>,
133 ) -> Result<HttpResponse<Body>, LemmyError> {
134 let community = blocking(context.pool(), move |conn| {
135 Community::read_from_name(conn, &info.community_name)
138 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
139 let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
140 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
141 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
144 pub(crate) async fn get_apub_community_moderators(
145 info: web::Path<CommunityQuery>,
146 context: web::Data<LemmyContext>,
147 ) -> Result<HttpResponse<Body>, LemmyError> {
148 let community: ApubCommunity = blocking(context.pool(), move |conn| {
149 Community::read_from_name(conn, &info.community_name)
153 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
154 let outbox_data = CommunityContext(community, context.get_ref().clone());
155 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
156 Ok(create_apub_response(
157 &moderators.to_apub(&outbox_data).await?,