3 activities::send_activity,
5 extensions::signatures::PublicKey,
14 activity::insert_activity,
15 user::{UserForm, User_},
22 use activitystreams::{
23 actor::{properties::ApActorProperties, Person},
25 endpoint::EndpointProperties,
26 object::{properties::ObjectProperties, AnyImage, Image},
27 primitives::XsdAnyUri,
29 use activitystreams_ext::Ext2;
30 use activitystreams_new::{
31 activity::{Follow, Undo},
35 use actix_web::{body::Body, client::Client, web, HttpResponse};
36 use serde::Deserialize;
38 #[derive(Deserialize)]
39 pub struct UserQuery {
43 #[async_trait::async_trait(?Send)]
44 impl ToApub for User_ {
45 type Response = PersonExt;
47 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
48 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
49 // TODO go through all these to_string and to_owned()
50 let mut person = Person::default();
51 let oprops: &mut ObjectProperties = person.as_mut();
53 .set_context_xsd_any_uri(context())?
54 .set_id(self.actor_id.to_string())?
55 .set_name_xsd_string(self.name.to_owned())?
56 .set_published(convert_datetime(self.published))?;
58 if let Some(u) = self.updated {
59 oprops.set_updated(convert_datetime(u))?;
62 if let Some(i) = &self.preferred_username {
63 oprops.set_name_xsd_string(i.to_owned())?;
66 if let Some(avatar_url) = &self.avatar {
67 let mut image = Image::new();
70 .set_url_xsd_any_uri(avatar_url.to_owned())?;
71 let any_image = AnyImage::from_concrete(image)?;
72 oprops.set_icon_any_image(any_image)?;
75 let mut endpoint_props = EndpointProperties::default();
77 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
79 let mut actor_props = ApActorProperties::default();
82 .set_inbox(self.get_inbox_url())?
83 .set_outbox(self.get_outbox_url())?
84 .set_endpoints(endpoint_props)?
85 .set_followers(self.get_followers_url())?
86 .set_following(self.get_following_url())?
87 .set_liked(self.get_liked_url())?;
89 Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
91 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
96 #[async_trait::async_trait(?Send)]
97 impl ActorType for User_ {
98 fn actor_id(&self) -> String {
99 self.actor_id.to_owned()
102 fn public_key(&self) -> String {
103 self.public_key.to_owned().unwrap()
106 fn private_key(&self) -> String {
107 self.private_key.to_owned().unwrap()
110 /// As a given local user, send out a follow request to a remote community.
111 async fn send_follow(
113 follow_actor_id: &str,
116 ) -> Result<(), LemmyError> {
117 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
118 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
119 follow.set_context(context()).set_id(id.parse()?);
120 let to = format!("{}/inbox", follow_actor_id);
122 insert_activity(self.id, follow.clone(), true, pool).await?;
124 send_activity(client, &follow, self, vec![to]).await?;
128 async fn send_unfollow(
130 follow_actor_id: &str,
133 ) -> Result<(), LemmyError> {
134 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
135 let mut follow = Follow::new(self.actor_id.to_owned(), follow_actor_id);
136 follow.set_context(context()).set_id(id.parse()?);
138 let to = format!("{}/inbox", follow_actor_id);
141 // Undo that fake activity
142 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
143 let mut undo = Undo::new(self.actor_id.parse::<XsdAnyUri>()?, follow.into_any_base()?);
144 undo.set_context(context()).set_id(undo_id.parse()?);
146 insert_activity(self.id, undo.clone(), true, pool).await?;
148 send_activity(client, &undo, 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> {
202 #[async_trait::async_trait(?Send)]
203 impl FromApub for UserForm {
204 type ApubType = PersonExt;
205 /// Parse an ActivityPub person received from another instance into a Lemmy user.
206 async fn from_apub(person: &PersonExt, _: &Client, _: &DbPool) -> Result<Self, LemmyError> {
207 let oprops = &person.inner.object_props;
208 let aprops = &person.ext_one;
209 let public_key: &PublicKey = &person.ext_two.public_key;
211 let avatar = match oprops.get_icon_any_image() {
212 Some(any_image) => any_image
214 .into_concrete::<Image>()?
216 .get_url_xsd_any_uri()
217 .map(|u| u.to_string()),
222 name: oprops.get_name_xsd_string().unwrap().to_string(),
223 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
224 password_encrypted: "".to_string(),
231 .map(|u| u.as_ref().to_owned().naive_local()),
233 theme: "".to_string(),
234 default_sort_type: 0,
235 default_listing_type: 0,
236 lang: "".to_string(),
238 send_notifications_to_email: false,
239 matrix_user_id: None,
240 actor_id: oprops.get_id().unwrap().to_string(),
241 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
244 public_key: Some(public_key.to_owned().public_key_pem),
245 last_refreshed_at: Some(naive_now()),
250 /// Return the user json over HTTP.
251 pub async fn get_apub_user_http(
252 info: web::Path<UserQuery>,
254 ) -> Result<HttpResponse<Body>, LemmyError> {
255 let user_name = info.into_inner().user_name;
256 let user = blocking(&db, move |conn| {
257 User_::find_by_email_or_username(conn, &user_name)
260 let u = user.to_apub(&db).await?;
261 Ok(create_apub_response(&u))