3 activities::send_activity,
16 use activitystreams_ext::Ext1;
17 use activitystreams_new::{
18 activity::{Follow, Undo},
19 actor::{ApActor, Endpoints, Person},
21 object::{Image, Tombstone},
24 use actix_web::{body::Body, client::Client, web, HttpResponse};
27 user::{UserForm, User_},
29 use lemmy_utils::convert_datetime;
30 use serde::Deserialize;
33 #[derive(Deserialize)]
34 pub struct UserQuery {
38 #[async_trait::async_trait(?Send)]
39 impl ToApub for User_ {
40 type Response = PersonExt;
42 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
43 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
44 // TODO go through all these to_string and to_owned()
45 let mut person = Person::new();
47 .set_context(context())
48 .set_id(Url::parse(&self.actor_id)?)
49 .set_name(self.name.to_owned())
50 .set_published(convert_datetime(self.published));
52 if let Some(u) = self.updated {
53 person.set_updated(convert_datetime(u));
56 if let Some(avatar_url) = &self.avatar {
57 let mut image = Image::new();
58 image.set_url(avatar_url.to_owned());
59 person.set_icon(image.into_any_base()?);
62 let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, person);
64 .set_outbox(self.get_outbox_url().parse()?)
65 .set_followers(self.get_followers_url().parse()?)
66 .set_following(self.get_following_url().parse()?)
67 .set_liked(self.get_liked_url().parse()?)
68 .set_endpoints(Endpoints {
69 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
73 if let Some(i) = &self.preferred_username {
74 ap_actor.set_preferred_username(i.to_owned());
77 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
79 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
84 #[async_trait::async_trait(?Send)]
85 impl ActorType for User_ {
86 fn actor_id_str(&self) -> String {
87 self.actor_id.to_owned()
90 fn public_key(&self) -> String {
91 self.public_key.to_owned().unwrap()
94 fn private_key(&self) -> String {
95 self.private_key.to_owned().unwrap()
98 /// As a given local user, send out a follow request to a remote community.
101 follow_actor_id: &str,
104 ) -> Result<(), LemmyError> {
105 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
106 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
107 follow.set_context(context()).set_id(id.parse()?);
108 let to = format!("{}/inbox", follow_actor_id);
110 insert_activity(self.id, follow.clone(), true, pool).await?;
112 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
116 async fn send_unfollow(
118 follow_actor_id: &str,
121 ) -> Result<(), LemmyError> {
122 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
123 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
124 follow.set_context(context()).set_id(id.parse()?);
126 let to = format!("{}/inbox", follow_actor_id);
129 // Undo that fake activity
130 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
131 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
132 undo.set_context(context()).set_id(undo_id.parse()?);
134 insert_activity(self.id, undo.clone(), true, pool).await?;
136 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
140 async fn send_delete(
145 ) -> Result<(), LemmyError> {
149 async fn send_undo_delete(
154 ) -> Result<(), LemmyError> {
158 async fn send_remove(
163 ) -> Result<(), LemmyError> {
167 async fn send_undo_remove(
172 ) -> Result<(), LemmyError> {
176 async fn send_accept_follow(
181 ) -> Result<(), LemmyError> {
185 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
189 fn user_id(&self) -> i32 {
194 #[async_trait::async_trait(?Send)]
195 impl FromApub for UserForm {
196 type ApubType = PersonExt;
197 /// Parse an ActivityPub person received from another instance into a Lemmy user.
203 ) -> Result<Self, LemmyError> {
204 let avatar = match person.icon() {
205 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
210 .as_single_xsd_any_uri()
211 .map(|u| u.to_string()),
224 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
225 password_encrypted: "".to_string(),
230 updated: person.updated().map(|u| u.to_owned().naive_local()),
232 theme: "".to_string(),
233 default_sort_type: 0,
234 default_listing_type: 0,
235 lang: "".to_string(),
237 send_notifications_to_email: false,
238 matrix_user_id: None,
239 actor_id: person.id(actor_id.domain().unwrap())?.unwrap().to_string(),
243 .map(|s| s.as_single_xsd_string().unwrap().into()),
246 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
247 last_refreshed_at: Some(naive_now()),
252 /// Return the user json over HTTP.
253 pub async fn get_apub_user_http(
254 info: web::Path<UserQuery>,
256 ) -> Result<HttpResponse<Body>, LemmyError> {
257 let user_name = info.into_inner().user_name;
258 let user = blocking(&db, move |conn| {
259 User_::find_by_email_or_username(conn, &user_name)
262 let u = user.to_apub(&db).await?;
263 Ok(create_apub_response(&u))