8 impl ToApub for User_ {
9 type Response = PersonExt;
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();
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))?;
22 if let Some(u) = self.updated {
23 oprops.set_updated(convert_datetime(u))?;
26 if let Some(i) = &self.preferred_username {
27 oprops.set_name_xsd_string(i.to_owned())?;
30 if let Some(avatar_url) = &self.avatar {
31 let mut image = Image::new();
34 .set_url_xsd_any_uri(avatar_url.to_owned())?;
35 let any_image = AnyImage::from_concrete(image)?;
36 oprops.set_icon_any_image(any_image)?;
39 let mut endpoint_props = EndpointProperties::default();
41 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
43 let mut actor_props = ApActorProperties::default();
46 .set_inbox(self.get_inbox_url())?
47 .set_outbox(self.get_outbox_url())?
48 .set_endpoints(endpoint_props)?
49 .set_followers(self.get_followers_url())?
50 .set_following(self.get_following_url())?
51 .set_liked(self.get_liked_url())?;
53 Ok(person.extend(actor_props).extend(self.get_public_key_ext()))
55 fn to_tombstone(&self) -> Result<Tombstone, Error> {
60 impl ActorType for User_ {
61 fn actor_id(&self) -> String {
62 self.actor_id.to_owned()
65 fn public_key(&self) -> String {
66 self.public_key.to_owned().unwrap()
69 fn private_key(&self) -> String {
70 self.private_key.to_owned().unwrap()
73 /// As a given local user, send out a follow request to a remote community.
74 fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
75 let mut follow = Follow::new();
77 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
81 .set_context_xsd_any_uri(context())?
85 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
86 .set_object_xsd_any_uri(follow_actor_id)?;
87 let to = format!("{}/inbox", follow_actor_id);
89 insert_activity(&conn, self.id, &follow, true)?;
91 send_activity(&follow, self, vec![to])?;
95 fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
96 let mut follow = Follow::new();
98 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
102 .set_context_xsd_any_uri(context())?
106 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
107 .set_object_xsd_any_uri(follow_actor_id)?;
108 let to = format!("{}/inbox", follow_actor_id);
111 // Undo that fake activity
112 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
113 let mut undo = Undo::default();
117 .set_context_xsd_any_uri(context())?
122 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
123 .set_object_base_box(follow)?;
125 insert_activity(&conn, self.id, &undo, true)?;
127 send_activity(&undo, self, vec![to])?;
131 fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
135 fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
139 fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
143 fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
147 fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
151 fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
156 impl FromApub for UserForm {
157 type ApubType = PersonExt;
158 /// Parse an ActivityPub person received from another instance into a Lemmy user.
159 fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
160 let oprops = &person.base.base.object_props;
161 let aprops = &person.base.extension;
162 let public_key: &PublicKey = &person.extension.public_key;
164 let avatar = match oprops.get_icon_any_image() {
165 Some(any_image) => any_image
167 .into_concrete::<Image>()?
169 .get_url_xsd_any_uri()
170 .map(|u| u.to_string()),
175 name: oprops.get_name_xsd_string().unwrap().to_string(),
176 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
177 password_encrypted: "".to_string(),
184 .map(|u| u.as_ref().to_owned().naive_local()),
186 theme: "".to_string(),
187 default_sort_type: 0,
188 default_listing_type: 0,
189 lang: "".to_string(),
191 send_notifications_to_email: false,
192 matrix_user_id: None,
193 actor_id: oprops.get_id().unwrap().to_string(),
194 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
197 public_key: Some(public_key.to_owned().public_key_pem),
198 last_refreshed_at: Some(naive_now()),
203 /// Return the user json over HTTP.
204 pub async fn get_apub_user_http(
205 info: Path<UserQuery>,
207 ) -> Result<HttpResponse<Body>, Error> {
208 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
209 let u = user.to_apub(&db.get().unwrap())?;
210 Ok(create_apub_response(&u))