]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Merge pull request #1328 from LemmyNet/move_views_to_diesel
[lemmy.git] / crates / apub / src / http / community.rs
1 use crate::{
2   extensions::context::lemmy_context,
3   http::{create_apub_response, create_apub_tombstone_response},
4   objects::ToApub,
5   ActorType,
6 };
7 use activitystreams::{
8   base::{AnyBase, BaseExt, ExtendsExt},
9   collection::{CollectionExt, OrderedCollection, UnorderedCollection},
10 };
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;
19
20 #[derive(Deserialize)]
21 pub struct CommunityQuery {
22   community_name: String,
23 }
24
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)
32   })
33   .await??;
34
35   if !community.deleted {
36     let apub = community.to_apub(context.pool()).await?;
37
38     Ok(create_apub_response(&apub))
39   } else {
40     Ok(create_apub_tombstone_response(&community.to_tombstone()?))
41   }
42 }
43
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)
51   })
52   .await??;
53
54   let community_id = community.id;
55   let community_followers = blocking(context.pool(), move |conn| {
56     CommunityFollowerView::for_community(&conn, community_id)
57   })
58   .await??;
59
60   let mut collection = UnorderedCollection::new();
61   collection
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))
66 }
67
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)
76   })
77   .await??;
78
79   let community_id = community.id;
80   let posts = blocking(context.pool(), move |conn| {
81     Post::list_for_community(conn, community_id)
82   })
83   .await??;
84
85   let mut pages: Vec<AnyBase> = vec![];
86   for p in posts {
87     pages.push(p.to_apub(context.pool()).await?.into_any_base()?);
88   }
89
90   let len = pages.len();
91   let mut collection = OrderedCollection::new();
92   collection
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))
98 }
99
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)
106   })
107   .await??;
108
109   let mut collection = OrderedCollection::new();
110   collection
111     .set_id(format!("{}/inbox", community.actor_id).parse()?)
112     .set_many_contexts(lemmy_context()?);
113   Ok(create_apub_response(&collection))
114 }