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(banner_url) = &self.banner {
69 let mut image = Image::new();
70 image.set_url(banner_url.to_owned());
71 person.set_image(image.into_any_base()?);
74 if let Some(bio) = &self.bio {
75 person.set_summary(bio.to_owned());
78 let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
80 .set_outbox(self.get_outbox_url()?)
81 .set_followers(self.get_followers_url().parse()?)
82 .set_following(self.get_following_url().parse()?)
83 .set_liked(self.get_liked_url().parse()?)
84 .set_endpoints(Endpoints {
85 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
89 if let Some(i) = &self.preferred_username {
90 ap_actor.set_preferred_username(i.to_owned());
93 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
95 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
100 #[async_trait::async_trait(?Send)]
101 impl ActorType for User_ {
102 fn actor_id_str(&self) -> String {
103 self.actor_id.to_owned()
106 fn public_key(&self) -> String {
107 self.public_key.to_owned().unwrap()
110 fn private_key(&self) -> String {
111 self.private_key.to_owned().unwrap()
114 /// As a given local user, send out a follow request to a remote community.
115 async fn send_follow(
117 follow_actor_id: &str,
120 ) -> Result<(), LemmyError> {
121 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
123 .set_context(context())
124 .set_id(generate_activity_id(FollowType::Follow)?);
125 let to = format!("{}/inbox", follow_actor_id);
127 insert_activity(self.id, follow.clone(), true, pool).await?;
129 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
133 async fn send_unfollow(
135 follow_actor_id: &str,
138 ) -> Result<(), LemmyError> {
139 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
141 .set_context(context())
142 .set_id(generate_activity_id(FollowType::Follow)?);
144 let to = format!("{}/inbox", follow_actor_id);
146 // Undo that fake activity
147 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
149 .set_context(context())
150 .set_id(generate_activity_id(UndoType::Undo)?);
152 insert_activity(self.id, undo.clone(), true, pool).await?;
154 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
158 async fn send_delete(
163 ) -> Result<(), LemmyError> {
167 async fn send_undo_delete(
172 ) -> Result<(), LemmyError> {
176 async fn send_remove(
181 ) -> Result<(), LemmyError> {
185 async fn send_undo_remove(
190 ) -> Result<(), LemmyError> {
194 async fn send_accept_follow(
199 ) -> Result<(), LemmyError> {
203 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
207 fn user_id(&self) -> i32 {
212 #[async_trait::async_trait(?Send)]
213 impl FromApub for UserForm {
214 type ApubType = PersonExt;
215 /// Parse an ActivityPub person received from another instance into a Lemmy user.
220 expected_domain: Option<Url>,
221 ) -> Result<Self, LemmyError> {
222 let avatar = match person.icon() {
223 Some(any_image) => Some(
224 Image::from_any_base(any_image.as_one().unwrap().clone())
229 .as_single_xsd_any_uri()
230 .map(|u| u.to_string()),
235 let banner = match person.image() {
236 Some(any_image) => Some(
237 Image::from_any_base(any_image.as_one().unwrap().clone())
242 .as_single_xsd_any_uri()
243 .map(|u| u.to_string()),
256 let preferred_username = person.inner.preferred_username().map(|u| u.to_string());
260 .map(|s| s.as_single_xsd_string().unwrap().into());
262 check_slurs_opt(&preferred_username)?;
263 check_slurs_opt(&bio)?;
268 password_encrypted: "".to_string(),
274 updated: person.updated().map(|u| u.to_owned().naive_local()),
276 theme: "".to_string(),
277 default_sort_type: 0,
278 default_listing_type: 0,
279 lang: "".to_string(),
281 send_notifications_to_email: false,
282 matrix_user_id: None,
283 actor_id: check_actor_domain(person, expected_domain)?,
287 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
288 last_refreshed_at: Some(naive_now()),
293 /// Return the user json over HTTP.
294 pub async fn get_apub_user_http(
295 info: web::Path<UserQuery>,
297 ) -> Result<HttpResponse<Body>, LemmyError> {
298 let user_name = info.into_inner().user_name;
299 let user = blocking(&db, move |conn| {
300 User_::find_by_email_or_username(conn, &user_name)
303 let u = user.to_apub(&db).await?;
304 Ok(create_apub_response(&u))