2 check_apub_id_valid_with_strictness,
4 objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
7 person::{Person, UserTypes},
15 use activitypub_federation::{
16 core::object_id::ObjectId,
17 traits::{Actor, ApubObject},
18 utils::verify_domains_match,
20 use chrono::NaiveDateTime;
21 use lemmy_api_common::{generate_outbox_url, utils::local_site_opt_to_slur_regex, LemmyContext};
22 use lemmy_db_schema::{
25 person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
27 traits::{ApubActor, Crud},
32 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
37 #[derive(Clone, Debug, PartialEq, Eq)]
38 pub struct ApubPerson(pub(crate) DbPerson);
40 impl Deref for ApubPerson {
41 type Target = DbPerson;
42 fn deref(&self) -> &Self::Target {
47 impl From<DbPerson> for ApubPerson {
48 fn from(p: DbPerson) -> Self {
53 #[async_trait::async_trait(?Send)]
54 impl ApubObject for ApubPerson {
55 type DataType = LemmyContext;
56 type ApubType = Person;
57 type DbType = DbPerson;
58 type Error = LemmyError;
60 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
61 Some(self.last_refreshed_at)
64 #[tracing::instrument(skip_all)]
65 async fn read_from_apub_id(
67 context: &LemmyContext,
68 ) -> Result<Option<Self>, LemmyError> {
70 DbPerson::read_from_apub_id(context.pool(), &object_id.into())
76 #[tracing::instrument(skip_all)]
77 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
78 let form = PersonUpdateForm::builder().deleted(Some(true)).build();
79 DbPerson::update(context.pool(), self.id, &form).await?;
83 #[tracing::instrument(skip_all)]
84 async fn into_apub(self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
85 let kind = if self.bot_account {
93 id: ObjectId::new(self.actor_id.clone()),
94 preferred_username: self.name.clone(),
95 name: self.display_name.clone(),
96 summary: self.bio.as_ref().map(|b| markdown_to_html(b)),
97 source: self.bio.clone().map(Source::new),
98 icon: self.avatar.clone().map(ImageObject::new),
99 image: self.banner.clone().map(ImageObject::new),
100 matrix_user_id: self.matrix_user_id.clone(),
101 published: Some(convert_datetime(self.published)),
102 outbox: generate_outbox_url(&self.actor_id)?.into(),
103 endpoints: self.shared_inbox_url.clone().map(|s| Endpoints {
104 shared_inbox: s.into(),
106 public_key: self.get_public_key(),
107 updated: self.updated.map(convert_datetime),
108 inbox: self.inbox_url.clone().into(),
113 #[tracing::instrument(skip_all)]
116 expected_domain: &Url,
117 context: &LemmyContext,
118 _request_counter: &mut i32,
119 ) -> Result<(), LemmyError> {
120 let local_site_data = fetch_local_site_data(context.pool()).await?;
121 let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
123 check_slurs(&person.preferred_username, slur_regex)?;
124 check_slurs_opt(&person.name, slur_regex)?;
126 verify_domains_match(person.id.inner(), expected_domain)?;
127 check_apub_id_valid_with_strictness(
134 let bio = read_from_string_or_source_opt(&person.summary, &None, &person.source);
135 check_slurs_opt(&bio, slur_regex)?;
139 #[tracing::instrument(skip_all)]
142 context: &LemmyContext,
143 request_counter: &mut i32,
144 ) -> Result<ApubPerson, LemmyError> {
145 let apub_id = person.id.inner().clone();
146 let instance = Instance::create_from_actor_id(context.pool(), &apub_id).await?;
148 let person_form = PersonInsertForm {
149 name: person.preferred_username,
150 display_name: person.name,
153 deleted: Some(false),
154 avatar: person.icon.map(|i| i.url.into()),
155 banner: person.image.map(|i| i.url.into()),
156 published: person.published.map(|u| u.naive_local()),
157 updated: person.updated.map(|u| u.naive_local()),
158 actor_id: Some(person.id.into()),
159 bio: read_from_string_or_source_opt(&person.summary, &None, &person.source),
162 bot_account: Some(person.kind == UserTypes::Service),
164 public_key: person.public_key.public_key_pem,
165 last_refreshed_at: Some(naive_now()),
166 inbox_url: Some(person.inbox.into()),
167 shared_inbox_url: person.endpoints.map(|e| e.shared_inbox.into()),
168 matrix_user_id: person.matrix_user_id,
169 instance_id: instance.id,
171 let person = DbPerson::create(context.pool(), &person_form).await?;
173 let actor_id = person.actor_id.clone().into();
174 fetch_instance_actor_for_object(actor_id, context, request_counter).await;
180 impl ActorType for ApubPerson {
181 fn actor_id(&self) -> Url {
182 self.actor_id.clone().into()
185 fn private_key(&self) -> Option<String> {
186 self.private_key.clone()
190 impl Actor for ApubPerson {
191 fn public_key(&self) -> &str {
195 fn inbox(&self) -> Url {
196 self.inbox_url.clone().into()
199 fn shared_inbox(&self) -> Option<Url> {
200 self.shared_inbox_url.clone().map(Into::into)
205 pub(crate) mod tests {
209 instance::{tests::parse_lemmy_instance, ApubSite},
212 protocol::{objects::instance::Instance, tests::file_to_json_object},
214 use lemmy_db_schema::{source::site::Site, traits::Crud};
215 use serial_test::serial;
217 pub(crate) async fn parse_lemmy_person(context: &LemmyContext) -> (ApubPerson, ApubSite) {
218 let site = parse_lemmy_instance(context).await;
219 let json = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
220 let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
221 let mut request_counter = 0;
222 ApubPerson::verify(&json, &url, context, &mut request_counter)
225 let person = ApubPerson::from_apub(json, context, &mut request_counter)
228 assert_eq!(request_counter, 0);
234 async fn test_parse_lemmy_person() {
235 let context = init_context().await;
236 let (person, site) = parse_lemmy_person(&context).await;
238 assert_eq!(person.display_name, Some("Jean-Luc Picard".to_string()));
239 assert!(!person.local);
240 assert_eq!(person.bio.as_ref().unwrap().len(), 39);
242 cleanup((person, site), &context).await;
247 async fn test_parse_pleroma_person() {
248 let context = init_context().await;
250 // create and parse a fake pleroma instance actor, to avoid network request during test
251 let mut json: Instance = file_to_json_object("assets/lemmy/objects/instance.json").unwrap();
252 let id = Url::parse("https://queer.hacktivis.me/").unwrap();
253 json.id = ObjectId::new(id);
254 let mut request_counter = 0;
255 let site = ApubSite::from_apub(json, &context, &mut request_counter)
259 let json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
260 let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
261 let mut request_counter = 0;
262 ApubPerson::verify(&json, &url, &context, &mut request_counter)
265 let person = ApubPerson::from_apub(json, &context, &mut request_counter)
269 assert_eq!(person.actor_id, url.into());
270 assert_eq!(person.name, "lanodan");
271 assert!(!person.local);
272 assert_eq!(request_counter, 0);
273 assert_eq!(person.bio.as_ref().unwrap().len(), 873);
275 cleanup((person, site), &context).await;
278 async fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) {
279 DbPerson::delete(context.pool(), data.0.id).await.unwrap();
280 Site::delete(context.pool(), data.1.id).await.unwrap();