2 check_apub_id_valid_with_strictness,
5 objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
8 person::{Person, UserTypes},
16 use activitypub_federation::{
17 core::object_id::ObjectId,
18 traits::{Actor, ApubObject},
19 utils::verify_domains_match,
21 use chrono::NaiveDateTime;
22 use lemmy_api_common::utils::{blocking, local_site_opt_to_slur_regex};
23 use lemmy_db_schema::{
26 person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
28 traits::{ApubActor, Crud},
33 utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
35 use lemmy_websocket::LemmyContext;
39 #[derive(Clone, Debug, PartialEq, Eq)]
40 pub struct ApubPerson(DbPerson);
42 impl Deref for ApubPerson {
43 type Target = DbPerson;
44 fn deref(&self) -> &Self::Target {
49 impl From<DbPerson> for ApubPerson {
50 fn from(p: DbPerson) -> Self {
55 #[async_trait::async_trait(?Send)]
56 impl ApubObject for ApubPerson {
57 type DataType = LemmyContext;
58 type ApubType = Person;
59 type DbType = DbPerson;
60 type Error = LemmyError;
62 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
63 Some(self.last_refreshed_at)
66 #[tracing::instrument(skip_all)]
67 async fn read_from_apub_id(
69 context: &LemmyContext,
70 ) -> Result<Option<Self>, LemmyError> {
72 blocking(context.pool(), move |conn| {
73 DbPerson::read_from_apub_id(conn, &object_id.into())
80 #[tracing::instrument(skip_all)]
81 async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
82 blocking(context.pool(), move |conn| {
83 let form = PersonUpdateForm::builder().deleted(Some(true)).build();
84 DbPerson::update(conn, self.id, &form)
90 #[tracing::instrument(skip_all)]
91 async fn into_apub(self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
92 let kind = if self.bot_account {
100 id: ObjectId::new(self.actor_id.clone()),
101 preferred_username: self.name.clone(),
102 name: self.display_name.clone(),
103 summary: self.bio.as_ref().map(|b| markdown_to_html(b)),
104 source: self.bio.clone().map(Source::new),
105 icon: self.avatar.clone().map(ImageObject::new),
106 image: self.banner.clone().map(ImageObject::new),
107 matrix_user_id: self.matrix_user_id.clone(),
108 published: Some(convert_datetime(self.published)),
109 outbox: generate_outbox_url(&self.actor_id)?.into(),
110 endpoints: self.shared_inbox_url.clone().map(|s| Endpoints {
111 shared_inbox: s.into(),
113 public_key: self.get_public_key(),
114 updated: self.updated.map(convert_datetime),
115 inbox: self.inbox_url.clone().into(),
120 #[tracing::instrument(skip_all)]
123 expected_domain: &Url,
124 context: &LemmyContext,
125 _request_counter: &mut i32,
126 ) -> Result<(), LemmyError> {
127 let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
128 let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
130 check_slurs(&person.preferred_username, slur_regex)?;
131 check_slurs_opt(&person.name, slur_regex)?;
133 verify_domains_match(person.id.inner(), expected_domain)?;
134 check_apub_id_valid_with_strictness(
141 let bio = read_from_string_or_source_opt(&person.summary, &None, &person.source);
142 check_slurs_opt(&bio, slur_regex)?;
146 #[tracing::instrument(skip_all)]
149 context: &LemmyContext,
150 request_counter: &mut i32,
151 ) -> Result<ApubPerson, LemmyError> {
152 let apub_id = person.id.inner().to_owned();
153 let instance = blocking(context.pool(), move |conn| {
154 Instance::create_from_actor_id(conn, &apub_id)
158 let person_form = PersonInsertForm {
159 name: person.preferred_username,
160 display_name: person.name,
163 deleted: Some(false),
164 avatar: person.icon.map(|i| i.url.into()),
165 banner: person.image.map(|i| i.url.into()),
166 published: person.published.map(|u| u.naive_local()),
167 updated: person.updated.map(|u| u.naive_local()),
168 actor_id: Some(person.id.into()),
169 bio: read_from_string_or_source_opt(&person.summary, &None, &person.source),
172 bot_account: Some(person.kind == UserTypes::Service),
174 public_key: person.public_key.public_key_pem,
175 last_refreshed_at: Some(naive_now()),
176 inbox_url: Some(person.inbox.into()),
177 shared_inbox_url: person.endpoints.map(|e| e.shared_inbox.into()),
178 matrix_user_id: person.matrix_user_id,
179 instance_id: instance.id,
181 let person = blocking(context.pool(), move |conn| {
182 DbPerson::create(conn, &person_form)
186 let actor_id = person.actor_id.clone().into();
187 fetch_instance_actor_for_object(actor_id, context, request_counter).await;
193 impl ActorType for ApubPerson {
194 fn actor_id(&self) -> Url {
195 self.actor_id.to_owned().into()
198 fn private_key(&self) -> Option<String> {
199 self.private_key.to_owned()
203 impl Actor for ApubPerson {
204 fn public_key(&self) -> &str {
208 fn inbox(&self) -> Url {
209 self.inbox_url.clone().into()
212 fn shared_inbox(&self) -> Option<Url> {
213 self.shared_inbox_url.clone().map(|s| s.into())
218 pub(crate) mod tests {
222 instance::{tests::parse_lemmy_instance, ApubSite},
225 protocol::{objects::instance::Instance, tests::file_to_json_object},
227 use lemmy_db_schema::{source::site::Site, traits::Crud};
228 use serial_test::serial;
230 pub(crate) async fn parse_lemmy_person(context: &LemmyContext) -> (ApubPerson, ApubSite) {
231 let site = parse_lemmy_instance(context).await;
232 let json = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
233 let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
234 let mut request_counter = 0;
235 ApubPerson::verify(&json, &url, context, &mut request_counter)
238 let person = ApubPerson::from_apub(json, context, &mut request_counter)
241 assert_eq!(request_counter, 0);
247 async fn test_parse_lemmy_person() {
248 let context = init_context();
249 let (person, site) = parse_lemmy_person(&context).await;
251 assert_eq!(person.display_name, Some("Jean-Luc Picard".to_string()));
252 assert!(!person.local);
253 assert_eq!(person.bio.as_ref().unwrap().len(), 39);
255 cleanup((person, site), &context);
260 async fn test_parse_pleroma_person() {
261 let context = init_context();
263 // create and parse a fake pleroma instance actor, to avoid network request during test
264 let mut json: Instance = file_to_json_object("assets/lemmy/objects/instance.json").unwrap();
265 let id = Url::parse("https://queer.hacktivis.me/").unwrap();
266 json.id = ObjectId::new(id);
267 let mut request_counter = 0;
268 let site = ApubSite::from_apub(json, &context, &mut request_counter)
272 let json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
273 let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
274 let mut request_counter = 0;
275 ApubPerson::verify(&json, &url, &context, &mut request_counter)
278 let person = ApubPerson::from_apub(json, &context, &mut request_counter)
282 assert_eq!(person.actor_id, url.into());
283 assert_eq!(person.name, "lanodan");
284 assert!(!person.local);
285 assert_eq!(request_counter, 0);
286 assert_eq!(person.bio.as_ref().unwrap().len(), 873);
288 cleanup((person, site), &context);
291 fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) {
292 let conn = &mut context.pool().get().unwrap();
293 DbPerson::delete(conn, data.0.id).unwrap();
294 Site::delete(conn, data.1.id).unwrap();