]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
migrate tombstone
[lemmy.git] / server / src / apub / user.rs
1 use crate::{
2   apub::{
3     activities::send_activity,
4     create_apub_response,
5     extensions::signatures::PublicKey,
6     ActorType,
7     FromApub,
8     PersonExt,
9     ToApub,
10   },
11   convert_datetime,
12   db::{
13     activity::insert_activity,
14     user::{UserForm, User_},
15   },
16   naive_now,
17   routes::DbPoolParam,
18 };
19 use activitystreams::{
20   activity::{Follow, Undo},
21   actor::{properties::ApActorProperties, Person},
22   context,
23   endpoint::EndpointProperties,
24   object::{properties::ObjectProperties, AnyImage, Image},
25 };
26 use activitystreams_ext::Ext2;
27 use activitystreams_new::object::Tombstone;
28 use actix_web::{body::Body, web::Path, HttpResponse, Result};
29 use diesel::PgConnection;
30 use failure::Error;
31 use serde::Deserialize;
32
33 #[derive(Deserialize)]
34 pub struct UserQuery {
35   user_name: String,
36 }
37
38 impl ToApub for User_ {
39   type Response = PersonExt;
40
41   // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
42   fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
43     // TODO go through all these to_string and to_owned()
44     let mut person = Person::default();
45     let oprops: &mut ObjectProperties = person.as_mut();
46     oprops
47       .set_context_xsd_any_uri(context())?
48       .set_id(self.actor_id.to_string())?
49       .set_name_xsd_string(self.name.to_owned())?
50       .set_published(convert_datetime(self.published))?;
51
52     if let Some(u) = self.updated {
53       oprops.set_updated(convert_datetime(u))?;
54     }
55
56     if let Some(i) = &self.preferred_username {
57       oprops.set_name_xsd_string(i.to_owned())?;
58     }
59
60     if let Some(avatar_url) = &self.avatar {
61       let mut image = Image::new();
62       image
63         .object_props
64         .set_url_xsd_any_uri(avatar_url.to_owned())?;
65       let any_image = AnyImage::from_concrete(image)?;
66       oprops.set_icon_any_image(any_image)?;
67     }
68
69     let mut endpoint_props = EndpointProperties::default();
70
71     endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
72
73     let mut actor_props = ApActorProperties::default();
74
75     actor_props
76       .set_inbox(self.get_inbox_url())?
77       .set_outbox(self.get_outbox_url())?
78       .set_endpoints(endpoint_props)?
79       .set_followers(self.get_followers_url())?
80       .set_following(self.get_following_url())?
81       .set_liked(self.get_liked_url())?;
82
83     Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
84   }
85   fn to_tombstone(&self) -> Result<Tombstone, Error> {
86     unimplemented!()
87   }
88 }
89
90 impl ActorType for User_ {
91   fn actor_id(&self) -> String {
92     self.actor_id.to_owned()
93   }
94
95   fn public_key(&self) -> String {
96     self.public_key.to_owned().unwrap()
97   }
98
99   fn private_key(&self) -> String {
100     self.private_key.to_owned().unwrap()
101   }
102
103   /// As a given local user, send out a follow request to a remote community.
104   fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
105     let mut follow = Follow::new();
106
107     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
108
109     follow
110       .object_props
111       .set_context_xsd_any_uri(context())?
112       .set_id(id)?;
113     follow
114       .follow_props
115       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
116       .set_object_xsd_any_uri(follow_actor_id)?;
117     let to = format!("{}/inbox", follow_actor_id);
118
119     insert_activity(&conn, self.id, &follow, true)?;
120
121     send_activity(&follow, self, vec![to])?;
122     Ok(())
123   }
124
125   fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
126     let mut follow = Follow::new();
127
128     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
129
130     follow
131       .object_props
132       .set_context_xsd_any_uri(context())?
133       .set_id(id)?;
134     follow
135       .follow_props
136       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
137       .set_object_xsd_any_uri(follow_actor_id)?;
138     let to = format!("{}/inbox", follow_actor_id);
139
140     // TODO
141     // Undo that fake activity
142     let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
143     let mut undo = Undo::default();
144
145     undo
146       .object_props
147       .set_context_xsd_any_uri(context())?
148       .set_id(undo_id)?;
149
150     undo
151       .undo_props
152       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
153       .set_object_base_box(follow)?;
154
155     insert_activity(&conn, self.id, &undo, true)?;
156
157     send_activity(&undo, self, vec![to])?;
158     Ok(())
159   }
160
161   fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
162     unimplemented!()
163   }
164
165   fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
166     unimplemented!()
167   }
168
169   fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
170     unimplemented!()
171   }
172
173   fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
174     unimplemented!()
175   }
176
177   fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
178     unimplemented!()
179   }
180
181   fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
182     unimplemented!()
183   }
184 }
185
186 impl FromApub for UserForm {
187   type ApubType = PersonExt;
188   /// Parse an ActivityPub person received from another instance into a Lemmy user.
189   fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
190     let oprops = &person.inner.object_props;
191     let aprops = &person.ext_one;
192     let public_key: &PublicKey = &person.ext_two.public_key;
193
194     let avatar = match oprops.get_icon_any_image() {
195       Some(any_image) => any_image
196         .to_owned()
197         .into_concrete::<Image>()?
198         .object_props
199         .get_url_xsd_any_uri()
200         .map(|u| u.to_string()),
201       None => None,
202     };
203
204     Ok(UserForm {
205       name: oprops.get_name_xsd_string().unwrap().to_string(),
206       preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
207       password_encrypted: "".to_string(),
208       admin: false,
209       banned: false,
210       email: None,
211       avatar,
212       updated: oprops
213         .get_updated()
214         .map(|u| u.as_ref().to_owned().naive_local()),
215       show_nsfw: false,
216       theme: "".to_string(),
217       default_sort_type: 0,
218       default_listing_type: 0,
219       lang: "".to_string(),
220       show_avatars: false,
221       send_notifications_to_email: false,
222       matrix_user_id: None,
223       actor_id: oprops.get_id().unwrap().to_string(),
224       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
225       local: false,
226       private_key: None,
227       public_key: Some(public_key.to_owned().public_key_pem),
228       last_refreshed_at: Some(naive_now()),
229     })
230   }
231 }
232
233 /// Return the user json over HTTP.
234 pub async fn get_apub_user_http(
235   info: Path<UserQuery>,
236   db: DbPoolParam,
237 ) -> Result<HttpResponse<Body>, Error> {
238   let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
239   let u = user.to_apub(&db.get().unwrap())?;
240   Ok(create_apub_response(&u))
241 }