]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Post thumbnail and user icons federating.
[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     if let Some(avatar_url) = &self.avatar {
31       let mut image = Image::new();
32       image
33         .object_props
34         .set_url_xsd_any_uri(avatar_url.to_owned())?;
35       let any_image = AnyImage::from_concrete(image)?;
36       oprops.set_icon_any_image(any_image)?;
37     }
38
39     let mut endpoint_props = EndpointProperties::default();
40
41     endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
42
43     let mut actor_props = ApActorProperties::default();
44
45     actor_props
46       .set_inbox(self.get_inbox_url())?
47       .set_outbox(self.get_outbox_url())?
48       .set_endpoints(endpoint_props)?
49       .set_followers(self.get_followers_url())?
50       .set_following(self.get_following_url())?
51       .set_liked(self.get_liked_url())?;
52
53     Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
54   }
55   fn to_tombstone(&self) -> Result<Tombstone, Error> {
56     unimplemented!()
57   }
58 }
59
60 impl ActorType for User_ {
61   fn actor_id(&self) -> String {
62     self.actor_id.to_owned()
63   }
64
65   fn public_key(&self) -> String {
66     self.public_key.to_owned().unwrap()
67   }
68
69   fn private_key(&self) -> String {
70     self.private_key.to_owned().unwrap()
71   }
72
73   /// As a given local user, send out a follow request to a remote community.
74   fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
75     let mut follow = Follow::new();
76
77     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
78
79     follow
80       .object_props
81       .set_context_xsd_any_uri(context())?
82       .set_id(id)?;
83     follow
84       .follow_props
85       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
86       .set_object_xsd_any_uri(follow_actor_id)?;
87     let to = format!("{}/inbox", follow_actor_id);
88
89     insert_activity(&conn, self.id, &follow, true)?;
90
91     send_activity(&follow, self, vec![to])?;
92     Ok(())
93   }
94
95   fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
96     let mut follow = Follow::new();
97
98     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
99
100     follow
101       .object_props
102       .set_context_xsd_any_uri(context())?
103       .set_id(id)?;
104     follow
105       .follow_props
106       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
107       .set_object_xsd_any_uri(follow_actor_id)?;
108     let to = format!("{}/inbox", follow_actor_id);
109
110     // TODO
111     // Undo that fake activity
112     let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
113     let mut undo = Undo::default();
114
115     undo
116       .object_props
117       .set_context_xsd_any_uri(context())?
118       .set_id(undo_id)?;
119
120     undo
121       .undo_props
122       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
123       .set_object_base_box(follow)?;
124
125     insert_activity(&conn, self.id, &undo, true)?;
126
127     send_activity(&undo, self, vec![to])?;
128     Ok(())
129   }
130
131   fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
132     unimplemented!()
133   }
134
135   fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
136     unimplemented!()
137   }
138
139   fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
140     unimplemented!()
141   }
142
143   fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
144     unimplemented!()
145   }
146
147   fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
148     unimplemented!()
149   }
150
151   fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
152     unimplemented!()
153   }
154 }
155
156 impl FromApub for UserForm {
157   type ApubType = PersonExt;
158   /// Parse an ActivityPub person received from another instance into a Lemmy user.
159   fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
160     let oprops = &person.base.base.object_props;
161     let aprops = &person.base.extension;
162     let public_key: &PublicKey = &person.extension.public_key;
163
164     let avatar = match oprops.get_icon_any_image() {
165       Some(any_image) => any_image
166         .to_owned()
167         .into_concrete::<Image>()?
168         .object_props
169         .get_url_xsd_any_uri()
170         .map(|u| u.to_string()),
171       None => None,
172     };
173
174     Ok(UserForm {
175       name: oprops.get_name_xsd_string().unwrap().to_string(),
176       preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
177       password_encrypted: "".to_string(),
178       admin: false,
179       banned: false,
180       email: None,
181       avatar,
182       updated: oprops
183         .get_updated()
184         .map(|u| u.as_ref().to_owned().naive_local()),
185       show_nsfw: false,
186       theme: "".to_string(),
187       default_sort_type: 0,
188       default_listing_type: 0,
189       lang: "".to_string(),
190       show_avatars: false,
191       send_notifications_to_email: false,
192       matrix_user_id: None,
193       actor_id: oprops.get_id().unwrap().to_string(),
194       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
195       local: false,
196       private_key: None,
197       public_key: Some(public_key.to_owned().public_key_pem),
198       last_refreshed_at: Some(naive_now()),
199     })
200   }
201 }
202
203 /// Return the user json over HTTP.
204 pub async fn get_apub_user_http(
205   info: Path<UserQuery>,
206   db: DbPoolParam,
207 ) -> Result<HttpResponse<Body>, Error> {
208   let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
209   let u = user.to_apub(&db.get().unwrap())?;
210   Ok(create_apub_response(&u))
211 }