2 extensions::context::lemmy_context,
3 http::{create_apub_response, create_apub_tombstone_response},
8 base::{AnyBase, BaseExt, ExtendsExt},
9 collection::{CollectionExt, OrderedCollection, UnorderedCollection},
11 use actix_web::{body::Body, web, HttpResponse};
12 use lemmy_db_queries::source::{community::Community_, post::Post_};
13 use lemmy_db_schema::source::{community::Community, post::Post};
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.get_followers_url()?)
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_id = community.id;
80 let posts = blocking(context.pool(), move |conn| {
81 Post::list_for_community(conn, community_id)
85 let mut pages: Vec<AnyBase> = vec![];
87 pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
90 let len = pages.len();
91 let mut collection = OrderedCollection::new();
93 .set_many_items(pages)
94 .set_many_contexts(lemmy_context()?)
95 .set_id(community.get_outbox_url()?)
96 .set_total_items(len as u64);
97 Ok(create_apub_response(&collection))
100 pub async fn get_apub_community_inbox(
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 mut collection = OrderedCollection::new();
111 .set_id(format!("{}/inbox", community.actor_id).parse()?)
112 .set_many_contexts(lemmy_context()?);
113 Ok(create_apub_response(&collection))