]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Merge branch 'remove_username_lower_unique' into federation
[lemmy.git] / server / src / apub / user.rs
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};
5 use activitystreams::{
6   actor::{properties::ApActorProperties, Person},
7   context,
8   ext::Extensible,
9   object::properties::ObjectProperties,
10 };
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;
17 use failure::Error;
18 use serde::Deserialize;
19
20 #[derive(Deserialize)]
21 pub struct UserQuery {
22   user_name: String,
23 }
24
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)?;
31
32   let mut person = Person::default();
33   let oprops: &mut ObjectProperties = person.as_mut();
34   oprops
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))?;
39
40   if let Some(u) = user.updated {
41     oprops.set_updated(convert_datetime(u))?;
42   }
43
44   if let Some(i) = &user.preferred_username {
45     oprops.set_name_xsd_string(i.to_owned())?;
46   }
47
48   let mut actor_props = ApActorProperties::default();
49
50   actor_props
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))?;
55
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(),
60   };
61
62   Ok(create_apub_response(
63     &person.extend(actor_props).extend(public_key.to_ext()),
64   ))
65 }
66
67 impl UserForm {
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;
73
74     Ok(UserForm {
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(),
78       admin: false,
79       banned: false,
80       email: None,
81       avatar: None, // -> icon, image
82       updated: oprops
83         .get_updated()
84         .map(|u| u.as_ref().to_owned().naive_local()),
85       show_nsfw: false,
86       theme: "".to_string(),
87       default_sort_type: 0,
88       default_listing_type: 0,
89       lang: "".to_string(),
90       show_avatars: false,
91       send_notifications_to_email: false,
92       matrix_user_id: None,
93       actor_id: oprops.get_id().unwrap().to_string(),
94       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
95       local: false,
96       private_key: None,
97       public_key: Some(public_key.to_owned().public_key_pem),
98       last_refreshed_at: Some(naive_now()),
99     })
100   }
101 }