2 extensions::context::lemmy_context,
5 get_source_markdown_value,
6 set_content_and_source,
14 use activitystreams::{
15 actor::{ApActor, Endpoints, Person},
16 object::{ApObject, Image, Tombstone},
19 use activitystreams_ext::Ext1;
21 use lemmy_db_queries::{ApubObject, DbPool};
22 use lemmy_db_schema::{
24 source::user::{UserForm, User_},
26 use lemmy_structs::blocking;
30 utils::{check_slurs, check_slurs_opt, convert_datetime},
33 use lemmy_websocket::LemmyContext;
36 #[async_trait::async_trait(?Send)]
37 impl ToApub for User_ {
38 type ApubType = PersonExt;
40 async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
41 let mut person = ApObject::new(Person::new());
43 .set_many_contexts(lemmy_context()?)
44 .set_id(Url::parse(&self.actor_id)?)
45 .set_published(convert_datetime(self.published));
47 if let Some(u) = self.updated {
48 person.set_updated(convert_datetime(u));
51 if let Some(avatar_url) = &self.avatar {
52 let mut image = Image::new();
53 image.set_url(Url::parse(avatar_url)?);
54 person.set_icon(image.into_any_base()?);
57 if let Some(banner_url) = &self.banner {
58 let mut image = Image::new();
59 image.set_url(Url::parse(banner_url)?);
60 person.set_image(image.into_any_base()?);
63 if let Some(bio) = &self.bio {
64 set_content_and_source(&mut person, bio)?;
65 // Also set summary for compatibility with older Lemmy versions.
66 // TODO: remove this after a while.
67 person.set_summary(bio.to_owned());
70 if let Some(i) = self.preferred_username.to_owned() {
74 let mut ap_actor = ApActor::new(self.get_inbox_url()?, person);
76 .set_preferred_username(self.name.to_owned())
77 .set_outbox(self.get_outbox_url()?)
78 .set_endpoints(Endpoints {
79 shared_inbox: Some(self.get_shared_inbox_url()?),
83 Ok(Ext1::new(ap_actor, self.get_public_key_ext()?))
85 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
90 #[async_trait::async_trait(?Send)]
91 impl FromApub for User_ {
92 type ApubType = PersonExt;
96 context: &LemmyContext,
98 request_counter: &mut i32,
99 ) -> Result<User_, LemmyError> {
100 let user_id = person.id_unchecked().context(location_info!())?.to_owned();
101 let domain = user_id.domain().context(location_info!())?;
102 if domain == Settings::get().hostname {
103 let user = blocking(context.pool(), move |conn| {
104 User_::read_from_apub_id(conn, user_id.as_str())
110 UserForm::from_apub(person, context, expected_domain, request_counter).await?;
111 let user = blocking(context.pool(), move |conn| User_::upsert(conn, &user_form)).await??;
117 #[async_trait::async_trait(?Send)]
118 impl FromApubToForm<PersonExt> for UserForm {
121 _context: &LemmyContext,
122 expected_domain: Url,
123 _request_counter: &mut i32,
124 ) -> Result<Self, LemmyError> {
125 let avatar = match person.icon() {
126 Some(any_image) => Some(
127 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())?
128 .context(location_info!())?
130 .context(location_info!())?
131 .as_single_xsd_any_uri()
132 .map(|u| u.to_string()),
137 let banner = match person.image() {
138 Some(any_image) => Some(
139 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
140 .context(location_info!())?
141 .context(location_info!())?
143 .context(location_info!())?
144 .as_single_xsd_any_uri()
145 .map(|u| u.to_string()),
150 let name: String = person
152 .preferred_username()
153 .context(location_info!())?
155 let preferred_username: Option<String> = person
159 .map(|n| n.to_owned().xsd_string())
162 let bio = get_source_markdown_value(person)?;
165 check_slurs_opt(&preferred_username)?;
166 check_slurs_opt(&bio)?;
170 preferred_username: Some(preferred_username),
171 password_encrypted: "".to_string(),
177 published: person.inner.published().map(|u| u.to_owned().naive_local()),
178 updated: person.updated().map(|u| u.to_owned().naive_local()),
180 theme: "".to_string(),
181 default_sort_type: 0,
182 default_listing_type: 0,
183 lang: "".to_string(),
185 send_notifications_to_email: false,
186 matrix_user_id: None,
187 actor_id: Some(check_object_domain(person, expected_domain)?),
191 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
192 last_refreshed_at: Some(naive_now()),