1 use crate::http::{create_apub_response, create_apub_tombstone_response};
3 base::{AnyBase, BaseExt},
4 collection::{CollectionExt, OrderedCollection, UnorderedCollection},
7 use actix_web::{body::Body, web, HttpResponse};
8 use lemmy_api_common::blocking;
10 extensions::context::lemmy_context,
11 generate_moderators_url,
15 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
16 use lemmy_db_schema::source::{activity::Activity, community::Community};
17 use lemmy_db_views_actor::{
18 community_follower_view::CommunityFollowerView,
19 community_moderator_view::CommunityModeratorView,
21 use lemmy_utils::LemmyError;
22 use lemmy_websocket::LemmyContext;
23 use serde::Deserialize;
25 #[derive(Deserialize)]
26 pub(crate) struct CommunityQuery {
27 community_name: String,
30 /// Return the ActivityPub json representation of a local community over HTTP.
31 pub(crate) async fn get_apub_community_http(
32 info: web::Path<CommunityQuery>,
33 context: web::Data<LemmyContext>,
34 ) -> Result<HttpResponse<Body>, LemmyError> {
35 let community = blocking(context.pool(), move |conn| {
36 Community::read_from_name(conn, &info.community_name)
40 if !community.deleted {
41 let apub = community.to_apub(context.pool()).await?;
43 Ok(create_apub_response(&apub))
45 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
49 /// Returns an empty followers collection, only populating the size (for privacy).
50 pub(crate) async fn get_apub_community_followers(
51 info: web::Path<CommunityQuery>,
52 context: web::Data<LemmyContext>,
53 ) -> Result<HttpResponse<Body>, LemmyError> {
54 let community = blocking(context.pool(), move |conn| {
55 Community::read_from_name(conn, &info.community_name)
59 let community_id = community.id;
60 let community_followers = blocking(context.pool(), move |conn| {
61 CommunityFollowerView::for_community(conn, community_id)
65 let mut collection = UnorderedCollection::new();
67 .set_many_contexts(lemmy_context()?)
68 .set_id(community.followers_url.into())
69 .set_total_items(community_followers.len() as u64);
70 Ok(create_apub_response(&collection))
73 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
74 /// activites like votes or comments).
75 pub(crate) async fn get_apub_community_outbox(
76 info: web::Path<CommunityQuery>,
77 context: web::Data<LemmyContext>,
78 ) -> Result<HttpResponse<Body>, LemmyError> {
79 let community = blocking(context.pool(), move |conn| {
80 Community::read_from_name(conn, &info.community_name)
84 let community_actor_id = community.actor_id.to_owned();
85 let activities = blocking(context.pool(), move |conn| {
86 Activity::read_community_outbox(conn, &community_actor_id)
90 let activities = activities
92 .map(AnyBase::from_arbitrary_json)
93 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
94 let len = activities.len();
95 let mut collection = OrderedCollection::new();
97 .set_many_items(activities)
98 .set_many_contexts(lemmy_context()?)
99 .set_id(community.get_outbox_url()?)
100 .set_total_items(len as u64);
101 Ok(create_apub_response(&collection))
104 pub(crate) async fn get_apub_community_inbox(
105 info: web::Path<CommunityQuery>,
106 context: web::Data<LemmyContext>,
107 ) -> Result<HttpResponse<Body>, LemmyError> {
108 let community = blocking(context.pool(), move |conn| {
109 Community::read_from_name(conn, &info.community_name)
113 let mut collection = OrderedCollection::new();
115 .set_id(community.inbox_url.into())
116 .set_many_contexts(lemmy_context()?);
117 Ok(create_apub_response(&collection))
120 pub(crate) async fn get_apub_community_moderators(
121 info: web::Path<CommunityQuery>,
122 context: web::Data<LemmyContext>,
123 ) -> Result<HttpResponse<Body>, LemmyError> {
124 let community = blocking(context.pool(), move |conn| {
125 Community::read_from_name(conn, &info.community_name)
129 // The attributed to, is an ordered vector with the creator actor_ids first,
130 // then the rest of the moderators
131 // TODO Technically the instance admins can mod the community, but lets
132 // ignore that for now
133 let cid = community.id;
134 let moderators = blocking(context.pool(), move |conn| {
135 CommunityModeratorView::for_community(conn, cid)
139 let moderators: Vec<Url> = moderators
141 .map(|m| m.moderator.actor_id.into_inner())
143 let mut collection = OrderedCollection::new();
145 .set_id(generate_moderators_url(&community.actor_id)?.into())
146 .set_total_items(moderators.len() as u64)
147 .set_many_items(moderators)
148 .set_many_contexts(lemmy_context()?);
149 Ok(create_apub_response(&collection))