3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
7 extensions::context::lemmy_context,
8 generate_moderators_url,
11 create_apub_tombstone_response,
18 use activitystreams::{
19 base::{AnyBase, BaseExt},
20 collection::{CollectionExt, OrderedCollection, UnorderedCollection},
23 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
24 use lemmy_api_common::blocking;
25 use lemmy_apub_lib::{ActivityFields, ActivityHandler};
26 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
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 = blocking(context.pool(), move |conn| {
48 Community::read_from_name(conn, &info.community_name)
52 if !community.deleted {
53 let apub = community.to_apub(context.pool()).await?;
55 Ok(create_apub_response(&apub))
57 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
61 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
63 pub enum GroupInboxActivities {
64 FollowCommunity(FollowCommunity),
65 UndoFollowCommunity(UndoFollowCommunity),
66 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 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
83 let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
85 AnnounceActivity::send(announcable, &community, vec![], &context).await?;
88 Ok(HttpResponse::Ok().finish())
91 pub(in crate::http) async fn receive_group_inbox(
92 activity: GroupInboxActivities,
94 context: &LemmyContext,
95 ) -> Result<HttpResponse, LemmyError> {
96 receive_activity(request, activity.clone(), context).await
99 /// Returns an empty followers collection, only populating the size (for privacy).
100 pub(crate) async fn get_apub_community_followers(
101 info: web::Path<CommunityQuery>,
102 context: web::Data<LemmyContext>,
103 ) -> Result<HttpResponse<Body>, LemmyError> {
104 let community = blocking(context.pool(), move |conn| {
105 Community::read_from_name(conn, &info.community_name)
109 let community_id = community.id;
110 let community_followers = blocking(context.pool(), move |conn| {
111 CommunityFollowerView::for_community(conn, community_id)
115 let mut collection = UnorderedCollection::new();
117 .set_many_contexts(lemmy_context())
118 .set_id(community.followers_url.into())
119 .set_total_items(community_followers.len() as u64);
120 Ok(create_apub_response(&collection))
123 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
124 /// activites like votes or comments).
125 pub(crate) async fn get_apub_community_outbox(
126 info: web::Path<CommunityQuery>,
127 context: web::Data<LemmyContext>,
128 ) -> Result<HttpResponse<Body>, LemmyError> {
129 let community = blocking(context.pool(), move |conn| {
130 Community::read_from_name(conn, &info.community_name)
134 let community_actor_id = community.actor_id.to_owned();
135 let activities = blocking(context.pool(), move |conn| {
136 Activity::read_community_outbox(conn, &community_actor_id)
140 let activities = activities
142 .map(AnyBase::from_arbitrary_json)
143 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
144 let len = activities.len();
145 let mut collection = OrderedCollection::new();
147 .set_many_items(activities)
148 .set_many_contexts(lemmy_context())
149 .set_id(community.get_outbox_url()?)
150 .set_total_items(len as u64);
151 Ok(create_apub_response(&collection))
154 pub(crate) async fn get_apub_community_inbox(
155 info: web::Path<CommunityQuery>,
156 context: web::Data<LemmyContext>,
157 ) -> Result<HttpResponse<Body>, LemmyError> {
158 let community = blocking(context.pool(), move |conn| {
159 Community::read_from_name(conn, &info.community_name)
163 let mut collection = OrderedCollection::new();
165 .set_id(community.inbox_url.into())
166 .set_many_contexts(lemmy_context());
167 Ok(create_apub_response(&collection))
170 pub(crate) async fn get_apub_community_moderators(
171 info: web::Path<CommunityQuery>,
172 context: web::Data<LemmyContext>,
173 ) -> Result<HttpResponse<Body>, LemmyError> {
174 let community = blocking(context.pool(), move |conn| {
175 Community::read_from_name(conn, &info.community_name)
179 // The attributed to, is an ordered vector with the creator actor_ids first,
180 // then the rest of the moderators
181 // TODO Technically the instance admins can mod the community, but lets
182 // ignore that for now
183 let cid = community.id;
184 let moderators = blocking(context.pool(), move |conn| {
185 CommunityModeratorView::for_community(conn, cid)
189 let moderators: Vec<Url> = moderators
191 .map(|m| m.moderator.actor_id.into())
193 let mut collection = OrderedCollection::new();
195 .set_id(generate_moderators_url(&community.actor_id)?.into())
196 .set_total_items(moderators.len() as u64)
197 .set_many_items(moderators)
198 .set_many_contexts(lemmy_context());
199 Ok(create_apub_response(&collection))