]> Untitled Git - lemmy.git/blob - crates/apub/src/http/community.rs
Merge remote-tracking branch 'origin/main' into 1462-jwt-revocation-on-pwd-change
[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},
9   collection::{CollectionExt, OrderedCollection, UnorderedCollection},
10 };
11 use actix_web::{body::Body, web, HttpResponse};
12 use lemmy_api_structs::blocking;
13 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
14 use lemmy_db_schema::source::{activity::Activity, community::Community};
15 use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
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.followers_url.into())
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_actor_id = community.actor_id.to_owned();
80   let activities = blocking(context.pool(), move |conn| {
81     Activity::read_community_outbox(conn, &community_actor_id)
82   })
83   .await??;
84
85   let activities = activities
86     .iter()
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();
91   collection
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))
97 }
98
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)
105   })
106   .await??;
107
108   let mut collection = OrderedCollection::new();
109   collection
110     .set_id(format!("{}/inbox", community.actor_id).parse()?)
111     .set_many_contexts(lemmy_context()?);
112   Ok(create_apub_response(&collection))
113 }