3 activities::{generate_activity_id, send_activity},
4 create_apub_response, insert_activity, ActorType, FromApub, PersonExt, ToApub,
10 use activitystreams_ext::Ext1;
11 use activitystreams_new::{
13 kind::{FollowType, UndoType},
16 actor::{ApActor, Endpoints, Person},
18 object::{Image, Tombstone},
21 use actix_web::{body::Body, client::Client, web, HttpResponse};
24 user::{UserForm, User_},
26 use lemmy_utils::convert_datetime;
27 use serde::Deserialize;
30 #[derive(Deserialize)]
31 pub struct UserQuery {
35 #[async_trait::async_trait(?Send)]
36 impl ToApub for User_ {
37 type Response = PersonExt;
39 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
40 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
41 // TODO go through all these to_string and to_owned()
42 let mut person = Person::new();
44 .set_context(context())
45 .set_id(Url::parse(&self.actor_id)?)
46 .set_name(self.name.to_owned())
47 .set_published(convert_datetime(self.published));
49 if let Some(u) = self.updated {
50 person.set_updated(convert_datetime(u));
53 if let Some(avatar_url) = &self.avatar {
54 let mut image = Image::new();
55 image.set_url(avatar_url.to_owned());
56 person.set_icon(image.into_any_base()?);
59 let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, person);
61 .set_outbox(self.get_outbox_url().parse()?)
62 .set_followers(self.get_followers_url().parse()?)
63 .set_following(self.get_following_url().parse()?)
64 .set_liked(self.get_liked_url().parse()?)
65 .set_endpoints(Endpoints {
66 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
70 if let Some(i) = &self.preferred_username {
71 ap_actor.set_preferred_username(i.to_owned());
74 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
76 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
81 #[async_trait::async_trait(?Send)]
82 impl ActorType for User_ {
83 fn actor_id_str(&self) -> String {
84 self.actor_id.to_owned()
87 fn public_key(&self) -> String {
88 self.public_key.to_owned().unwrap()
91 fn private_key(&self) -> String {
92 self.private_key.to_owned().unwrap()
95 /// As a given local user, send out a follow request to a remote community.
98 follow_actor_id: &str,
101 ) -> Result<(), LemmyError> {
102 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
104 .set_context(context())
105 .set_id(generate_activity_id(FollowType::Follow)?);
106 let to = format!("{}/inbox", follow_actor_id);
108 insert_activity(self.id, follow.clone(), true, pool).await?;
110 send_activity(client, &follow.into_any_base()?, self, vec![to]).await?;
114 async fn send_unfollow(
116 follow_actor_id: &str,
119 ) -> Result<(), LemmyError> {
120 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
122 .set_context(context())
123 .set_id(generate_activity_id(FollowType::Follow)?);
125 let to = format!("{}/inbox", follow_actor_id);
127 // Undo that fake activity
128 let mut undo = Undo::new(Url::parse(&self.actor_id)?, follow.into_any_base()?);
130 .set_context(context())
131 .set_id(generate_activity_id(UndoType::Undo)?);
133 insert_activity(self.id, undo.clone(), true, pool).await?;
135 send_activity(client, &undo.into_any_base()?, self, vec![to]).await?;
139 async fn send_delete(
144 ) -> Result<(), LemmyError> {
148 async fn send_undo_delete(
153 ) -> Result<(), LemmyError> {
157 async fn send_remove(
162 ) -> Result<(), LemmyError> {
166 async fn send_undo_remove(
171 ) -> Result<(), LemmyError> {
175 async fn send_accept_follow(
180 ) -> Result<(), LemmyError> {
184 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
188 fn user_id(&self) -> i32 {
193 #[async_trait::async_trait(?Send)]
194 impl FromApub for UserForm {
195 type ApubType = PersonExt;
196 /// Parse an ActivityPub person received from another instance into a Lemmy user.
202 ) -> Result<Self, LemmyError> {
203 let avatar = match person.icon() {
204 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
209 .as_single_xsd_any_uri()
210 .map(|u| u.to_string()),
223 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
224 password_encrypted: "".to_string(),
229 updated: person.updated().map(|u| u.to_owned().naive_local()),
231 theme: "".to_string(),
232 default_sort_type: 0,
233 default_listing_type: 0,
234 lang: "".to_string(),
236 send_notifications_to_email: false,
237 matrix_user_id: None,
238 actor_id: person.id(actor_id.domain().unwrap())?.unwrap().to_string(),
242 .map(|s| s.as_single_xsd_string().unwrap().into()),
245 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
246 last_refreshed_at: Some(naive_now()),
251 /// Return the user json over HTTP.
252 pub async fn get_apub_user_http(
253 info: web::Path<UserQuery>,
255 ) -> Result<HttpResponse<Body>, LemmyError> {
256 let user_name = info.into_inner().user_name;
257 let user = blocking(&db, move |conn| {
258 User_::find_by_email_or_username(conn, &user_name)
261 let u = user.to_apub(&db).await?;
262 Ok(create_apub_response(&u))