2 extensions::context::lemmy_context,
3 http::{create_apub_response, create_apub_tombstone_response},
8 base::{AnyBase, BaseExt},
9 collection::{CollectionExt, OrderedCollection, UnorderedCollection},
11 use actix_web::{body::Body, web, HttpResponse};
12 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
13 use lemmy_db_schema::source::{activity::Activity, community::Community};
14 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
15 use lemmy_structs::blocking;
16 use lemmy_utils::LemmyError;
17 use lemmy_websocket::LemmyContext;
18 use serde::Deserialize;
20 #[derive(Deserialize)]
21 pub struct CommunityQuery {
22 community_name: String,
25 /// Return the ActivityPub json representation of a local community over HTTP.
26 pub async fn get_apub_community_http(
27 info: web::Path<CommunityQuery>,
28 context: web::Data<LemmyContext>,
29 ) -> Result<HttpResponse<Body>, LemmyError> {
30 let community = blocking(context.pool(), move |conn| {
31 Community::read_from_name(conn, &info.community_name)
35 if !community.deleted {
36 let apub = community.to_apub(context.pool()).await?;
38 Ok(create_apub_response(&apub))
40 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
44 /// Returns an empty followers collection, only populating the size (for privacy).
45 pub async fn get_apub_community_followers(
46 info: web::Path<CommunityQuery>,
47 context: web::Data<LemmyContext>,
48 ) -> Result<HttpResponse<Body>, LemmyError> {
49 let community = blocking(context.pool(), move |conn| {
50 Community::read_from_name(&conn, &info.community_name)
54 let community_id = community.id;
55 let community_followers = blocking(context.pool(), move |conn| {
56 CommunityFollowerView::for_community(&conn, community_id)
60 let mut collection = UnorderedCollection::new();
62 .set_many_contexts(lemmy_context()?)
63 .set_id(community.followers_url.into())
64 .set_total_items(community_followers.len() as u64);
65 Ok(create_apub_response(&collection))
68 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
69 /// activites like votes or comments).
70 pub async fn get_apub_community_outbox(
71 info: web::Path<CommunityQuery>,
72 context: web::Data<LemmyContext>,
73 ) -> Result<HttpResponse<Body>, LemmyError> {
74 let community = blocking(context.pool(), move |conn| {
75 Community::read_from_name(&conn, &info.community_name)
79 let community_actor_id = community.actor_id.to_owned();
80 let activities = blocking(context.pool(), move |conn| {
81 Activity::read_community_outbox(conn, &community_actor_id)
85 let activities = activities
87 .map(AnyBase::from_arbitrary_json)
88 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
89 let len = activities.len();
90 let mut collection = OrderedCollection::new();
92 .set_many_items(activities)
93 .set_many_contexts(lemmy_context()?)
94 .set_id(community.get_outbox_url()?)
95 .set_total_items(len as u64);
96 Ok(create_apub_response(&collection))
99 pub async fn get_apub_community_inbox(
100 info: web::Path<CommunityQuery>,
101 context: web::Data<LemmyContext>,
102 ) -> Result<HttpResponse<Body>, LemmyError> {
103 let community = blocking(context.pool(), move |conn| {
104 Community::read_from_name(&conn, &info.community_name)
108 let mut collection = OrderedCollection::new();
110 .set_id(format!("{}/inbox", community.actor_id).parse()?)
111 .set_many_contexts(lemmy_context()?);
112 Ok(create_apub_response(&collection))