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_api_structs::blocking;
22 use lemmy_db_queries::{ApubObject, DbPool};
23 use lemmy_db_schema::{
25 source::user::{UserForm, User_},
29 settings::structs::Settings,
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(self.actor_id.to_owned().into_inner())
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>(avatar_url.to_owned().into());
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>(banner_url.to_owned().into());
60 person.set_image(image.into_any_base()?);
63 if let Some(bio) = &self.bio {
64 set_content_and_source(&mut person, bio)?;
67 if let Some(i) = self.preferred_username.to_owned() {
71 let mut ap_actor = ApActor::new(self.inbox_url.clone().into(), person);
73 .set_preferred_username(self.name.to_owned())
74 .set_outbox(self.get_outbox_url()?)
75 .set_endpoints(Endpoints {
76 shared_inbox: Some(self.get_shared_inbox_or_inbox_url()),
80 Ok(Ext1::new(ap_actor, self.get_public_key_ext()?))
82 fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
87 #[async_trait::async_trait(?Send)]
88 impl FromApub for User_ {
89 type ApubType = PersonExt;
93 context: &LemmyContext,
95 request_counter: &mut i32,
96 ) -> Result<User_, LemmyError> {
97 let user_id = person.id_unchecked().context(location_info!())?.to_owned();
98 let domain = user_id.domain().context(location_info!())?;
99 if domain == Settings::get().hostname() {
100 let user = blocking(context.pool(), move |conn| {
101 User_::read_from_apub_id(conn, &user_id.into())
107 UserForm::from_apub(person, context, expected_domain, request_counter).await?;
108 let user = blocking(context.pool(), move |conn| User_::upsert(conn, &user_form)).await??;
114 #[async_trait::async_trait(?Send)]
115 impl FromApubToForm<PersonExt> for UserForm {
118 _context: &LemmyContext,
119 expected_domain: Url,
120 _request_counter: &mut i32,
121 ) -> Result<Self, LemmyError> {
122 let avatar = match person.icon() {
123 Some(any_image) => Some(
124 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())?
125 .context(location_info!())?
127 .context(location_info!())?
128 .as_single_xsd_any_uri()
129 .map(|url| url.to_owned()),
134 let banner = match person.image() {
135 Some(any_image) => Some(
136 Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
137 .context(location_info!())?
138 .context(location_info!())?
140 .context(location_info!())?
141 .as_single_xsd_any_uri()
142 .map(|url| url.to_owned()),
147 let name: String = person
149 .preferred_username()
150 .context(location_info!())?
152 let preferred_username: Option<String> = person
156 .map(|n| n.to_owned().xsd_string())
158 let bio = get_source_markdown_value(person)?;
159 let shared_inbox = person
162 .map(|e| e.shared_inbox)
164 .map(|s| s.to_owned().into());
167 check_slurs_opt(&preferred_username)?;
168 check_slurs_opt(&bio)?;
172 preferred_username: Some(preferred_username),
173 password_encrypted: "".to_string(),
177 avatar: avatar.map(|o| o.map(|i| i.into())),
178 banner: banner.map(|o| o.map(|i| i.into())),
179 published: person.inner.published().map(|u| u.to_owned().naive_local()),
180 updated: person.updated().map(|u| u.to_owned().naive_local()),
182 theme: "".to_string(),
183 default_sort_type: 0,
184 default_listing_type: 0,
185 lang: "".to_string(),
187 send_notifications_to_email: false,
188 matrix_user_id: None,
189 actor_id: Some(check_object_domain(person, expected_domain)?),
193 public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
194 last_refreshed_at: Some(naive_now()),
195 inbox_url: Some(person.inner.inbox()?.to_owned().into()),
196 shared_inbox_url: Some(shared_inbox),