4 activities::send_activity, create_apub_response, insert_activity, ActorType, FromApub,
11 use activitystreams_ext::Ext1;
12 use activitystreams_new::{
13 activity::{Follow, Undo},
14 actor::{ApActor, Endpoints, Person},
16 object::{Image, Tombstone},
18 primitives::{XsdAnyUri, XsdDateTime},
20 use actix_web::{body::Body, client::Client, web, HttpResponse};
21 use failure::_core::str::FromStr;
24 user::{UserForm, User_},
26 use lemmy_utils::convert_datetime;
27 use serde::Deserialize;
29 #[derive(Deserialize)]
30 pub struct UserQuery {
34 #[async_trait::async_trait(?Send)]
35 impl ToApub for User_ {
36 type Response = PersonExt;
38 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
39 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
40 // TODO go through all these to_string and to_owned()
41 let mut person = Person::new();
43 .set_context(context())
44 .set_id(XsdAnyUri::from_str(&self.actor_id)?)
45 .set_name(self.name.to_owned())
46 .set_published(XsdDateTime::from(convert_datetime(self.published)));
48 if let Some(u) = self.updated {
49 person.set_updated(XsdDateTime::from(convert_datetime(u)));
52 if let Some(avatar_url) = &self.avatar {
53 let mut image = Image::new();
54 image.set_url(avatar_url.to_owned());
55 person.set_icon(image.into_any_base()?);
58 let mut ap_actor = ApActor::new(self.get_inbox_url().parse()?, person);
60 .set_outbox(self.get_outbox_url().parse()?)
61 .set_followers(self.get_followers_url().parse()?)
62 .set_following(self.get_following_url().parse()?)
63 .set_liked(self.get_liked_url().parse()?)
64 .set_endpoints(Endpoints {
65 shared_inbox: Some(self.get_shared_inbox_url().parse()?),
69 if let Some(i) = &self.preferred_username {
70 ap_actor.set_preferred_username(i.to_owned());
73 Ok(Ext1::new(ap_actor, self.get_public_key_ext()))
75 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
80 #[async_trait::async_trait(?Send)]
81 impl ActorType for User_ {
82 fn actor_id(&self) -> String {
83 self.actor_id.to_owned()
86 fn public_key(&self) -> String {
87 self.public_key.to_owned().unwrap()
90 fn private_key(&self) -> String {
91 self.private_key.to_owned().unwrap()
94 /// As a given local user, send out a follow request to a remote community.
97 follow_actor_id: &str,
100 ) -> Result<(), LemmyError> {
101 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
102 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
103 follow.set_context(context()).set_id(id.parse()?);
104 let to = format!("{}/inbox", follow_actor_id);
106 insert_activity(self.id, follow.clone(), true, pool).await?;
108 send_activity(client, &follow, self, vec![to]).await?;
112 async fn send_unfollow(
114 follow_actor_id: &str,
117 ) -> Result<(), LemmyError> {
118 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
119 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
120 follow.set_context(context()).set_id(id.parse()?);
122 let to = format!("{}/inbox", follow_actor_id);
125 // Undo that fake activity
126 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
127 let mut undo = Undo::new(self.actor_id.parse::<XsdAnyUri>()?, follow.into_any_base()?);
128 undo.set_context(context()).set_id(undo_id.parse()?);
130 insert_activity(self.id, undo.clone(), true, pool).await?;
132 send_activity(client, &undo, self, vec![to]).await?;
136 async fn send_delete(
141 ) -> Result<(), LemmyError> {
145 async fn send_undo_delete(
150 ) -> Result<(), LemmyError> {
154 async fn send_remove(
159 ) -> Result<(), LemmyError> {
163 async fn send_undo_remove(
168 ) -> Result<(), LemmyError> {
172 async fn send_accept_follow(
177 ) -> Result<(), LemmyError> {
181 async fn get_follower_inboxes(&self, _pool: &DbPool) -> Result<Vec<String>, LemmyError> {
186 #[async_trait::async_trait(?Send)]
187 impl FromApub for UserForm {
188 type ApubType = PersonExt;
189 /// Parse an ActivityPub person received from another instance into a Lemmy user.
190 async fn from_apub(person: &PersonExt, _: &Client, _: &DbPool) -> Result<Self, LemmyError> {
191 let avatar = match person.icon() {
192 Some(any_image) => Image::from_any_base(any_image.as_one().unwrap().clone())
197 .as_single_xsd_any_uri()
198 .map(|u| u.to_string()),
206 .as_single_xsd_string()
209 preferred_username: person.inner.preferred_username().map(|u| u.to_string()),
210 password_encrypted: "".to_string(),
217 .map(|u| u.as_ref().to_owned().naive_local()),
219 theme: "".to_string(),
220 default_sort_type: 0,
221 default_listing_type: 0,
222 lang: "".to_string(),
224 send_notifications_to_email: false,
225 matrix_user_id: None,
226 actor_id: person.id().unwrap().to_string(),
229 .map(|s| s.as_single_xsd_string().unwrap().into()),
232 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
233 last_refreshed_at: Some(naive_now()),
238 /// Return the user json over HTTP.
239 pub async fn get_apub_user_http(
240 info: web::Path<UserQuery>,
242 ) -> Result<HttpResponse<Body>, LemmyError> {
243 let user_name = info.into_inner().user_name;
244 let user = blocking(&db, move |conn| {
245 Claims::find_by_email_or_username(conn, &user_name)
248 let u = user.to_apub(&db).await?;
249 Ok(create_apub_response(&u))