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};
15 views::community_follower_view::CommunityFollowerView,
17 use lemmy_structs::blocking;
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use serde::Deserialize;
22 #[derive(Deserialize)]
23 pub struct CommunityQuery {
24 community_name: String,
27 /// Return the ActivityPub json representation of a local community over HTTP.
28 pub async fn get_apub_community_http(
29 info: web::Path<CommunityQuery>,
30 context: web::Data<LemmyContext>,
31 ) -> Result<HttpResponse<Body>, LemmyError> {
32 let community = blocking(context.pool(), move |conn| {
33 Community::read_from_name(conn, &info.community_name)
37 if !community.deleted {
38 let apub = community.to_apub(context.pool()).await?;
40 Ok(create_apub_response(&apub))
42 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
46 /// Returns an empty followers collection, only populating the size (for privacy).
47 pub async fn get_apub_community_followers(
48 info: web::Path<CommunityQuery>,
49 context: web::Data<LemmyContext>,
50 ) -> Result<HttpResponse<Body>, LemmyError> {
51 let community = blocking(context.pool(), move |conn| {
52 Community::read_from_name(&conn, &info.community_name)
56 let community_id = community.id;
57 let community_followers = blocking(context.pool(), move |conn| {
58 CommunityFollowerView::for_community(&conn, community_id)
62 let mut collection = UnorderedCollection::new();
64 .set_many_contexts(lemmy_context()?)
65 .set_id(community.get_followers_url()?)
66 .set_total_items(community_followers.len() as u64);
67 Ok(create_apub_response(&collection))
70 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
71 /// activites like votes or comments).
72 pub async fn get_apub_community_outbox(
73 info: web::Path<CommunityQuery>,
74 context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse<Body>, LemmyError> {
76 let community = blocking(context.pool(), move |conn| {
77 Community::read_from_name(&conn, &info.community_name)
81 let community_id = community.id;
82 let posts = blocking(context.pool(), move |conn| {
83 Post::list_for_community(conn, community_id)
87 let mut pages: Vec<AnyBase> = vec![];
89 pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
92 let len = pages.len();
93 let mut collection = OrderedCollection::new();
95 .set_many_items(pages)
96 .set_many_contexts(lemmy_context()?)
97 .set_id(community.get_outbox_url()?)
98 .set_total_items(len as u64);
99 Ok(create_apub_response(&collection))