2020-03-11 11:29:10 +00:00
|
|
|
use crate::apub::group_wrapper::GroupHelper;
|
2019-12-19 21:59:13 +00:00
|
|
|
use crate::apub::make_apub_endpoint;
|
|
|
|
use crate::db::community::Community;
|
|
|
|
use crate::db::community_view::CommunityFollowerView;
|
2020-01-12 15:31:51 +00:00
|
|
|
use crate::db::establish_unpooled_connection;
|
2020-03-05 10:32:29 +00:00
|
|
|
use activitypub::{actor::Group, collection::UnorderedCollection, context};
|
2019-12-19 21:59:13 +00:00
|
|
|
use actix_web::body::Body;
|
|
|
|
use actix_web::web::Path;
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
use serde::Deserialize;
|
2020-03-11 11:29:10 +00:00
|
|
|
use serde_json::{Value};
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
impl Community {
|
|
|
|
pub fn as_group(&self) -> Group {
|
2020-03-11 17:26:58 +00:00
|
|
|
let base_url = make_apub_endpoint("c", &self.id);
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
let mut group = Group::default();
|
|
|
|
|
|
|
|
group.object_props.set_context_object(context()).ok();
|
2020-03-11 17:26:58 +00:00
|
|
|
Group::set_id(&mut group, &base_url);
|
|
|
|
|
2020-03-11 11:29:10 +00:00
|
|
|
Group::set_title(&mut group, &self.title);
|
|
|
|
Group::set_published(&mut group, self.published);
|
|
|
|
Group::set_updated(&mut group, self.updated);
|
2020-03-11 17:26:58 +00:00
|
|
|
Group::set_creator_id(&mut group, make_apub_endpoint("u", &self.creator_id));
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-03-11 11:29:10 +00:00
|
|
|
Group::set_description(&mut group, &self.description);
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-03-11 11:29:10 +00:00
|
|
|
group.ap_actor_props.inbox = Value::String(format!("{}/inbox", &base_url));
|
|
|
|
group.ap_actor_props.outbox = Value::String(format!("{}/outbox", &base_url));
|
|
|
|
group.ap_actor_props.followers = Some(Value::String(format!("{}/followers", &base_url)));
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
group
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn followers_as_collection(&self) -> UnorderedCollection {
|
|
|
|
let base_url = make_apub_endpoint("c", &self.name);
|
|
|
|
|
|
|
|
let mut collection = UnorderedCollection::default();
|
|
|
|
collection.object_props.set_context_object(context()).ok();
|
2020-01-02 11:30:00 +00:00
|
|
|
collection.object_props.set_id_string(base_url).ok();
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2020-01-12 15:31:51 +00:00
|
|
|
let connection = establish_unpooled_connection();
|
2019-12-19 21:59:13 +00:00
|
|
|
//As we are an object, we validated that the community id was valid
|
|
|
|
let community_followers = CommunityFollowerView::for_community(&connection, self.id).unwrap();
|
|
|
|
|
|
|
|
collection
|
|
|
|
.collection_props
|
2020-02-29 17:38:47 +00:00
|
|
|
.set_total_items_u64(community_followers.len() as u64)
|
2019-12-19 21:59:13 +00:00
|
|
|
.unwrap();
|
|
|
|
collection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct CommunityQuery {
|
|
|
|
community_name: String,
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:30:45 +00:00
|
|
|
pub async fn get_apub_community(info: Path<CommunityQuery>) -> HttpResponse<Body> {
|
2020-01-12 15:31:51 +00:00
|
|
|
let connection = establish_unpooled_connection();
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/activity+json")
|
|
|
|
.body(serde_json::to_string(&community.as_group()).unwrap())
|
|
|
|
} else {
|
|
|
|
HttpResponse::NotFound().finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:30:45 +00:00
|
|
|
pub async fn get_apub_community_followers(info: Path<CommunityQuery>) -> HttpResponse<Body> {
|
2020-01-12 15:31:51 +00:00
|
|
|
let connection = establish_unpooled_connection();
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/activity+json")
|
|
|
|
.body(serde_json::to_string(&community.followers_as_collection()).unwrap())
|
|
|
|
} else {
|
|
|
|
HttpResponse::NotFound().finish()
|
|
|
|
}
|
|
|
|
}
|