3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
7 context::lemmy_context,
8 generate_moderators_url,
12 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::traits::{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 #[activity_handler(LemmyContext)]
64 pub enum GroupInboxActivities {
65 FollowCommunity(FollowCommunity),
66 UndoFollowCommunity(UndoFollowCommunity),
67 AnnouncableActivities(AnnouncableActivities),
70 /// Handler for all incoming receive to community inboxes.
71 pub async fn community_inbox(
74 _path: web::Path<String>,
75 context: web::Data<LemmyContext>,
76 ) -> Result<HttpResponse, LemmyError> {
77 let unparsed = payload_to_string(payload).await?;
78 trace!("Received community inbox activity {}", unparsed);
79 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
81 receive_group_inbox(activity.clone(), request, &context).await?;
83 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
84 let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
86 AnnounceActivity::send(announcable, &community, vec![], &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 receive_activity(request, activity.clone(), context).await
100 /// Returns an empty followers collection, only populating the size (for privacy).
101 pub(crate) async fn get_apub_community_followers(
102 info: web::Path<CommunityQuery>,
103 context: web::Data<LemmyContext>,
104 ) -> Result<HttpResponse<Body>, LemmyError> {
105 let community = blocking(context.pool(), move |conn| {
106 Community::read_from_name(conn, &info.community_name)
110 let community_id = community.id;
111 let community_followers = blocking(context.pool(), move |conn| {
112 CommunityFollowerView::for_community(conn, community_id)
116 let mut collection = UnorderedCollection::new();
118 .set_many_contexts(lemmy_context())
119 .set_id(community.followers_url.into())
120 .set_total_items(community_followers.len() as u64);
121 Ok(create_apub_response(&collection))
124 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
125 /// activites like votes or comments).
126 pub(crate) async fn get_apub_community_outbox(
127 info: web::Path<CommunityQuery>,
128 context: web::Data<LemmyContext>,
129 ) -> Result<HttpResponse<Body>, LemmyError> {
130 let community = blocking(context.pool(), move |conn| {
131 Community::read_from_name(conn, &info.community_name)
135 let community_actor_id = community.actor_id.to_owned();
136 let activities = blocking(context.pool(), move |conn| {
137 Activity::read_community_outbox(conn, &community_actor_id)
141 let activities = activities
143 .map(AnyBase::from_arbitrary_json)
144 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
145 let len = activities.len();
146 let mut collection = OrderedCollection::new();
148 .set_many_items(activities)
149 .set_many_contexts(lemmy_context())
150 .set_id(generate_outbox_url(&community.actor_id)?.into())
151 .set_total_items(len as u64);
152 Ok(create_apub_response(&collection))
155 pub(crate) async fn get_apub_community_inbox(
156 info: web::Path<CommunityQuery>,
157 context: web::Data<LemmyContext>,
158 ) -> Result<HttpResponse<Body>, LemmyError> {
159 let community = blocking(context.pool(), move |conn| {
160 Community::read_from_name(conn, &info.community_name)
164 let mut collection = OrderedCollection::new();
166 .set_id(community.inbox_url.into())
167 .set_many_contexts(lemmy_context());
168 Ok(create_apub_response(&collection))
171 pub(crate) async fn get_apub_community_moderators(
172 info: web::Path<CommunityQuery>,
173 context: web::Data<LemmyContext>,
174 ) -> Result<HttpResponse<Body>, LemmyError> {
175 let community = blocking(context.pool(), move |conn| {
176 Community::read_from_name(conn, &info.community_name)
180 // The attributed to, is an ordered vector with the creator actor_ids first,
181 // then the rest of the moderators
182 // TODO Technically the instance admins can mod the community, but lets
183 // ignore that for now
184 let cid = community.id;
185 let moderators = blocking(context.pool(), move |conn| {
186 CommunityModeratorView::for_community(conn, cid)
190 let moderators: Vec<Url> = moderators
192 .map(|m| m.moderator.actor_id.into())
194 let mut collection = OrderedCollection::new();
196 .set_id(generate_moderators_url(&community.actor_id)?.into())
197 .set_total_items(moderators.len() as u64)
198 .set_many_items(moderators)
199 .set_many_contexts(lemmy_context());
200 Ok(create_apub_response(&collection))