2019-12-19 21:59:13 +00:00
|
|
|
use crate::apub::make_apub_endpoint;
|
2020-01-12 15:31:51 +00:00
|
|
|
use crate::db::establish_unpooled_connection;
|
2019-12-19 21:59:13 +00:00
|
|
|
use crate::db::user::User_;
|
|
|
|
use crate::to_datetime_utc;
|
2020-03-05 10:32:29 +00:00
|
|
|
use activitypub::{actor::Person, 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;
|
|
|
|
|
|
|
|
impl User_ {
|
|
|
|
pub fn as_person(&self) -> Person {
|
|
|
|
let base_url = make_apub_endpoint("u", &self.name);
|
|
|
|
let mut person = Person::default();
|
|
|
|
person.object_props.set_context_object(context()).ok();
|
|
|
|
person.object_props.set_id_string(base_url.to_string()).ok();
|
|
|
|
person
|
|
|
|
.object_props
|
|
|
|
.set_name_string(self.name.to_owned())
|
|
|
|
.ok();
|
|
|
|
person
|
|
|
|
.object_props
|
|
|
|
.set_published_utctime(to_datetime_utc(self.published))
|
|
|
|
.ok();
|
|
|
|
if let Some(updated) = self.updated {
|
|
|
|
person
|
|
|
|
.object_props
|
|
|
|
.set_updated_utctime(to_datetime_utc(updated))
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
person
|
|
|
|
.ap_actor_props
|
|
|
|
.set_inbox_string(format!("{}/inbox", &base_url))
|
|
|
|
.ok();
|
|
|
|
person
|
|
|
|
.ap_actor_props
|
|
|
|
.set_outbox_string(format!("{}/outbox", &base_url))
|
|
|
|
.ok();
|
|
|
|
person
|
|
|
|
.ap_actor_props
|
|
|
|
.set_following_string(format!("{}/following", &base_url))
|
|
|
|
.ok();
|
|
|
|
person
|
|
|
|
.ap_actor_props
|
|
|
|
.set_liked_string(format!("{}/liked", &base_url))
|
|
|
|
.ok();
|
|
|
|
if let Some(i) = &self.preferred_username {
|
|
|
|
person
|
|
|
|
.ap_actor_props
|
|
|
|
.set_preferred_username_string(i.to_string())
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
person
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UserQuery {
|
|
|
|
user_name: String,
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:30:45 +00:00
|
|
|
pub async fn get_apub_user(info: Path<UserQuery>) -> 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(user) = User_::find_by_email_or_username(&connection, &info.user_name) {
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/activity+json")
|
|
|
|
.body(serde_json::to_string(&user.as_person()).unwrap())
|
|
|
|
} else {
|
|
|
|
HttpResponse::NotFound().finish()
|
|
|
|
}
|
|
|
|
}
|