]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Merge branch 'main' into federation-authorisation
[lemmy.git] / server / src / apub / user.rs
1 use crate::{
2   api::{check_slurs, check_slurs_opt},
3   apub::{
4     activities::{generate_activity_id, send_activity},
5     check_actor_domain,
6     create_apub_response,
7     insert_activity,
8     ActorType,
9     FromApub,
10     PersonExt,
11     ToApub,
12   },
13   blocking,
14   routes::DbPoolParam,
15   DbPool,
16   LemmyError,
17 };
18 use activitystreams::{
19   activity::{
20     kind::{FollowType, UndoType},
21     Follow,
22     Undo,
23   },
24   actor::{ApActor, Endpoints, Person},
25   context,
26   object::{Image, Tombstone},
27   prelude::*,
28 };
29 use activitystreams_ext::Ext1;
30 use actix_web::{body::Body, client::Client, web, HttpResponse};
31 use lemmy_db::{
32   naive_now,
33   user::{UserForm, User_},
34 };
35 use lemmy_utils::convert_datetime;
36 use serde::Deserialize;
37 use url::Url;
38
39 #[derive(Deserialize)]
40 pub struct UserQuery {
41   user_name: String,
42 }
43
44 #[async_trait::async_trait(?Send)]
45 impl ToApub for User_ {
46   type Response = PersonExt;
47
48   // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
49   async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
50     // TODO go through all these to_string and to_owned()
51     let mut person = Person::new();
52     person
53       .set_context(context())
54       .set_id(Url::parse(&self.actor_id)?)
55       .set_name(self.name.to_owned())
56       .set_published(convert_datetime(self.published));
57
58     if let Some(u) = self.updated {
59       person.set_updated(convert_datetime(u));
60     }
61
62     if let Some(avatar_url) = &self.avatar {
63       let mut image = Image::new();
64       image.set_url(avatar_url.to_owned());
65       person.set_icon(image.into_any_base()?);
66     }
67
68     if let Some(banner_url) = &self.banner {
69       let mut image = Image::new();
70       image.set_url(banner_url.to_owned());
71       person.set_image(image.into_any_base()?);
72     }
73
74     if let Some(bio) = &self.bio {
75       person.set_summary(bio.to_owned());
76     }
77
78     let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
79     ap_actor
80       .set_outbox(self.get_outbox_url()?)
81       .set_followers(self.get_followers_url().parse()?)
82       .set_following(self.get_following_url().parse()?)
83       .set_liked(self.get_liked_url().parse()?)
84       .set_endpoints(Endpoints {
85         shared_inbox: Some(self.get_shared_inbox_url().parse()?),
86         ..Default::default()
87       });
88
89     if let Some(i) = &self.preferred_username {
90       ap_actor.set_preferred_username(i.to_owned());
91     }
92
93     Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
94   }
95   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
96     unimplemented!()
97   }
98 }
99
100 #[async_trait::async_trait(?Send)]
101 impl ActorType for User_ {
102   fn actor_id_str(&self) -> String {
103     self.actor_id.to_owned()
104   }
105
106   fn public_key(&self) -> String {
107     self.public_key.to_owned().unwrap()
108   }
109
110   fn private_key(&self) -> String {
111     self.private_key.to_owned().unwrap()
112   }
113
114   /// As a given local user, send out a follow request to a remote community.
115   async fn send_follow(
116     &self,
117     follow_actor_id: &str,
118     client: &Client,
119     pool: &DbPool,
120   ) -> Result<(), LemmyError> {
121     let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
122     follow
123       .set_context(context())
124       .set_id(generate_activity_id(FollowType::Follow)?);
125     let to = format!("{}/inbox", follow_actor_id);
126
127     insert_activity(self.id, follow.clone(), true, pool).await?;
128
129     send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
130     Ok(())
131   }
132
133   async fn send_unfollow(
134     &self,
135     follow_actor_id: &str,
136     client: &Client,
137     pool: &DbPool,
138   ) -> Result<(), LemmyError> {
139     let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
140     follow
141       .set_context(context())
142       .set_id(generate_activity_id(FollowType::Follow)?);
143
144     let to = format!("{}/inbox", follow_actor_id);
145
146     // Undo that fake activity
147     let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
148     undo
149       .set_context(context())
150       .set_id(generate_activity_id(UndoType::Undo)?);
151
152     insert_activity(self.id, undo.clone(), true, pool).await?;
153
154     send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
155     Ok(())
156   }
157
158   async fn send_delete(
159     &self,
160     _creator: &User_,
161     _client: &Client,
162     _pool: &DbPool,
163   ) -> Result<(), LemmyError> {
164     unimplemented!()
165   }
166
167   async fn send_undo_delete(
168     &self,
169     _creator: &User_,
170     _client: &Client,
171     _pool: &DbPool,
172   ) -> Result<(), LemmyError> {
173     unimplemented!()
174   }
175
176   async fn send_remove(
177     &self,
178     _creator: &User_,
179     _client: &Client,
180     _pool: &DbPool,
181   ) -> Result<(), LemmyError> {
182     unimplemented!()
183   }
184
185   async fn send_undo_remove(
186     &self,
187     _creator: &User_,
188     _client: &Client,
189     _pool: &DbPool,
190   ) -> Result<(), LemmyError> {
191     unimplemented!()
192   }
193
194   async fn send_accept_follow(
195     &self,
196     _follow: Follow,
197     _client: &Client,
198     _pool: &DbPool,
199   ) -> Result<(), LemmyError> {
200     unimplemented!()
201   }
202
203   async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
204     unimplemented!()
205   }
206
207   fn user_id(&self) -> i32 {
208     self.id
209   }
210 }
211
212 #[async_trait::async_trait(?Send)]
213 impl FromApub for UserForm {
214   type ApubType = PersonExt;
215   /// Parse an ActivityPub person received from another instance into a Lemmy user.
216   async fn from_apub(
217     person: &PersonExt,
218     _: &Client,
219     _: &DbPool,
220     expected_domain: Option<Url>,
221   ) -> Result<Self, LemmyError> {
222     let avatar = match person.icon() {
223       Some(any_image) => Some(
224         Image::from_any_base(any_image.as_one().unwrap().clone())
225           .unwrap()
226           .unwrap()
227           .url()
228           .unwrap()
229           .as_single_xsd_any_uri()
230           .map(|u| u.to_string()),
231       ),
232       None => None,
233     };
234
235     let banner = match person.image() {
236       Some(any_image) => Some(
237         Image::from_any_base(any_image.as_one().unwrap().clone())
238           .unwrap()
239           .unwrap()
240           .url()
241           .unwrap()
242           .as_single_xsd_any_uri()
243           .map(|u| u.to_string()),
244       ),
245       None => None,
246     };
247
248     let name = person
249       .name()
250       .unwrap()
251       .one()
252       .unwrap()
253       .as_xsd_string()
254       .unwrap()
255       .to_string();
256     let preferred_username = person.inner.preferred_username().map(|u| u.to_string());
257     let bio = person
258       .inner
259       .summary()
260       .map(|s| s.as_single_xsd_string().unwrap().into());
261     check_slurs(&name)?;
262     check_slurs_opt(&preferred_username)?;
263     check_slurs_opt(&bio)?;
264
265     Ok(UserForm {
266       name,
267       preferred_username,
268       password_encrypted: "".to_string(),
269       admin: false,
270       banned: false,
271       email: None,
272       avatar,
273       banner,
274       updated: person.updated().map(|u| u.to_owned().naive_local()),
275       show_nsfw: false,
276       theme: "".to_string(),
277       default_sort_type: 0,
278       default_listing_type: 0,
279       lang: "".to_string(),
280       show_avatars: false,
281       send_notifications_to_email: false,
282       matrix_user_id: None,
283       actor_id: check_actor_domain(person, expected_domain)?,
284       bio,
285       local: false,
286       private_key: None,
287       public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
288       last_refreshed_at: Some(naive_now()),
289     })
290   }
291 }
292
293 /// Return the user json over HTTP.
294 pub async fn get_apub_user_http(
295   info: web::Path<UserQuery>,
296   db: DbPoolParam,
297 ) -> Result<HttpResponse<Body>, LemmyError> {
298   let user_name = info.into_inner().user_name;
299   let user = blocking(&db, move |conn| {
300     User_::find_by_email_or_username(conn, &user_name)
301   })
302   .await??;
303   let u = user.to_apub(&db).await?;
304   Ok(create_apub_response(&u))
305 }