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, 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 let mut endpoint_props = EndpointProperties::default();
61 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
63 let mut actor_props = ApActorProperties::default();
66 .set_inbox(self.get_inbox_url())?
67 .set_outbox(self.get_outbox_url())?
68 .set_endpoints(endpoint_props)?
69 .set_followers(self.get_followers_url())?
70 .set_following(self.get_following_url())?
71 .set_liked(self.get_liked_url())?;
73 Ok(Ext2::new(person, actor_props, self.get_public_key_ext()))
75 fn to_tombstone(&self) -> Result<Tombstone, Error> {
80 impl ActorType for User_ {
81 fn actor_id(&self) -> String {
82 self.actor_id.to_owned()
85 fn public_key(&self) -> String {
86 self.public_key.to_owned().unwrap()
89 fn private_key(&self) -> String {
90 self.private_key.to_owned().unwrap()
93 /// As a given local user, send out a follow request to a remote community.
94 fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
95 let mut follow = Follow::new();
97 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
101 .set_context_xsd_any_uri(context())?
105 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
106 .set_object_xsd_any_uri(follow_actor_id)?;
107 let to = format!("{}/inbox", follow_actor_id);
109 insert_activity(&conn, self.id, &follow, true)?;
111 send_activity(&follow, self, vec![to])?;
115 fn send_unfollow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
116 let mut follow = Follow::new();
118 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
122 .set_context_xsd_any_uri(context())?
126 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
127 .set_object_xsd_any_uri(follow_actor_id)?;
128 let to = format!("{}/inbox", follow_actor_id);
131 // Undo that fake activity
132 let undo_id = format!("{}/undo/follow/{}", self.actor_id, uuid::Uuid::new_v4());
133 let mut undo = Undo::default();
137 .set_context_xsd_any_uri(context())?
142 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
143 .set_object_base_box(follow)?;
145 insert_activity(&conn, self.id, &undo, true)?;
147 send_activity(&undo, self, vec![to])?;
151 fn send_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
155 fn send_undo_delete(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
159 fn send_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
163 fn send_undo_remove(&self, _creator: &User_, _conn: &PgConnection) -> Result<(), Error> {
167 fn send_accept_follow(&self, _follow: &Follow, _conn: &PgConnection) -> Result<(), Error> {
171 fn get_follower_inboxes(&self, _conn: &PgConnection) -> Result<Vec<String>, Error> {
176 impl FromApub for UserForm {
177 type ApubType = PersonExt;
178 /// Parse an ActivityPub person received from another instance into a Lemmy user.
179 fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
180 let oprops = &person.inner.object_props;
181 let aprops = &person.ext_one;
182 let public_key: &PublicKey = &person.ext_two.public_key;
185 name: oprops.get_name_xsd_string().unwrap().to_string(),
186 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
187 password_encrypted: "".to_string(),
191 avatar: None, // -> icon, image
194 .map(|u| u.as_ref().to_owned().naive_local()),
196 theme: "".to_string(),
197 default_sort_type: 0,
198 default_listing_type: 0,
199 lang: "".to_string(),
201 send_notifications_to_email: false,
202 matrix_user_id: None,
203 actor_id: oprops.get_id().unwrap().to_string(),
204 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
207 public_key: Some(public_key.to_owned().public_key_pem),
208 last_refreshed_at: Some(naive_now()),
213 /// Return the user json over HTTP.
214 pub async fn get_apub_user_http(
215 info: Path<UserQuery>,
217 ) -> Result<HttpResponse<Body>, Error> {
218 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
219 let u = user.to_apub(&db.get().unwrap())?;
220 Ok(create_apub_response(&u))