3 community::announce::{AnnouncableActivities, AnnounceActivity, GetCommunity},
4 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
6 verify_person_in_community,
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;
31 traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
32 verify::verify_domains_match,
34 use lemmy_db_schema::source::community::Community;
35 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::LemmyContext;
39 use serde::{Deserialize, Serialize};
41 #[derive(Deserialize)]
42 pub(crate) struct CommunityQuery {
43 community_name: String,
46 /// Return the ActivityPub json representation of a local community over HTTP.
47 pub(crate) async fn get_apub_community_http(
48 info: web::Path<CommunityQuery>,
49 context: web::Data<LemmyContext>,
50 ) -> Result<HttpResponse<Body>, LemmyError> {
51 let community: ApubCommunity = blocking(context.pool(), move |conn| {
52 Community::read_from_name(conn, &info.community_name)
57 if !community.deleted {
58 let apub = community.to_apub(&**context).await?;
60 Ok(create_apub_response(&apub))
62 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
66 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
68 #[activity_handler(LemmyContext)]
69 pub enum GroupInboxActivities {
70 FollowCommunity(FollowCommunity),
71 UndoFollowCommunity(UndoFollowCommunity),
72 AnnouncableActivities(AnnouncableActivities),
76 /// Handler for all incoming receive to community inboxes.
77 pub async fn community_inbox(
80 _path: web::Path<String>,
81 context: web::Data<LemmyContext>,
82 ) -> Result<HttpResponse, LemmyError> {
83 let unparsed = payload_to_string(payload).await?;
84 info!("Received community inbox activity {}", unparsed);
85 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
87 receive_group_inbox(activity.clone(), request, &context).await?;
89 Ok(HttpResponse::Ok().finish())
92 pub(in crate::http) async fn receive_group_inbox(
93 activity: GroupInboxActivities,
95 context: &LemmyContext,
96 ) -> Result<HttpResponse, LemmyError> {
97 let res = receive_activity(request, activity.clone(), context).await;
99 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
100 let community = announcable.get_community(context, &mut 0).await?;
101 let actor_id = ObjectId::new(announcable.actor().clone());
102 verify_domains_match(&community.actor_id(), announcable.id_unchecked())?;
103 verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
105 AnnounceActivity::send(announcable, &community, vec![], context).await?;
112 /// Returns an empty followers collection, only populating the size (for privacy).
113 pub(crate) async fn get_apub_community_followers(
114 info: web::Path<CommunityQuery>,
115 context: web::Data<LemmyContext>,
116 ) -> Result<HttpResponse<Body>, LemmyError> {
117 let community = blocking(context.pool(), move |conn| {
118 Community::read_from_name(conn, &info.community_name)
122 let community_id = community.id;
123 let community_followers = blocking(context.pool(), move |conn| {
124 CommunityFollowerView::for_community(conn, community_id)
128 let mut collection = UnorderedCollection::new();
130 .set_many_contexts(lemmy_context())
131 .set_id(community.followers_url.into())
132 .set_total_items(community_followers.len() as u64);
133 Ok(create_apub_response(&collection))
136 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
137 /// activites like votes or comments).
138 pub(crate) async fn get_apub_community_outbox(
139 info: web::Path<CommunityQuery>,
140 context: web::Data<LemmyContext>,
141 ) -> Result<HttpResponse<Body>, LemmyError> {
142 let community = blocking(context.pool(), move |conn| {
143 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.into(), context.get_ref().clone());
148 let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
149 Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
152 pub(crate) async fn get_apub_community_moderators(
153 info: web::Path<CommunityQuery>,
154 context: web::Data<LemmyContext>,
155 ) -> Result<HttpResponse<Body>, LemmyError> {
156 let community: ApubCommunity = blocking(context.pool(), move |conn| {
157 Community::read_from_name(conn, &info.community_name)
161 let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
162 let outbox_data = CommunityContext(community, context.get_ref().clone());
163 let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
164 Ok(create_apub_response(
165 &moderators.to_apub(&outbox_data).await?,