1 use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType, PersonExt};
2 use crate::db::user::{UserForm, User_};
3 use crate::{convert_datetime, naive_now};
5 actor::{properties::ApActorProperties, Person},
8 object::properties::ObjectProperties,
10 use actix_web::body::Body;
11 use actix_web::web::Path;
12 use actix_web::HttpResponse;
13 use actix_web::{web, Result};
14 use diesel::r2d2::{ConnectionManager, Pool};
15 use diesel::PgConnection;
17 use serde::Deserialize;
19 #[derive(Deserialize)]
20 pub struct UserQuery {
24 pub async fn get_apub_user(
25 info: Path<UserQuery>,
26 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
27 ) -> Result<HttpResponse<Body>, Error> {
28 dbg!(&info.user_name);
29 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
30 let base_url = make_apub_endpoint(EndpointType::User, &user.name);
32 let mut person = Person::default();
33 let oprops: &mut ObjectProperties = person.as_mut();
35 .set_context_xsd_any_uri(context())?
36 .set_id(base_url.to_string())?
37 .set_name_xsd_string(user.name.to_owned())?
38 .set_published(convert_datetime(user.published))?;
40 if let Some(u) = user.updated {
41 oprops.set_updated(convert_datetime(u))?;
44 if let Some(i) = &user.preferred_username {
45 oprops.set_name_xsd_string(i.to_owned())?;
48 let mut actor_props = ApActorProperties::default();
51 .set_inbox(format!("{}/inbox", &base_url))?
52 .set_outbox(format!("{}/outbox", &base_url))?
53 .set_following(format!("{}/following", &base_url))?
54 .set_liked(format!("{}/liked", &base_url))?;
56 Ok(create_apub_response(&person.extend(actor_props)))
60 pub fn from_person(person: &PersonExt) -> Result<Self, Error> {
61 let oprops = &person.base.object_props;
62 let aprops = &person.extension;
64 name: oprops.get_name_xsd_string().unwrap().to_string(),
65 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
66 password_encrypted: "".to_string(),
73 .map(|u| u.as_ref().to_owned().naive_local()),
75 theme: "".to_string(),
77 default_listing_type: 0,
80 send_notifications_to_email: false,
82 actor_id: oprops.get_id().unwrap().to_string(),
83 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
87 last_refreshed_at: Some(naive_now()),