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 // Turn a Lemmy user into an ActivityPub person and return it as json.
26 pub async fn get_apub_user(
27 info: Path<UserQuery>,
28 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
29 ) -> Result<HttpResponse<Body>, Error> {
30 let user = User_::find_by_email_or_username(&&db.get()?, &info.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(user.actor_id.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", &user.actor_id))?
52 .set_outbox(format!("{}/outbox", &user.actor_id))?
53 .set_following(format!("{}/following", &user.actor_id))?
54 .set_liked(format!("{}/liked", &user.actor_id))?;
56 let public_key = PublicKey {
57 id: format!("{}#main-key", user.actor_id),
58 owner: user.actor_id.to_owned(),
59 public_key_pem: user.public_key.unwrap(),
62 Ok(create_apub_response(
63 &person.extend(actor_props).extend(public_key.to_ext()),
68 /// Parse an ActivityPub person received from another instance into a Lemmy user.
69 pub fn from_person(person: &PersonExt) -> Result<Self, Error> {
70 let oprops = &person.base.base.object_props;
71 let aprops = &person.base.extension;
72 let public_key: &PublicKey = &person.extension.public_key;
75 name: oprops.get_name_xsd_string().unwrap().to_string(),
76 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
77 password_encrypted: "".to_string(),
81 avatar: None, // -> icon, image
84 .map(|u| u.as_ref().to_owned().naive_local()),
86 theme: "".to_string(),
88 default_listing_type: 0,
91 send_notifications_to_email: false,
93 actor_id: oprops.get_id().unwrap().to_string(),
94 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
97 public_key: Some(public_key.to_owned().public_key_pem),
98 last_refreshed_at: Some(naive_now()),