4 activities::send_activity,
17 use activitystreams_ext::Ext1;
18 use activitystreams_new::{
19 activity::{Follow, Undo},
20 actor::{ApActor, Endpoints, Person},
22 object::{Image, Tombstone},
25 use actix_web::{body::Body, client::Client, web, HttpResponse};
28 user::{UserForm, User_},
30 use lemmy_utils::convert_datetime;
31 use serde::Deserialize;
34 #[derive(Deserialize)]
35 pub struct UserQuery {
39 #[async_trait::async_trait(?Send)]
40 impl ToApub for User_ {
41 type Response = PersonExt;
43 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
44 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
45 // TODO go through all these to_string and to_owned()
46 let mut person = Person::new();
48 .set_context(context())
49 .set_id(Url::parse(&self.actor_id)?)
50 .set_name(self.name.to_owned())
51 .set_published(convert_datetime(self.published));
53 if let Some(u) = self.updated {
54 person.set_updated(convert_datetime(u));
57 if let Some(avatar_url) = &self.avatar {
58 let mut image = Image::new();
59 image.set_url(avatar_url.to_owned());
60 person.set_icon(image.into_any_base()?);
63 let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, person);
65 .set_outbox(self.get_outbox_url().parse()?)
66 .set_followers(self.get_followers_url().parse()?)
67 .set_following(self.get_following_url().parse()?)
68 .set_liked(self.get_liked_url().parse()?)
69 .set_endpoints(Endpoints {
70 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
74 if let Some(i) = &self.preferred_username {
75 ap_actor.set_preferred_username(i.to_owned());
78 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
80 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
85 #[async_trait::async_trait(?Send)]
86 impl ActorType for User_ {
87 fn actor_id_str(&self) -> String {
88 self.actor_id.to_owned()
91 fn public_key(&self) -> String {
92 self.public_key.to_owned().unwrap()
95 fn private_key(&self) -> String {
96 self.private_key.to_owned().unwrap()
99 /// As a given local user, send out a follow request to a remote community.
100 async fn send_follow(
102 follow_actor_id: &str,
105 ) -> Result<(), LemmyError> {
106 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
107 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
108 follow.set_context(context()).set_id(id.parse()?);
109 let to = format!("{}/inbox", follow_actor_id);
111 insert_activity(self.id, follow.clone(), true, pool).await?;
113 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
117 async fn send_unfollow(
119 follow_actor_id: &str,
122 ) -> Result<(), LemmyError> {
123 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
124 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
125 follow.set_context(context()).set_id(id.parse()?);
127 let to = format!("{}/inbox", follow_actor_id);
130 // Undo that fake activity
131 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
132 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
133 undo.set_context(context()).set_id(undo_id.parse()?);
135 insert_activity(self.id, undo.clone(), true, pool).await?;
137 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
141 async fn send_delete(
146 ) -> Result<(), LemmyError> {
150 async fn send_undo_delete(
155 ) -> Result<(), LemmyError> {
159 async fn send_remove(
164 ) -> Result<(), LemmyError> {
168 async fn send_undo_remove(
173 ) -> Result<(), LemmyError> {
177 async fn send_accept_follow(
182 ) -> Result<(), LemmyError> {
186 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
191 #[async_trait::async_trait(?Send)]
192 impl FromApub for UserForm {
193 type ApubType = PersonExt;
194 /// Parse an ActivityPub person received from another instance into a Lemmy user.
200 ) -> Result<Self, LemmyError> {
201 let avatar = match person.icon() {
202 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
207 .as_single_xsd_any_uri()
208 .map(|u| u.to_string()),
221 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
222 password_encrypted: "".to_string(),
227 updated: person.updated().map(|u| u.to_owned().naive_local()),
229 theme: "".to_string(),
230 default_sort_type: 0,
231 default_listing_type: 0,
232 lang: "".to_string(),
234 send_notifications_to_email: false,
235 matrix_user_id: None,
236 actor_id: person.id(actor_id.domain().unwrap())?.unwrap().to_string(),
240 .map(|s| s.as_single_xsd_string().unwrap().into()),
243 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
244 last_refreshed_at: Some(naive_now()),
249 /// Return the user json over HTTP.
250 pub async fn get_apub_user_http(
251 info: web::Path<UserQuery>,
253 ) -> Result<HttpResponse<Body>, LemmyError> {
254 let user_name = info.into_inner().user_name;
255 let user = blocking(&db, move |conn| {
256 Claims::find_by_email_or_username(conn, &user_name)
259 let u = user.to_apub(&db).await?;
260 Ok(create_apub_response(&u))