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