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