3 activities::send_activity,
5 extensions::signatures::PublicKey,
13 activity::insert_activity,
14 user::{UserForm, User_},
19 use activitystreams::{
20 activity::{Follow, Undo},
21 actor::{properties::ApActorProperties, Person},
23 endpoint::EndpointProperties,
24 object::{properties::ObjectProperties, AnyImage, Image, Tombstone},
26 use activitystreams_ext::Ext2;
27 use actix_web::{body::Body, web::Path, HttpResponse, Result};
28 use diesel::PgConnection;
30 use serde::Deserialize;
32 #[derive(Deserialize)]
33 pub struct UserQuery {
37 impl ToApub for User_ {
38 type Response = PersonExt;
40 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
41 fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
42 // TODO go through all these to_string and to_owned()
43 let mut person = Person::default();
44 let oprops: &mut ObjectProperties = person.as_mut();
46 .set_context_xsd_any_uri(context())?
47 .set_id(self.actor_id.to_string())?
48 .set_name_xsd_string(self.name.to_owned())?
49 .set_published(convert_datetime(self.published))?;
51 if let Some(u) = self.updated {
52 oprops.set_updated(convert_datetime(u))?;
55 if let Some(i) = &self.preferred_username {
56 oprops.set_name_xsd_string(i.to_owned())?;
59 if let Some(avatar_url) = &self.avatar {
60 let mut image = Image::new();
63 .set_url_xsd_any_uri(avatar_url.to_owned())?;
64 let any_image = AnyImage::from_concrete(image)?;
65 oprops.set_icon_any_image(any_image)?;
68 let mut endpoint_props = EndpointProperties::default();
70 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
72 let mut actor_props = ApActorProperties::default();
75 .set_inbox(self.get_inbox_url())?
76 .set_outbox(self.get_outbox_url())?
77 .set_endpoints(endpoint_props)?
78 .set_followers(self.get_followers_url())?
79 .set_following(self.get_following_url())?
80 .set_liked(self.get_liked_url())?;
82 Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
84 fn to_tombstone(&self) -> Result<Tombstone, Error> {
89 impl ActorType for User_ {
90 fn actor_id(&self) -> String {
91 self.actor_id.to_owned()
94 fn public_key(&self) -> String {
95 self.public_key.to_owned().unwrap()
98 fn private_key(&self) -> String {
99 self.private_key.to_owned().unwrap()
102 /// As a given local user, send out a follow request to a remote community.
103 fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
104 let mut follow = Follow::new();
106 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
110 .set_context_xsd_any_uri(context())?
114 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
115 .set_object_xsd_any_uri(follow_actor_id)?;
116 let to = format!("{}/inbox", follow_actor_id);
118 insert_activity(&conn, self.id, &follow, true)?;
120 send_activity(&follow, self, vec![to])?;
124 fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
125 let mut follow = Follow::new();
127 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
131 .set_context_xsd_any_uri(context())?
135 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
136 .set_object_xsd_any_uri(follow_actor_id)?;
137 let to = format!("{}/inbox", follow_actor_id);
140 // Undo that fake activity
141 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
142 let mut undo = Undo::default();
146 .set_context_xsd_any_uri(context())?
151 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
152 .set_object_base_box(follow)?;
154 insert_activity(&conn, self.id, &undo, true)?;
156 send_activity(&undo, self, vec![to])?;
160 fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
164 fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
168 fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
172 fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
176 fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
180 fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
185 impl FromApub for UserForm {
186 type ApubType = PersonExt;
187 /// Parse an ActivityPub person received from another instance into a Lemmy user.
188 fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
189 let oprops = &person.inner.object_props;
190 let aprops = &person.ext_one;
191 let public_key: &PublicKey = &person.ext_two.public_key;
193 let avatar = match oprops.get_icon_any_image() {
194 Some(any_image) => any_image
196 .into_concrete::<Image>()?
198 .get_url_xsd_any_uri()
199 .map(|u| u.to_string()),
204 name: oprops.get_name_xsd_string().unwrap().to_string(),
205 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
206 password_encrypted: "".to_string(),
213 .map(|u| u.as_ref().to_owned().naive_local()),
215 theme: "".to_string(),
216 default_sort_type: 0,
217 default_listing_type: 0,
218 lang: "".to_string(),
220 send_notifications_to_email: false,
221 matrix_user_id: None,
222 actor_id: oprops.get_id().unwrap().to_string(),
223 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
226 public_key: Some(public_key.to_owned().public_key_pem),
227 last_refreshed_at: Some(naive_now()),
232 /// Return the user json over HTTP.
233 pub async fn get_apub_user_http(
234 info: Path<UserQuery>,
236 ) -> Result<HttpResponse<Body>, Error> {
237 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
238 let u = user.to_apub(&db.get().unwrap())?;
239 Ok(create_apub_response(&u))