8 impl ToApub for User_ {
9 type Response = PersonExt;
11 // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
12 fn to_apub(&self, _conn: &PgConnection) -> Result<ResponseOrTombstone<PersonExt>, Error> {
13 // TODO go through all these to_string and to_owned()
14 let mut person = Person::default();
15 let oprops: &mut ObjectProperties = person.as_mut();
17 .set_context_xsd_any_uri(context())?
18 .set_id(self.actor_id.to_string())?
19 .set_name_xsd_string(self.name.to_owned())?
20 .set_published(convert_datetime(self.published))?;
22 if let Some(u) = self.updated {
23 oprops.set_updated(convert_datetime(u))?;
26 if let Some(i) = &self.preferred_username {
27 oprops.set_name_xsd_string(i.to_owned())?;
30 let mut endpoint_props = EndpointProperties::default();
32 endpoint_props.set_shared_inbox(self.get_shared_inbox_url())?;
34 let mut actor_props = ApActorProperties::default();
37 .set_inbox(self.get_inbox_url())?
38 .set_outbox(self.get_outbox_url())?
39 .set_endpoints(endpoint_props)?
40 .set_followers(self.get_followers_url())?
41 .set_following(self.get_following_url())?
42 .set_liked(self.get_liked_url())?;
44 Ok(ResponseOrTombstone::Response(
45 person.extend(actor_props).extend(self.get_public_key_ext()),
50 impl ActorType for User_ {
51 fn actor_id(&self) -> String {
52 self.actor_id.to_owned()
55 fn public_key(&self) -> String {
56 self.public_key.to_owned().unwrap()
59 /// As a given local user, send out a follow request to a remote community.
60 fn send_follow(&self, follow_actor_id: &str, conn: &PgConnection) -> Result<(), Error> {
61 let mut follow = Follow::new();
63 let id = format!("{}/follow/{}", self.actor_id, uuid::Uuid::new_v4());
67 .set_context_xsd_any_uri(context())?
71 .set_actor_xsd_any_uri(self.actor_id.to_owned())?
72 .set_object_xsd_any_uri(follow_actor_id)?;
73 let to = format!("{}/inbox", follow_actor_id);
75 // Insert the sent activity into the activity table
76 let activity_form = activity::ActivityForm {
78 data: serde_json::to_value(&follow)?,
82 activity::Activity::create(&conn, &activity_form)?;
86 &self.private_key.as_ref().unwrap(),
93 fn send_delete(&self, _conn: &PgConnection) -> Result<(), Error> {
98 impl FromApub for UserForm {
99 type ApubType = PersonExt;
100 /// Parse an ActivityPub person received from another instance into a Lemmy user.
101 fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
102 let oprops = &person.base.base.object_props;
103 let aprops = &person.base.extension;
104 let public_key: &PublicKey = &person.extension.public_key;
107 name: oprops.get_name_xsd_string().unwrap().to_string(),
108 preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
109 password_encrypted: "".to_string(),
113 avatar: None, // -> icon, image
116 .map(|u| u.as_ref().to_owned().naive_local()),
118 theme: "".to_string(),
119 default_sort_type: 0,
120 default_listing_type: 0,
121 lang: "".to_string(),
123 send_notifications_to_email: false,
124 matrix_user_id: None,
125 actor_id: oprops.get_id().unwrap().to_string(),
126 bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
129 public_key: Some(public_key.to_owned().public_key_pem),
130 last_refreshed_at: Some(naive_now()),
135 /// Return the user json over HTTP.
136 pub async fn get_apub_user_http(
137 info: Path<UserQuery>,
139 ) -> Result<HttpResponse<Body>, Error> {
140 let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
141 let u = user.to_apub(&db.get().unwrap())?;
142 Ok(create_apub_response(&u))