2 api::{check_slurs, check_slurs_opt},
4 activities::{generate_activity_id, send_activity},
18 use activitystreams::{
20 kind::{FollowType, UndoType},
24 actor::{ApActor, Endpoints, Person},
26 object::{Image, Tombstone},
29 use activitystreams_ext::Ext1;
30 use actix_web::{body::Body, client::Client, web, HttpResponse};
33 user::{UserForm, User_},
35 use lemmy_utils::convert_datetime;
36 use serde::Deserialize;
39 #[derive(Deserialize)]
40 pub struct UserQuery {
44 #[async_trait::async_trait(?Send)]
45 impl ToApub for User_ {
46 type Response = PersonExt;
48 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
49 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
50 // TODO go through all these to_string and to_owned()
51 let mut person = Person::new();
53 .set_context(context())
54 .set_id(Url::parse(&self.actor_id)?)
55 .set_name(self.name.to_owned())
56 .set_published(convert_datetime(self.published));
58 if let Some(u) = self.updated {
59 person.set_updated(convert_datetime(u));
62 if let Some(avatar_url) = &self.avatar {
63 let mut image = Image::new();
64 image.set_url(avatar_url.to_owned());
65 person.set_icon(image.into_any_base()?);
68 if let Some(bio) = &self.bio {
69 person.set_summary(bio.to_owned());
72 let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
74 .set_outbox(self.get_outbox_url()?)
75 .set_followers(self.get_followers_url().parse()?)
76 .set_following(self.get_following_url().parse()?)
77 .set_liked(self.get_liked_url().parse()?)
78 .set_endpoints(Endpoints {
79 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
83 if let Some(i) = &self.preferred_username {
84 ap_actor.set_preferred_username(i.to_owned());
87 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
89 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
94 #[async_trait::async_trait(?Send)]
95 impl ActorType for User_ {
96 fn actor_id_str(&self) -> String {
97 self.actor_id.to_owned()
100 fn public_key(&self) -> String {
101 self.public_key.to_owned().unwrap()
104 fn private_key(&self) -> String {
105 self.private_key.to_owned().unwrap()
108 /// As a given local user, send out a follow request to a remote community.
109 async fn send_follow(
111 follow_actor_id: &str,
114 ) -> Result<(), LemmyError> {
115 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
117 .set_context(context())
118 .set_id(generate_activity_id(FollowType::Follow)?);
119 let to = format!("{}/inbox", follow_actor_id);
121 insert_activity(self.id, follow.clone(), true, pool).await?;
123 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
127 async fn send_unfollow(
129 follow_actor_id: &str,
132 ) -> Result<(), LemmyError> {
133 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
135 .set_context(context())
136 .set_id(generate_activity_id(FollowType::Follow)?);
138 let to = format!("{}/inbox", follow_actor_id);
140 // Undo that fake activity
141 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
143 .set_context(context())
144 .set_id(generate_activity_id(UndoType::Undo)?);
146 insert_activity(self.id, undo.clone(), true, pool).await?;
148 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
152 async fn send_delete(
157 ) -> Result<(), LemmyError> {
161 async fn send_undo_delete(
166 ) -> Result<(), LemmyError> {
170 async fn send_remove(
175 ) -> Result<(), LemmyError> {
179 async fn send_undo_remove(
184 ) -> Result<(), LemmyError> {
188 async fn send_accept_follow(
193 ) -> Result<(), LemmyError> {
197 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
201 fn user_id(&self) -> i32 {
206 #[async_trait::async_trait(?Send)]
207 impl FromApub for UserForm {
208 type ApubType = PersonExt;
209 /// Parse an ActivityPub person received from another instance into a Lemmy user.
214 expected_domain: Option<Url>,
215 ) -> Result<Self, LemmyError> {
216 let avatar = match person.icon() {
217 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
222 .as_single_xsd_any_uri()
223 .map(|u| u.to_string()),
235 let preferred_username = person.inner.preferred_username().map(|u| u.to_string());
239 .map(|s| s.as_single_xsd_string().unwrap().into());
241 check_slurs_opt(&preferred_username)?;
242 check_slurs_opt(&bio)?;
247 password_encrypted: "".to_string(),
252 updated: person.updated().map(|u| u.to_owned().naive_local()),
254 theme: "".to_string(),
255 default_sort_type: 0,
256 default_listing_type: 0,
257 lang: "".to_string(),
259 send_notifications_to_email: false,
260 matrix_user_id: None,
261 actor_id: check_actor_domain(person, expected_domain)?,
265 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
266 last_refreshed_at: Some(naive_now()),
271 /// Return the user json over HTTP.
272 pub async fn get_apub_user_http(
273 info: web::Path<UserQuery>,
275 ) -> Result<HttpResponse<Body>, LemmyError> {
276 let user_name = info.into_inner().user_name;
277 let user = blocking(&db, move |conn| {
278 User_::find_by_email_or_username(conn, &user_name)
281 let u = user.to_apub(&db).await?;
282 Ok(create_apub_response(&u))