3 community::announce::{AnnouncableActivities, AnnounceActivity},
5 following::{follow::FollowCommunity, undo::UndoFollowCommunity},
8 context::lemmy_context,
9 generate_moderators_url,
13 create_apub_tombstone_response,
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};
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),
71 /// Handler for all incoming receive to community inboxes.
72 pub async fn community_inbox(
75 _path: web::Path<String>,
76 context: web::Data<LemmyContext>,
77 ) -> Result<HttpResponse, LemmyError> {
78 let unparsed = payload_to_string(payload).await?;
79 trace!("Received community inbox activity {}", unparsed);
80 let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
82 receive_group_inbox(activity.clone(), request, &context).await?;
84 if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
85 let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
87 AnnounceActivity::send(announcable, &community, vec![], &context).await?;
90 Ok(HttpResponse::Ok().finish())
93 pub(in crate::http) async fn receive_group_inbox(
94 activity: GroupInboxActivities,
96 context: &LemmyContext,
97 ) -> Result<HttpResponse, LemmyError> {
98 receive_activity(request, activity.clone(), context).await
101 /// Returns an empty followers collection, only populating the size (for privacy).
102 pub(crate) async fn get_apub_community_followers(
103 info: web::Path<CommunityQuery>,
104 context: web::Data<LemmyContext>,
105 ) -> Result<HttpResponse<Body>, LemmyError> {
106 let community = blocking(context.pool(), move |conn| {
107 Community::read_from_name(conn, &info.community_name)
111 let community_id = community.id;
112 let community_followers = blocking(context.pool(), move |conn| {
113 CommunityFollowerView::for_community(conn, community_id)
117 let mut collection = UnorderedCollection::new();
119 .set_many_contexts(lemmy_context())
120 .set_id(community.followers_url.into())
121 .set_total_items(community_followers.len() as u64);
122 Ok(create_apub_response(&collection))
125 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
126 /// activites like votes or comments).
127 pub(crate) async fn get_apub_community_outbox(
128 info: web::Path<CommunityQuery>,
129 context: web::Data<LemmyContext>,
130 ) -> Result<HttpResponse<Body>, LemmyError> {
131 let community = blocking(context.pool(), move |conn| {
132 Community::read_from_name(conn, &info.community_name)
136 let community_actor_id = community.actor_id.to_owned();
137 let activities = blocking(context.pool(), move |conn| {
138 Activity::read_community_outbox(conn, &community_actor_id)
142 let activities = activities
144 .map(AnyBase::from_arbitrary_json)
145 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
146 let len = activities.len();
147 let mut collection = OrderedCollection::new();
149 .set_many_items(activities)
150 .set_many_contexts(lemmy_context())
151 .set_id(generate_outbox_url(&community.actor_id)?.into())
152 .set_total_items(len as u64);
153 Ok(create_apub_response(&collection))
156 pub(crate) async fn get_apub_community_inbox(
157 info: web::Path<CommunityQuery>,
158 context: web::Data<LemmyContext>,
159 ) -> Result<HttpResponse<Body>, LemmyError> {
160 let community = blocking(context.pool(), move |conn| {
161 Community::read_from_name(conn, &info.community_name)
165 let mut collection = OrderedCollection::new();
167 .set_id(community.inbox_url.into())
168 .set_many_contexts(lemmy_context());
169 Ok(create_apub_response(&collection))
172 pub(crate) async fn get_apub_community_moderators(
173 info: web::Path<CommunityQuery>,
174 context: web::Data<LemmyContext>,
175 ) -> Result<HttpResponse<Body>, LemmyError> {
176 let community = blocking(context.pool(), move |conn| {
177 Community::read_from_name(conn, &info.community_name)
181 // The attributed to, is an ordered vector with the creator actor_ids first,
182 // then the rest of the moderators
183 // TODO Technically the instance admins can mod the community, but lets
184 // ignore that for now
185 let cid = community.id;
186 let moderators = blocking(context.pool(), move |conn| {
187 CommunityModeratorView::for_community(conn, cid)
191 let moderators: Vec<Url> = moderators
193 .map(|m| m.moderator.actor_id.into())
195 let mut collection = OrderedCollection::new();
197 .set_id(generate_moderators_url(&community.actor_id)?.into())
198 .set_total_items(moderators.len() as u64)
199 .set_many_items(moderators)
200 .set_many_contexts(lemmy_context());
201 Ok(create_apub_response(&collection))