3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
8 context::lemmy_context,
9 generate_moderators_url,
13 create_apub_tombstone_response,
17 objects::community::ApubCommunity,
19 use activitystreams::{
20 base::{AnyBase, BaseExt},
21 collection::{CollectionExt, OrderedCollection, 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, ToApub};
27 use lemmy_db_schema::source::{activity::Activity, community::Community};
28 use lemmy_db_views_actor::{
29 community_follower_view::CommunityFollowerView,
30 community_moderator_view::CommunityModeratorView,
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.pool()).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 trace!("Received community inbox activity {}", unparsed);
81 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
83 receive_group_inbox(activity.clone(), request, &context).await?;
85 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
86 let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
88 AnnounceActivity::send(announcable, &community, vec![], &context).await?;
91 Ok(HttpResponse::Ok().finish())
94 pub(in crate::http) async fn receive_group_inbox(
95 activity: GroupInboxActivities,
97 context: &LemmyContext,
98 ) -> Result<HttpResponse, LemmyError> {
99 receive_activity(request, activity.clone(), context).await
102 /// Returns an empty followers collection, only populating the size (for privacy).
103 pub(crate) async fn get_apub_community_followers(
104 info: web::Path<CommunityQuery>,
105 context: web::Data<LemmyContext>,
106 ) -> Result<HttpResponse<Body>, LemmyError> {
107 let community = blocking(context.pool(), move |conn| {
108 Community::read_from_name(conn, &info.community_name)
112 let community_id = community.id;
113 let community_followers = blocking(context.pool(), move |conn| {
114 CommunityFollowerView::for_community(conn, community_id)
118 let mut collection = UnorderedCollection::new();
120 .set_many_contexts(lemmy_context())
121 .set_id(community.followers_url.into())
122 .set_total_items(community_followers.len() as u64);
123 Ok(create_apub_response(&collection))
126 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
127 /// activites like votes or comments).
128 pub(crate) async fn get_apub_community_outbox(
129 info: web::Path<CommunityQuery>,
130 context: web::Data<LemmyContext>,
131 ) -> Result<HttpResponse<Body>, LemmyError> {
132 let community = blocking(context.pool(), move |conn| {
133 Community::read_from_name(conn, &info.community_name)
137 let community_actor_id = community.actor_id.to_owned();
138 let activities = blocking(context.pool(), move |conn| {
139 Activity::read_community_outbox(conn, &community_actor_id)
143 let activities = activities
145 .map(AnyBase::from_arbitrary_json)
146 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
147 let len = activities.len();
148 let mut collection = OrderedCollection::new();
150 .set_many_items(activities)
151 .set_many_contexts(lemmy_context())
152 .set_id(generate_outbox_url(&community.actor_id)?.into())
153 .set_total_items(len as u64);
154 Ok(create_apub_response(&collection))
157 pub(crate) async fn get_apub_community_inbox(
158 info: web::Path<CommunityQuery>,
159 context: web::Data<LemmyContext>,
160 ) -> Result<HttpResponse<Body>, LemmyError> {
161 let community = blocking(context.pool(), move |conn| {
162 Community::read_from_name(conn, &info.community_name)
166 let mut collection = OrderedCollection::new();
168 .set_id(community.inbox_url.into())
169 .set_many_contexts(lemmy_context());
170 Ok(create_apub_response(&collection))
173 pub(crate) async fn get_apub_community_moderators(
174 info: web::Path<CommunityQuery>,
175 context: web::Data<LemmyContext>,
176 ) -> Result<HttpResponse<Body>, LemmyError> {
177 let community: ApubCommunity = blocking(context.pool(), move |conn| {
178 Community::read_from_name(conn, &info.community_name)
183 // The attributed to, is an ordered vector with the creator actor_ids first,
184 // then the rest of the moderators
185 // TODO Technically the instance admins can mod the community, but lets
186 // ignore that for now
187 let cid = community.id;
188 let moderators = blocking(context.pool(), move |conn| {
189 CommunityModeratorView::for_community(conn, cid)
193 let moderators: Vec<Url> = moderators
195 .map(|m| m.moderator.actor_id.into())
197 let mut collection = OrderedCollection::new();
199 .set_id(generate_moderators_url(&community.actor_id)?.into())
200 .set_total_items(moderators.len() as u64)
201 .set_many_items(moderators)
202 .set_many_contexts(lemmy_context());
203 Ok(create_apub_response(&collection))