1 use crate::apub::make_apub_endpoint;
2 use crate::db::community::Community;
3 use crate::db::community_view::CommunityFollowerView;
4 use crate::db::establish_unpooled_connection;
5 use crate::to_datetime_utc;
6 use activitypub::{actor::Group, collection::UnorderedCollection, context};
7 use actix_web::body::Body;
8 use actix_web::web::Path;
9 use actix_web::HttpResponse;
10 use serde::Deserialize;
13 pub fn as_group(&self) -> Group {
14 let base_url = make_apub_endpoint("c", &self.name);
16 let mut group = Group::default();
18 group.object_props.set_context_object(context()).ok();
19 group.object_props.set_id_string(base_url.to_string()).ok();
22 .set_name_string(self.name.to_owned())
26 .set_published_utctime(to_datetime_utc(self.published))
28 if let Some(updated) = self.updated {
31 .set_updated_utctime(to_datetime_utc(updated))
35 if let Some(description) = &self.description {
38 .set_summary_string(description.to_string())
44 .set_inbox_string(format!("{}/inbox", &base_url))
48 .set_outbox_string(format!("{}/outbox", &base_url))
52 .set_followers_string(format!("{}/followers", &base_url))
58 pub fn followers_as_collection(&self) -> UnorderedCollection {
59 let base_url = make_apub_endpoint("c", &self.name);
61 let mut collection = UnorderedCollection::default();
62 collection.object_props.set_context_object(context()).ok();
63 collection.object_props.set_id_string(base_url).ok();
65 let connection = establish_unpooled_connection();
66 //As we are an object, we validated that the community id was valid
67 let community_followers = CommunityFollowerView::for_community(&connection, self.id).unwrap();
69 let ap_followers = community_followers
71 .map(|follower| make_apub_endpoint("u", &follower.user_name))
76 .set_items_string_vec(ap_followers)
82 #[derive(Deserialize)]
83 pub struct CommunityQuery {
84 community_name: String,
87 pub async fn get_apub_community(info: Path<CommunityQuery>) -> HttpResponse<Body> {
88 let connection = establish_unpooled_connection();
90 if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
92 .content_type("application/activity+json")
93 .body(serde_json::to_string(&community.as_group()).unwrap())
95 HttpResponse::NotFound().finish()
99 pub async fn get_apub_community_followers(info: Path<CommunityQuery>) -> HttpResponse<Body> {
100 let connection = establish_unpooled_connection();
102 if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
104 .content_type("application/activity+json")
105 .body(serde_json::to_string(&community.followers_as_collection()).unwrap())
107 HttpResponse::NotFound().finish()