]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Merge pull request #663 from LemmyNet/federation_changes_1
[lemmy.git] / server / src / apub / user.rs
1 use super::*;
2
3 #[derive(Deserialize)]
4 pub struct UserQuery {
5   user_name: String,
6 }
7
8 impl ToApub for User_ {
9   type Response = PersonExt;
10
11   // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
12   fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
13     // TODO go through all these to_string and to_owned()
14     let mut person = Person::default();
15     let oprops: &mut ObjectProperties = person.as_mut();
16     oprops
17       .set_context_xsd_any_uri(context())?
18       .set_id(self.actor_id.to_string())?
19       .set_name_xsd_string(self.name.to_owned())?
20       .set_published(convert_datetime(self.published))?;
21
22     if let Some(u) = self.updated {
23       oprops.set_updated(convert_datetime(u))?;
24     }
25
26     if let Some(i) = &self.preferred_username {
27       oprops.set_name_xsd_string(i.to_owned())?;
28     }
29
30     let mut actor_props = ApActorProperties::default();
31
32     actor_props
33       .set_inbox(self.get_inbox_url())?
34       .set_outbox(self.get_outbox_url())?
35       .set_followers(self.get_followers_url())?
36       .set_following(self.get_following_url())?
37       .set_liked(self.get_liked_url())?;
38
39     Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
40   }
41 }
42
43 impl ActorType for User_ {
44   fn actor_id(&self) -> String {
45     self.actor_id.to_owned()
46   }
47
48   fn public_key(&self) -> String {
49     self.public_key.to_owned().unwrap()
50   }
51 }
52
53 impl FromApub for UserForm {
54   type ApubType = PersonExt;
55   /// Parse an ActivityPub person received from another instance into a Lemmy user.
56   fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
57     let oprops = &person.base.base.object_props;
58     let aprops = &person.base.extension;
59     let public_key: &PublicKey = &person.extension.public_key;
60
61     Ok(UserForm {
62       name: oprops.get_name_xsd_string().unwrap().to_string(),
63       preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
64       password_encrypted: "".to_string(),
65       admin: false,
66       banned: false,
67       email: None,
68       avatar: None, // -> icon, image
69       updated: oprops
70         .get_updated()
71         .map(|u| u.as_ref().to_owned().naive_local()),
72       show_nsfw: false,
73       theme: "".to_string(),
74       default_sort_type: 0,
75       default_listing_type: 0,
76       lang: "".to_string(),
77       show_avatars: false,
78       send_notifications_to_email: false,
79       matrix_user_id: None,
80       actor_id: oprops.get_id().unwrap().to_string(),
81       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
82       local: false,
83       private_key: None,
84       public_key: Some(public_key.to_owned().public_key_pem),
85       last_refreshed_at: Some(naive_now()),
86     })
87   }
88 }
89
90 /// Return the user json over HTTP.
91 pub async fn get_apub_user_http(
92   info: Path<UserQuery>,
93   db: DbPoolParam,
94 ) -> Result<HttpResponse<Body>, Error> {
95   let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
96   let u = user.to_apub(&db.get().unwrap())?;
97   Ok(create_apub_response(&u))
98 }