2020-10-12 14:10:09 +00:00
|
|
|
use crate::{
|
2020-11-25 17:44:49 +00:00
|
|
|
extensions::context::lemmy_context,
|
2020-10-12 14:10:09 +00:00
|
|
|
http::{create_apub_response, create_apub_tombstone_response},
|
2020-12-08 17:38:48 +00:00
|
|
|
objects::ToApub,
|
2020-10-12 14:10:09 +00:00
|
|
|
ActorType,
|
|
|
|
};
|
|
|
|
use activitystreams::{
|
2021-01-27 14:02:28 +00:00
|
|
|
base::{AnyBase, BaseExt},
|
2020-10-12 14:10:09 +00:00
|
|
|
collection::{CollectionExt, OrderedCollection, UnorderedCollection},
|
|
|
|
};
|
|
|
|
use actix_web::{body::Body, web, HttpResponse};
|
2021-01-27 14:02:28 +00:00
|
|
|
use lemmy_db_queries::source::{activity::Activity_, community::Community_};
|
|
|
|
use lemmy_db_schema::source::{activity::Activity, community::Community};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_structs::blocking;
|
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct CommunityQuery {
|
|
|
|
community_name: String,
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Return the ActivityPub json representation of a local community over HTTP.
|
2020-10-12 14:10:09 +00:00
|
|
|
pub async fn get_apub_community_http(
|
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read_from_name(conn, &info.community_name)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
if !community.deleted {
|
|
|
|
let apub = community.to_apub(context.pool()).await?;
|
|
|
|
|
|
|
|
Ok(create_apub_response(&apub))
|
|
|
|
} else {
|
|
|
|
Ok(create_apub_tombstone_response(&community.to_tombstone()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an empty followers collection, only populating the size (for privacy).
|
|
|
|
pub async fn get_apub_community_followers(
|
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read_from_name(&conn, &info.community_name)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = community.id;
|
|
|
|
let community_followers = blocking(context.pool(), move |conn| {
|
|
|
|
CommunityFollowerView::for_community(&conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let mut collection = UnorderedCollection::new();
|
|
|
|
collection
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_id(community.followers_url.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_total_items(community_followers.len() as u64);
|
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
|
|
|
|
/// activites like votes or comments).
|
2020-10-12 14:10:09 +00:00
|
|
|
pub async fn get_apub_community_outbox(
|
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read_from_name(&conn, &info.community_name)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
let community_actor_id = community.actor_id.to_owned();
|
|
|
|
let activities = blocking(context.pool(), move |conn| {
|
|
|
|
Activity::read_community_outbox(conn, &community_actor_id)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
let activities = activities
|
|
|
|
.iter()
|
|
|
|
.map(AnyBase::from_arbitrary_json)
|
|
|
|
.collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
|
|
|
|
let len = activities.len();
|
2020-10-12 14:10:09 +00:00
|
|
|
let mut collection = OrderedCollection::new();
|
|
|
|
collection
|
2021-01-27 14:02:28 +00:00
|
|
|
.set_many_items(activities)
|
2020-11-25 17:44:49 +00:00
|
|
|
.set_many_contexts(lemmy_context()?)
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_id(community.get_outbox_url()?)
|
|
|
|
.set_total_items(len as u64);
|
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|
2020-12-16 20:07:48 +00:00
|
|
|
|
|
|
|
pub async fn get_apub_community_inbox(
|
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read_from_name(&conn, &info.community_name)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let mut collection = OrderedCollection::new();
|
|
|
|
collection
|
|
|
|
.set_id(format!("{}/inbox", community.actor_id).parse()?)
|
|
|
|
.set_many_contexts(lemmy_context()?);
|
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|