3 activities::{generate_activity_id, send_activity},
16 use activitystreams_ext::Ext1;
17 use activitystreams_new::{
19 kind::{FollowType, UndoType},
23 actor::{ApActor, Endpoints, Person},
25 object::{Image, Tombstone},
28 use actix_web::{body::Body, client::Client, web, HttpResponse};
31 user::{UserForm, User_},
33 use lemmy_utils::convert_datetime;
34 use serde::Deserialize;
37 #[derive(Deserialize)]
38 pub struct UserQuery {
42 #[async_trait::async_trait(?Send)]
43 impl ToApub for User_ {
44 type Response = PersonExt;
46 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
47 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
48 // TODO go through all these to_string and to_owned()
49 let mut person = Person::new();
51 .set_context(context())
52 .set_id(Url::parse(&self.actor_id)?)
53 .set_name(self.name.to_owned())
54 .set_published(convert_datetime(self.published));
56 if let Some(u) = self.updated {
57 person.set_updated(convert_datetime(u));
60 if let Some(avatar_url) = &self.avatar {
61 let mut image = Image::new();
62 image.set_url(avatar_url.to_owned());
63 person.set_icon(image.into_any_base()?);
66 let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
68 .set_outbox(self.get_outbox_url()?)
69 .set_followers(self.get_followers_url().parse()?)
70 .set_following(self.get_following_url().parse()?)
71 .set_liked(self.get_liked_url().parse()?)
72 .set_endpoints(Endpoints {
73 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
77 if let Some(i) = &self.preferred_username {
78 ap_actor.set_preferred_username(i.to_owned());
81 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
83 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
88 #[async_trait::async_trait(?Send)]
89 impl ActorType for User_ {
90 fn actor_id_str(&self) -> String {
91 self.actor_id.to_owned()
94 fn public_key(&self) -> String {
95 self.public_key.to_owned().unwrap()
98 fn private_key(&self) -> String {
99 self.private_key.to_owned().unwrap()
102 /// As a given local user, send out a follow request to a remote community.
103 async fn send_follow(
105 follow_actor_id: &str,
108 ) -> Result<(), LemmyError> {
109 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
111 .set_context(context())
112 .set_id(generate_activity_id(FollowType::Follow)?);
113 let to = format!("{}/inbox", follow_actor_id);
115 insert_activity(self.id, follow.clone(), true, pool).await?;
117 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
121 async fn send_unfollow(
123 follow_actor_id: &str,
126 ) -> Result<(), LemmyError> {
127 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
129 .set_context(context())
130 .set_id(generate_activity_id(FollowType::Follow)?);
132 let to = format!("{}/inbox", follow_actor_id);
134 // Undo that fake activity
135 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
137 .set_context(context())
138 .set_id(generate_activity_id(UndoType::Undo)?);
140 insert_activity(self.id, undo.clone(), true, pool).await?;
142 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
146 async fn send_delete(
151 ) -> Result<(), LemmyError> {
155 async fn send_undo_delete(
160 ) -> Result<(), LemmyError> {
164 async fn send_remove(
169 ) -> Result<(), LemmyError> {
173 async fn send_undo_remove(
178 ) -> Result<(), LemmyError> {
182 async fn send_accept_follow(
187 ) -> Result<(), LemmyError> {
191 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
195 fn user_id(&self) -> i32 {
200 #[async_trait::async_trait(?Send)]
201 impl FromApub for UserForm {
202 type ApubType = PersonExt;
203 /// Parse an ActivityPub person received from another instance into a Lemmy user.
204 async fn from_apub(person: &PersonExt, _: &Client, _: &DbPool) -> Result<Self, LemmyError> {
205 let avatar = match person.icon() {
206 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
211 .as_single_xsd_any_uri()
212 .map(|u| u.to_string()),
225 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
226 password_encrypted: "".to_string(),
231 updated: person.updated().map(|u| u.to_owned().naive_local()),
233 theme: "".to_string(),
234 default_sort_type: 0,
235 default_listing_type: 0,
236 lang: "".to_string(),
238 send_notifications_to_email: false,
239 matrix_user_id: None,
240 actor_id: person.id_unchecked().unwrap().to_string(),
244 .map(|s| s.as_single_xsd_string().unwrap().into()),
247 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
248 last_refreshed_at: Some(naive_now()),
253 /// Return the user json over HTTP.
254 pub async fn get_apub_user_http(
255 info: web::Path<UserQuery>,
257 ) -> Result<HttpResponse<Body>, LemmyError> {
258 let user_name = info.into_inner().user_name;
259 let user = blocking(&db, move |conn| {
260 User_::find_by_email_or_username(conn, &user_name)
263 let u = user.to_apub(&db).await?;
264 Ok(create_apub_response(&u))