]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Address comments, implement delete for posts and comments
[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 endpoint_props = EndpointProperties::default();
31
32     endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
33
34     let mut actor_props = ApActorProperties::default();
35
36     actor_props
37       .set_inbox(self.get_inbox_url())?
38       .set_outbox(self.get_outbox_url())?
39       .set_endpoints(endpoint_props)?
40       .set_followers(self.get_followers_url())?
41       .set_following(self.get_following_url())?
42       .set_liked(self.get_liked_url())?;
43
44     Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
45   }
46 }
47
48 impl ActorType for User_ {
49   fn actor_id(&self) -> String {
50     self.actor_id.to_owned()
51   }
52
53   fn public_key(&self) -> String {
54     self.public_key.to_owned().unwrap()
55   }
56
57   /// As a given local user, send out a follow request to a remote community.
58   fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
59     let mut follow = Follow::new();
60
61     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
62
63     follow
64       .object_props
65       .set_context_xsd_any_uri(context())?
66       .set_id(id)?;
67     follow
68       .follow_props
69       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
70       .set_object_xsd_any_uri(follow_actor_id)?;
71     let to = format!("{}/inbox", follow_actor_id);
72
73     // Insert the sent activity into the activity table
74     let activity_form = activity::ActivityForm {
75       user_id: self.id,
76       data: serde_json::to_value(&follow)?,
77       local: true,
78       updated: None,
79     };
80     activity::Activity::create(&conn, &activity_form)?;
81
82     send_activity(
83       &follow,
84       &self.private_key.as_ref().unwrap(),
85       &follow_actor_id,
86       vec![to],
87     )?;
88     Ok(())
89   }
90
91   fn send_delete(&self, _conn: &PgConnection) -> Result<(), Error> {
92     unimplemented!()
93   }
94 }
95
96 impl FromApub for UserForm {
97   type ApubType = PersonExt;
98   /// Parse an ActivityPub person received from another instance into a Lemmy user.
99   fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
100     let oprops = &person.base.base.object_props;
101     let aprops = &person.base.extension;
102     let public_key: &PublicKey = &person.extension.public_key;
103
104     Ok(UserForm {
105       name: oprops.get_name_xsd_string().unwrap().to_string(),
106       preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
107       password_encrypted: "".to_string(),
108       admin: false,
109       banned: false,
110       email: None,
111       avatar: None, // -> icon, image
112       updated: oprops
113         .get_updated()
114         .map(|u| u.as_ref().to_owned().naive_local()),
115       show_nsfw: false,
116       theme: "".to_string(),
117       default_sort_type: 0,
118       default_listing_type: 0,
119       lang: "".to_string(),
120       show_avatars: false,
121       send_notifications_to_email: false,
122       matrix_user_id: None,
123       actor_id: oprops.get_id().unwrap().to_string(),
124       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
125       local: false,
126       private_key: None,
127       public_key: Some(public_key.to_owned().public_key_pem),
128       last_refreshed_at: Some(naive_now()),
129     })
130   }
131 }
132
133 /// Return the user json over HTTP.
134 pub async fn get_apub_user_http(
135   info: Path<UserQuery>,
136   db: DbPoolParam,
137 ) -> Result<HttpResponse<Body>, Error> {
138   let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
139   let u = user.to_apub(&db.get().unwrap())?;
140   Ok(create_apub_response(&u))
141 }