2 extensions::context::lemmy_context,
3 generate_moderators_url,
6 create_apub_tombstone_response,
7 inbox_enums::GroupInboxActivities,
14 use activitystreams::{
15 base::{AnyBase, BaseExt},
16 collection::{CollectionExt, OrderedCollection, UnorderedCollection},
19 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
20 use lemmy_api_common::blocking;
21 use lemmy_db_queries::source::{activity::Activity_, community::Community_};
22 use lemmy_db_schema::source::{activity::Activity, community::Community};
23 use lemmy_db_views_actor::{
24 community_follower_view::CommunityFollowerView,
25 community_moderator_view::CommunityModeratorView,
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
29 use serde::Deserialize;
31 #[derive(Deserialize)]
32 pub(crate) struct CommunityQuery {
33 community_name: String,
36 /// Return the ActivityPub json representation of a local community over HTTP.
37 pub(crate) async fn get_apub_community_http(
38 info: web::Path<CommunityQuery>,
39 context: web::Data<LemmyContext>,
40 ) -> Result<HttpResponse<Body>, LemmyError> {
41 let community = blocking(context.pool(), move |conn| {
42 Community::read_from_name(conn, &info.community_name)
46 if !community.deleted {
47 let apub = community.to_apub(context.pool()).await?;
49 Ok(create_apub_response(&apub))
51 Ok(create_apub_tombstone_response(&community.to_tombstone()?))
55 /// Handler for all incoming receive to community inboxes.
56 pub async fn community_inbox(
59 _path: web::Path<String>,
60 context: web::Data<LemmyContext>,
61 ) -> Result<HttpResponse, LemmyError> {
62 let unparsed = payload_to_string(payload).await?;
63 receive_activity::<GroupInboxActivities>(request, &unparsed, context).await
66 /// Returns an empty followers collection, only populating the size (for privacy).
67 pub(crate) async fn get_apub_community_followers(
68 info: web::Path<CommunityQuery>,
69 context: web::Data<LemmyContext>,
70 ) -> Result<HttpResponse<Body>, LemmyError> {
71 let community = blocking(context.pool(), move |conn| {
72 Community::read_from_name(conn, &info.community_name)
76 let community_id = community.id;
77 let community_followers = blocking(context.pool(), move |conn| {
78 CommunityFollowerView::for_community(conn, community_id)
82 let mut collection = UnorderedCollection::new();
84 .set_many_contexts(lemmy_context()?)
85 .set_id(community.followers_url.into())
86 .set_total_items(community_followers.len() as u64);
87 Ok(create_apub_response(&collection))
90 /// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
91 /// activites like votes or comments).
92 pub(crate) async fn get_apub_community_outbox(
93 info: web::Path<CommunityQuery>,
94 context: web::Data<LemmyContext>,
95 ) -> Result<HttpResponse<Body>, LemmyError> {
96 let community = blocking(context.pool(), move |conn| {
97 Community::read_from_name(conn, &info.community_name)
101 let community_actor_id = community.actor_id.to_owned();
102 let activities = blocking(context.pool(), move |conn| {
103 Activity::read_community_outbox(conn, &community_actor_id)
107 let activities = activities
109 .map(AnyBase::from_arbitrary_json)
110 .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
111 let len = activities.len();
112 let mut collection = OrderedCollection::new();
114 .set_many_items(activities)
115 .set_many_contexts(lemmy_context()?)
116 .set_id(community.get_outbox_url()?)
117 .set_total_items(len as u64);
118 Ok(create_apub_response(&collection))
121 pub(crate) async fn get_apub_community_inbox(
122 info: web::Path<CommunityQuery>,
123 context: web::Data<LemmyContext>,
124 ) -> Result<HttpResponse<Body>, LemmyError> {
125 let community = blocking(context.pool(), move |conn| {
126 Community::read_from_name(conn, &info.community_name)
130 let mut collection = OrderedCollection::new();
132 .set_id(community.inbox_url.into())
133 .set_many_contexts(lemmy_context()?);
134 Ok(create_apub_response(&collection))
137 pub(crate) async fn get_apub_community_moderators(
138 info: web::Path<CommunityQuery>,
139 context: web::Data<LemmyContext>,
140 ) -> Result<HttpResponse<Body>, LemmyError> {
141 let community = blocking(context.pool(), move |conn| {
142 Community::read_from_name(conn, &info.community_name)
146 // The attributed to, is an ordered vector with the creator actor_ids first,
147 // then the rest of the moderators
148 // TODO Technically the instance admins can mod the community, but lets
149 // ignore that for now
150 let cid = community.id;
151 let moderators = blocking(context.pool(), move |conn| {
152 CommunityModeratorView::for_community(conn, cid)
156 let moderators: Vec<Url> = moderators
158 .map(|m| m.moderator.actor_id.into_inner())
160 let mut collection = OrderedCollection::new();
162 .set_id(generate_moderators_url(&community.actor_id)?.into())
163 .set_total_items(moderators.len() as u64)
164 .set_many_items(moderators)
165 .set_many_contexts(lemmy_context()?);
166 Ok(create_apub_response(&collection))