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