4 activities::send_activity, create_apub_response, insert_activity, ActorType, FromApub,
11 use activitystreams_ext::Ext1;
12 use activitystreams_new::{
13 activity::{Follow, Undo},
14 actor::{ApActor, Endpoints, Person},
16 object::{Image, Tombstone},
19 use actix_web::{body::Body, client::Client, web, HttpResponse};
22 user::{UserForm, User_},
24 use lemmy_utils::convert_datetime;
25 use serde::Deserialize;
28 #[derive(Deserialize)]
29 pub struct UserQuery {
33 #[async_trait::async_trait(?Send)]
34 impl ToApub for User_ {
35 type Response = PersonExt;
37 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
38 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
39 // TODO go through all these to_string and to_owned()
40 let mut person = Person::new();
42 .set_context(context())
43 .set_id(Url::parse(&self.actor_id)?)
44 .set_name(self.name.to_owned())
45 .set_published(convert_datetime(self.published));
47 if let Some(u) = self.updated {
48 person.set_updated(convert_datetime(u));
51 if let Some(avatar_url) = &self.avatar {
52 let mut image = Image::new();
53 image.set_url(avatar_url.to_owned());
54 person.set_icon(image.into_any_base()?);
57 let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, person);
59 .set_outbox(self.get_outbox_url().parse()?)
60 .set_followers(self.get_followers_url().parse()?)
61 .set_following(self.get_following_url().parse()?)
62 .set_liked(self.get_liked_url().parse()?)
63 .set_endpoints(Endpoints {
64 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
68 if let Some(i) = &self.preferred_username {
69 ap_actor.set_preferred_username(i.to_owned());
72 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
74 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
79 #[async_trait::async_trait(?Send)]
80 impl ActorType for User_ {
81 fn actor_id_str(&self) -> String {
82 self.actor_id.to_owned()
85 fn public_key(&self) -> String {
86 self.public_key.to_owned().unwrap()
89 fn private_key(&self) -> String {
90 self.private_key.to_owned().unwrap()
93 /// As a given local user, send out a follow request to a remote community.
96 follow_actor_id: &str,
99 ) -> Result<(), LemmyError> {
100 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
101 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
102 follow.set_context(context()).set_id(id.parse()?);
103 let to = format!("{}/inbox", follow_actor_id);
105 insert_activity(self.id, follow.clone(), true, pool).await?;
107 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
111 async fn send_unfollow(
113 follow_actor_id: &str,
116 ) -> Result<(), LemmyError> {
117 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
118 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
119 follow.set_context(context()).set_id(id.parse()?);
121 let to = format!("{}/inbox", follow_actor_id);
124 // Undo that fake activity
125 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
126 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
127 undo.set_context(context()).set_id(undo_id.parse()?);
129 insert_activity(self.id, undo.clone(), true, pool).await?;
131 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
135 async fn send_delete(
140 ) -> Result<(), LemmyError> {
144 async fn send_undo_delete(
149 ) -> Result<(), LemmyError> {
153 async fn send_remove(
158 ) -> Result<(), LemmyError> {
162 async fn send_undo_remove(
167 ) -> Result<(), LemmyError> {
171 async fn send_accept_follow(
176 ) -> Result<(), LemmyError> {
180 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
185 #[async_trait::async_trait(?Send)]
186 impl FromApub for UserForm {
187 type ApubType = PersonExt;
188 /// Parse an ActivityPub person received from another instance into a Lemmy user.
194 ) -> Result<Self, LemmyError> {
195 let avatar = match person.icon() {
196 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
201 .as_single_xsd_any_uri()
202 .map(|u| u.to_string()),
215 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
216 password_encrypted: "".to_string(),
221 updated: person.updated().map(|u| u.to_owned().naive_local()),
223 theme: "".to_string(),
224 default_sort_type: 0,
225 default_listing_type: 0,
226 lang: "".to_string(),
228 send_notifications_to_email: false,
229 matrix_user_id: None,
230 actor_id: person.id(actor_id.domain().unwrap())?.unwrap().to_string(),
234 .map(|s| s.as_single_xsd_string().unwrap().into()),
237 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
238 last_refreshed_at: Some(naive_now()),
243 /// Return the user json over HTTP.
244 pub async fn get_apub_user_http(
245 info: web::Path<UserQuery>,
247 ) -> Result<HttpResponse<Body>, LemmyError> {
248 let user_name = info.into_inner().user_name;
249 let user = blocking(&db, move |conn| {
250 Claims::find_by_email_or_username(conn, &user_name)
253 let u = user.to_apub(&db).await?;
254 Ok(create_apub_response(&u))