2019-12-19 21:59:13 +00:00
|
|
|
use crate::apub::make_apub_endpoint;
|
2020-03-12 00:01:25 +00:00
|
|
|
use crate::convert_datetime;
|
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_;
|
2020-03-12 00:01:25 +00:00
|
|
|
use activitystreams::{actor::apub::Person, context, object::properties::ObjectProperties};
|
2019-12-19 21:59:13 +00:00
|
|
|
use actix_web::body::Body;
|
|
|
|
use actix_web::web::Path;
|
|
|
|
use actix_web::HttpResponse;
|
2020-03-12 00:01:25 +00:00
|
|
|
use failure::Error;
|
2019-12-19 21:59:13 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
impl User_ {
|
2020-03-12 00:01:25 +00:00
|
|
|
pub fn as_person(&self) -> Result<Person, Error> {
|
2019-12-19 21:59:13 +00:00
|
|
|
let base_url = make_apub_endpoint("u", &self.name);
|
2020-03-12 00:01:25 +00:00
|
|
|
|
2019-12-19 21:59:13 +00:00
|
|
|
let mut person = Person::default();
|
2020-03-12 00:01:25 +00:00
|
|
|
let oprops: &mut ObjectProperties = person.as_mut();
|
|
|
|
oprops
|
|
|
|
.set_context_xsd_any_uri(context())?
|
|
|
|
.set_id(base_url.to_string())?
|
|
|
|
.set_published(convert_datetime(self.published))?;
|
|
|
|
|
|
|
|
if let Some(u) = self.updated {
|
|
|
|
oprops.set_updated(convert_datetime(u))?;
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(i) = &self.preferred_username {
|
2020-03-12 00:01:25 +00:00
|
|
|
oprops.set_name_xsd_string(i.to_owned())?;
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
person
|
2020-03-12 00:01:25 +00:00
|
|
|
.ap_actor_props
|
|
|
|
.set_inbox(format!("{}/inbox", &base_url))?
|
|
|
|
.set_outbox(format!("{}/outbox", &base_url))?
|
|
|
|
.set_following(format!("{}/following", &base_url))?
|
|
|
|
.set_liked(format!("{}/liked", &base_url))?;
|
|
|
|
|
|
|
|
Ok(person)
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UserQuery {
|
|
|
|
user_name: String,
|
|
|
|
}
|
|
|
|
|
2020-03-12 00:01:25 +00:00
|
|
|
pub async fn get_apub_user(info: Path<UserQuery>) -> Result<HttpResponse<Body>, Error> {
|
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) {
|
2020-03-12 00:01:25 +00:00
|
|
|
Ok(
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("application/activity+json")
|
|
|
|
.body(serde_json::to_string(&user.as_person()?).unwrap()),
|
|
|
|
)
|
2019-12-19 21:59:13 +00:00
|
|
|
} else {
|
2020-03-12 00:01:25 +00:00
|
|
|
Ok(HttpResponse::NotFound().finish())
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
|
|
|
}
|