1 use crate::apub::signatures::PublicKey;
2 use crate::apub::{create_apub_response, PersonExt};
3 use crate::db::user::{UserForm, User_};
4 use crate::{convert_datetime, naive_now};
6 actor::{properties::ApActorProperties, Person},
9 object::properties::ObjectProperties,
11 use actix_web::body::Body;
12 use actix_web::web::Path;
13 use actix_web::HttpResponse;
14 use actix_web::{web, Result};
15 use diesel::r2d2::{ConnectionManager, Pool};
16 use diesel::PgConnection;
18 use serde::Deserialize;
20 #[derive(Deserialize)]
21 pub struct UserQuery {
25 pub async fn get_apub_user(
26 info: Path<UserQuery>,
27 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
28 ) -> Result<HttpResponse<Body>, Error> {
29 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
31 let mut person = Person::default();
32 let oprops: &mut ObjectProperties = person.as_mut();
34 .set_context_xsd_any_uri(context())?
35 .set_id(user.actor_id.to_string())?
36 .set_name_xsd_string(user.name.to_owned())?
37 .set_published(convert_datetime(user.published))?;
39 if let Some(u) = user.updated {
40 oprops.set_updated(convert_datetime(u))?;
43 if let Some(i) = &user.preferred_username {
44 oprops.set_name_xsd_string(i.to_owned())?;
47 let mut actor_props = ApActorProperties::default();
50 .set_inbox(format!("{}/inbox", &user.actor_id))?
51 .set_outbox(format!("{}/outbox", &user.actor_id))?
52 .set_following(format!("{}/following", &user.actor_id))?
53 .set_liked(format!("{}/liked", &user.actor_id))?;
55 let public_key = PublicKey {
56 id: format!("{}#main-key", user.actor_id),
57 owner: user.actor_id.to_owned(),
58 public_key_pem: user.public_key.unwrap(),
61 Ok(create_apub_response(
62 &person.extend(actor_props).extend(public_key.to_ext()),
67 pub fn from_person(person: &PersonExt) -> Result<Self, Error> {
68 let oprops = &person.base.base.object_props;
69 let aprops = &person.base.extension;
70 let public_key: &PublicKey = &person.extension.public_key;
73 name: oprops.get_name_xsd_string().unwrap().to_string(),
74 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
75 password_encrypted: "".to_string(),
82 .map(|u| u.as_ref().to_owned().naive_local()),
84 theme: "".to_string(),
86 default_listing_type: 0,
89 send_notifications_to_email: false,
91 actor_id: oprops.get_id().unwrap().to_string(),
92 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
95 public_key: Some(public_key.to_owned().public_key_pem),
96 last_refreshed_at: Some(naive_now()),