3 activities::send_activity,
5 extensions::signatures::PublicKey,
13 activity::insert_activity,
14 user::{UserForm, User_},
19 use activitystreams::{
20 activity::{Follow, Undo},
21 actor::{properties::ApActorProperties, Person},
23 endpoint::EndpointProperties,
24 object::{properties::ObjectProperties, AnyImage, Image},
26 use activitystreams_ext::Ext2;
27 use activitystreams_new::object::Tombstone;
28 use actix_web::{body::Body, web::Path, HttpResponse, Result};
29 use diesel::PgConnection;
31 use serde::Deserialize;
33 #[derive(Deserialize)]
34 pub struct UserQuery {
38 impl ToApub for User_ {
39 type Response = PersonExt;
41 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
42 fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
43 // TODO go through all these to_string and to_owned()
44 let mut person = Person::default();
45 let oprops: &mut ObjectProperties = person.as_mut();
47 .set_context_xsd_any_uri(context())?
48 .set_id(self.actor_id.to_string())?
49 .set_name_xsd_string(self.name.to_owned())?
50 .set_published(convert_datetime(self.published))?;
52 if let Some(u) = self.updated {
53 oprops.set_updated(convert_datetime(u))?;
56 if let Some(i) = &self.preferred_username {
57 oprops.set_name_xsd_string(i.to_owned())?;
60 if let Some(avatar_url) = &self.avatar {
61 let mut image = Image::new();
64 .set_url_xsd_any_uri(avatar_url.to_owned())?;
65 let any_image = AnyImage::from_concrete(image)?;
66 oprops.set_icon_any_image(any_image)?;
69 let mut endpoint_props = EndpointProperties::default();
71 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
73 let mut actor_props = ApActorProperties::default();
76 .set_inbox(self.get_inbox_url())?
77 .set_outbox(self.get_outbox_url())?
78 .set_endpoints(endpoint_props)?
79 .set_followers(self.get_followers_url())?
80 .set_following(self.get_following_url())?
81 .set_liked(self.get_liked_url())?;
83 Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
85 fn to_tombstone(&self) -> Result<Tombstone, Error> {
90 impl ActorType for User_ {
91 fn actor_id(&self) -> String {
92 self.actor_id.to_owned()
95 fn public_key(&self) -> String {
96 self.public_key.to_owned().unwrap()
99 fn private_key(&self) -> String {
100 self.private_key.to_owned().unwrap()
103 /// As a given local user, send out a follow request to a remote community.
104 fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
105 let mut follow = Follow::new();
107 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
111 .set_context_xsd_any_uri(context())?
115 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
116 .set_object_xsd_any_uri(follow_actor_id)?;
117 let to = format!("{}/inbox", follow_actor_id);
119 insert_activity(&conn, self.id, &follow, true)?;
121 send_activity(&follow, self, vec![to])?;
125 fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
126 let mut follow = Follow::new();
128 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
132 .set_context_xsd_any_uri(context())?
136 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
137 .set_object_xsd_any_uri(follow_actor_id)?;
138 let to = format!("{}/inbox", follow_actor_id);
141 // Undo that fake activity
142 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
143 let mut undo = Undo::default();
147 .set_context_xsd_any_uri(context())?
152 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
153 .set_object_base_box(follow)?;
155 insert_activity(&conn, self.id, &undo, true)?;
157 send_activity(&undo, self, vec![to])?;
161 fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
165 fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
169 fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
173 fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
177 fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
181 fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
186 impl FromApub for UserForm {
187 type ApubType = PersonExt;
188 /// Parse an ActivityPub person received from another instance into a Lemmy user.
189 fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
190 let oprops = &person.inner.object_props;
191 let aprops = &person.ext_one;
192 let public_key: &PublicKey = &person.ext_two.public_key;
194 let avatar = match oprops.get_icon_any_image() {
195 Some(any_image) => any_image
197 .into_concrete::<Image>()?
199 .get_url_xsd_any_uri()
200 .map(|u| u.to_string()),
205 name: oprops.get_name_xsd_string().unwrap().to_string(),
206 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
207 password_encrypted: "".to_string(),
214 .map(|u| u.as_ref().to_owned().naive_local()),
216 theme: "".to_string(),
217 default_sort_type: 0,
218 default_listing_type: 0,
219 lang: "".to_string(),
221 send_notifications_to_email: false,
222 matrix_user_id: None,
223 actor_id: oprops.get_id().unwrap().to_string(),
224 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
227 public_key: Some(public_key.to_owned().public_key_pem),
228 last_refreshed_at: Some(naive_now()),
233 /// Return the user json over HTTP.
234 pub async fn get_apub_user_http(
235 info: Path<UserQuery>,
237 ) -> Result<HttpResponse<Body>, Error> {
238 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
239 let u = user.to_apub(&db.get().unwrap())?;
240 Ok(create_apub_response(&u))