]> Untitled Git - lemmy.git/blob - server/src/apub/user.rs
Add helper function for Activity::create()
[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_activity(&conn, self.id, &follow, true)?;
77
78     send_activity(
79       &follow,
80       &self.private_key.as_ref().unwrap(),
81       &follow_actor_id,
82       vec![to],
83     )?;
84     Ok(())
85   }
86
87   fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
88     let mut follow = Follow::new();
89
90     let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
91
92     follow
93       .object_props
94       .set_context_xsd_any_uri(context())?
95       .set_id(id)?;
96     follow
97       .follow_props
98       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
99       .set_object_xsd_any_uri(follow_actor_id)?;
100     let to = format!("{}/inbox", follow_actor_id);
101
102     // TODO
103     // Undo that fake activity
104     let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
105     let mut undo = Undo::default();
106
107     undo
108       .object_props
109       .set_context_xsd_any_uri(context())?
110       .set_id(undo_id)?;
111
112     undo
113       .undo_props
114       .set_actor_xsd_any_uri(self.actor_id.to_owned())?
115       .set_object_base_box(follow)?;
116
117     insert_activity(&conn, self.id, &undo, true)?;
118
119     send_activity(
120       &undo,
121       &self.private_key.as_ref().unwrap(),
122       &follow_actor_id,
123       vec![to],
124     )?;
125     Ok(())
126   }
127
128   fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
129     unimplemented!()
130   }
131
132   fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
133     unimplemented!()
134   }
135
136   fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
137     unimplemented!()
138   }
139
140   fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
141     unimplemented!()
142   }
143
144   fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
145     unimplemented!()
146   }
147
148   fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
149     unimplemented!()
150   }
151 }
152
153 impl FromApub for UserForm {
154   type ApubType = PersonExt;
155   /// Parse an ActivityPub person received from another instance into a Lemmy user.
156   fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
157     let oprops = &person.base.base.object_props;
158     let aprops = &person.base.extension;
159     let public_key: &PublicKey = &person.extension.public_key;
160
161     Ok(UserForm {
162       name: oprops.get_name_xsd_string().unwrap().to_string(),
163       preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
164       password_encrypted: "".to_string(),
165       admin: false,
166       banned: false,
167       email: None,
168       avatar: None, // -> icon, image
169       updated: oprops
170         .get_updated()
171         .map(|u| u.as_ref().to_owned().naive_local()),
172       show_nsfw: false,
173       theme: "".to_string(),
174       default_sort_type: 0,
175       default_listing_type: 0,
176       lang: "".to_string(),
177       show_avatars: false,
178       send_notifications_to_email: false,
179       matrix_user_id: None,
180       actor_id: oprops.get_id().unwrap().to_string(),
181       bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
182       local: false,
183       private_key: None,
184       public_key: Some(public_key.to_owned().public_key_pem),
185       last_refreshed_at: Some(naive_now()),
186     })
187   }
188 }
189
190 /// Return the user json over HTTP.
191 pub async fn get_apub_user_http(
192   info: Path<UserQuery>,
193   db: DbPoolParam,
194 ) -> Result<HttpResponse<Body>, Error> {
195   let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
196   let u = user.to_apub(&db.get().unwrap())?;
197   Ok(create_apub_response(&u))
198 }