3 activities::{generate_activity_id, send_activity},
16 use activitystreams::{
18 kind::{FollowType, UndoType},
22 actor::{ApActor, Endpoints, Person},
24 object::{Image, Tombstone},
27 use activitystreams_ext::Ext1;
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 if let Some(banner_url) = &self.banner {
67 let mut image = Image::new();
68 image.set_url(banner_url.to_owned());
69 person.set_image(image.into_any_base()?);
72 if let Some(bio) = &self.bio {
73 person.set_summary(bio.to_owned());
76 let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
78 .set_outbox(self.get_outbox_url()?)
79 .set_followers(self.get_followers_url().parse()?)
80 .set_following(self.get_following_url().parse()?)
81 .set_liked(self.get_liked_url().parse()?)
82 .set_endpoints(Endpoints {
83 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
87 if let Some(i) = &self.preferred_username {
88 ap_actor.set_preferred_username(i.to_owned());
91 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
93 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
98 #[async_trait::async_trait(?Send)]
99 impl ActorType for User_ {
100 fn actor_id_str(&self) -> String {
101 self.actor_id.to_owned()
104 fn public_key(&self) -> String {
105 self.public_key.to_owned().unwrap()
108 fn private_key(&self) -> String {
109 self.private_key.to_owned().unwrap()
112 /// As a given local user, send out a follow request to a remote community.
113 async fn send_follow(
115 follow_actor_id: &str,
118 ) -> Result<(), LemmyError> {
119 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
121 .set_context(context())
122 .set_id(generate_activity_id(FollowType::Follow)?);
123 let to = format!("{}/inbox", follow_actor_id);
125 insert_activity(self.id, follow.clone(), true, pool).await?;
127 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
131 async fn send_unfollow(
133 follow_actor_id: &str,
136 ) -> Result<(), LemmyError> {
137 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
139 .set_context(context())
140 .set_id(generate_activity_id(FollowType::Follow)?);
142 let to = format!("{}/inbox", follow_actor_id);
144 // Undo that fake activity
145 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
147 .set_context(context())
148 .set_id(generate_activity_id(UndoType::Undo)?);
150 insert_activity(self.id, undo.clone(), true, pool).await?;
152 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
156 async fn send_delete(
161 ) -> Result<(), LemmyError> {
165 async fn send_undo_delete(
170 ) -> Result<(), LemmyError> {
174 async fn send_remove(
179 ) -> Result<(), LemmyError> {
183 async fn send_undo_remove(
188 ) -> Result<(), LemmyError> {
192 async fn send_accept_follow(
197 ) -> Result<(), LemmyError> {
201 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
205 fn user_id(&self) -> i32 {
210 #[async_trait::async_trait(?Send)]
211 impl FromApub for UserForm {
212 type ApubType = PersonExt;
213 /// Parse an ActivityPub person received from another instance into a Lemmy user.
214 async fn from_apub(person: &PersonExt, _: &Client, _: &DbPool) -> Result<Self, LemmyError> {
215 let avatar = match person.icon() {
216 Some(any_image) => Some(
217 Image::from_any_base(any_image.as_one().unwrap().clone())
222 .as_single_xsd_any_uri()
223 .map(|u| u.to_string()),
228 let banner = match person.image() {
229 Some(any_image) => Some(
230 Image::from_any_base(any_image.as_one().unwrap().clone())
235 .as_single_xsd_any_uri()
236 .map(|u| u.to_string()),
250 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
251 password_encrypted: "".to_string(),
257 updated: person.updated().map(|u| u.to_owned().naive_local()),
259 theme: "".to_string(),
260 default_sort_type: 0,
261 default_listing_type: 0,
262 lang: "".to_string(),
264 send_notifications_to_email: false,
265 matrix_user_id: None,
266 actor_id: person.id_unchecked().unwrap().to_string(),
270 .map(|s| s.as_single_xsd_string().unwrap().into()),
273 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
274 last_refreshed_at: Some(naive_now()),
279 /// Return the user json over HTTP.
280 pub async fn get_apub_user_http(
281 info: web::Path<UserQuery>,
283 ) -> Result<HttpResponse<Body>, LemmyError> {
284 let user_name = info.into_inner().user_name;
285 let user = blocking(&db, move |conn| {
286 User_::find_by_email_or_username(conn, &user_name)
289 let u = user.to_apub(&db).await?;
290 Ok(create_apub_response(&u))