2020-11-24 17:53:43 +00:00
|
|
|
use crate::{
|
2021-08-12 12:48:09 +00:00
|
|
|
check_is_apub_id_valid,
|
2021-10-06 20:20:05 +00:00
|
|
|
generate_outbox_url,
|
2022-03-18 15:46:58 +00:00
|
|
|
objects::{
|
|
|
|
instance::fetch_instance_actor_for_object,
|
2022-03-24 16:33:42 +00:00
|
|
|
read_from_string_or_source_opt,
|
2022-03-18 15:46:58 +00:00
|
|
|
verify_image_domain_matches,
|
|
|
|
},
|
2021-11-01 13:05:20 +00:00
|
|
|
protocol::{
|
2021-11-06 13:25:34 +00:00
|
|
|
objects::{
|
|
|
|
person::{Person, UserTypes},
|
|
|
|
Endpoints,
|
|
|
|
},
|
2021-11-01 13:05:20 +00:00
|
|
|
ImageObject,
|
2022-04-01 18:25:19 +00:00
|
|
|
Source,
|
2021-11-01 13:05:20 +00:00
|
|
|
},
|
2020-11-24 17:53:43 +00:00
|
|
|
};
|
2021-11-01 13:05:20 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::utils::blocking;
|
2021-08-12 12:48:09 +00:00
|
|
|
use lemmy_apub_lib::{
|
2021-11-05 00:24:10 +00:00
|
|
|
object_id::ObjectId,
|
2021-10-27 16:03:07 +00:00
|
|
|
traits::{ActorType, ApubObject},
|
2021-10-06 20:20:05 +00:00
|
|
|
verify::verify_domains_match,
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
2020-12-18 18:38:32 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-03-11 04:43:11 +00:00
|
|
|
source::person::{Person as DbPerson, PersonForm},
|
2022-01-27 16:39:22 +00:00
|
|
|
traits::ApubActor,
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::naive_now,
|
2020-07-10 18:15:41 +00:00
|
|
|
};
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_utils::{
|
2021-08-12 12:48:09 +00:00
|
|
|
utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
|
2020-09-14 15:29:50 +00:00
|
|
|
LemmyError,
|
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-10-18 21:36:44 +00:00
|
|
|
use std::ops::Deref;
|
2020-07-17 21:11:07 +00:00
|
|
|
use url::Url;
|
2019-12-19 21:59:13 +00:00
|
|
|
|
2021-10-27 16:03:07 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2021-10-18 21:36:44 +00:00
|
|
|
pub struct ApubPerson(DbPerson);
|
|
|
|
|
|
|
|
impl Deref for ApubPerson {
|
|
|
|
type Target = DbPerson;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DbPerson> for ApubPerson {
|
|
|
|
fn from(p: DbPerson) -> Self {
|
2022-03-30 14:58:03 +00:00
|
|
|
ApubPerson(p)
|
2021-10-18 21:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-10-18 21:36:44 +00:00
|
|
|
impl ApubObject for ApubPerson {
|
|
|
|
type DataType = LemmyContext;
|
2021-10-27 16:03:07 +00:00
|
|
|
type ApubType = Person;
|
2022-03-23 21:27:51 +00:00
|
|
|
type DbType = DbPerson;
|
2021-10-28 15:25:26 +00:00
|
|
|
type TombstoneType = ();
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-18 21:36:44 +00:00
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
2022-03-23 21:27:51 +00:00
|
|
|
DbPerson::read_from_apub_id(conn, &object_id.into())
|
2021-10-18 21:36:44 +00:00
|
|
|
})
|
|
|
|
.await??
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-18 21:36:44 +00:00
|
|
|
async fn delete(self, context: &LemmyContext) -> Result<(), LemmyError> {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
DbPerson::update_deleted(conn, self.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-24 21:30:27 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 12:37:55 +00:00
|
|
|
async fn into_apub(self, _pool: &LemmyContext) -> Result<Person, LemmyError> {
|
2021-04-21 21:41:14 +00:00
|
|
|
let kind = if self.bot_account {
|
|
|
|
UserTypes::Service
|
|
|
|
} else {
|
|
|
|
UserTypes::Person
|
|
|
|
};
|
2021-08-12 12:48:09 +00:00
|
|
|
|
|
|
|
let person = Person {
|
|
|
|
kind,
|
2021-11-03 16:26:09 +00:00
|
|
|
id: ObjectId::new(self.actor_id.clone()),
|
2021-08-12 12:48:09 +00:00
|
|
|
preferred_username: self.name.clone(),
|
|
|
|
name: self.display_name.clone(),
|
2021-10-22 16:21:26 +00:00
|
|
|
summary: self.bio.as_ref().map(|b| markdown_to_html(b)),
|
2022-04-01 18:25:19 +00:00
|
|
|
source: self.bio.clone().map(Source::new),
|
2022-02-07 19:23:12 +00:00
|
|
|
icon: self.avatar.clone().map(ImageObject::new),
|
|
|
|
image: self.banner.clone().map(ImageObject::new),
|
2021-08-12 12:48:09 +00:00
|
|
|
matrix_user_id: self.matrix_user_id.clone(),
|
2021-10-21 17:25:35 +00:00
|
|
|
published: Some(convert_datetime(self.published)),
|
2021-10-06 20:20:05 +00:00
|
|
|
outbox: generate_outbox_url(&self.actor_id)?.into(),
|
2022-01-17 14:40:47 +00:00
|
|
|
endpoints: self.shared_inbox_url.clone().map(|s| Endpoints {
|
|
|
|
shared_inbox: s.into(),
|
|
|
|
}),
|
2021-08-12 12:48:09 +00:00
|
|
|
public_key: self.get_public_key()?,
|
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
inbox: self.inbox_url.clone().into(),
|
|
|
|
};
|
|
|
|
Ok(person)
|
2020-04-24 19:55:54 +00:00
|
|
|
}
|
2021-10-27 16:03:07 +00:00
|
|
|
|
2021-10-28 15:25:26 +00:00
|
|
|
fn to_tombstone(&self) -> Result<(), LemmyError> {
|
2020-05-01 14:07:38 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
2020-10-19 14:29:35 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
person: &Person,
|
2021-08-12 12:48:09 +00:00
|
|
|
expected_domain: &Url,
|
2021-11-06 17:35:14 +00:00
|
|
|
context: &LemmyContext,
|
2020-10-22 18:27:32 +00:00
|
|
|
_request_counter: &mut i32,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2021-11-03 16:26:09 +00:00
|
|
|
verify_domains_match(person.id.inner(), expected_domain)?;
|
2021-11-06 17:35:14 +00:00
|
|
|
check_is_apub_id_valid(person.id.inner(), false, &context.settings())?;
|
2022-03-18 15:46:58 +00:00
|
|
|
verify_image_domain_matches(expected_domain, &person.icon)?;
|
|
|
|
verify_image_domain_matches(expected_domain, &person.image)?;
|
2020-05-16 00:23:20 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let slur_regex = &context.settings().slur_regex();
|
2021-11-06 17:35:14 +00:00
|
|
|
check_slurs(&person.preferred_username, slur_regex)?;
|
|
|
|
check_slurs_opt(&person.name, slur_regex)?;
|
2022-05-06 23:53:33 +00:00
|
|
|
let bio = read_from_string_or_source_opt(&person.summary, &None, &person.source);
|
2021-09-22 15:57:09 +00:00
|
|
|
check_slurs_opt(&bio, slur_regex)?;
|
2021-11-06 17:35:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-22 15:57:09 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn from_apub(
|
|
|
|
person: Person,
|
|
|
|
context: &LemmyContext,
|
2022-02-07 19:23:12 +00:00
|
|
|
request_counter: &mut i32,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<ApubPerson, LemmyError> {
|
2021-08-12 12:48:09 +00:00
|
|
|
let person_form = PersonForm {
|
2021-11-06 17:35:14 +00:00
|
|
|
name: person.preferred_username,
|
|
|
|
display_name: Some(person.name),
|
2020-11-09 14:29:36 +00:00
|
|
|
banned: None,
|
2022-01-08 12:37:07 +00:00
|
|
|
ban_expires: None,
|
2021-03-10 22:33:55 +00:00
|
|
|
deleted: None,
|
2021-11-06 12:37:55 +00:00
|
|
|
avatar: Some(person.icon.map(|i| i.url.into())),
|
|
|
|
banner: Some(person.image.map(|i| i.url.into())),
|
|
|
|
published: person.published.map(|u| u.naive_local()),
|
|
|
|
updated: person.updated.map(|u| u.naive_local()),
|
|
|
|
actor_id: Some(person.id.into()),
|
2022-03-24 16:33:42 +00:00
|
|
|
bio: Some(read_from_string_or_source_opt(
|
2021-11-06 17:35:14 +00:00
|
|
|
&person.summary,
|
2022-05-06 23:53:33 +00:00
|
|
|
&None,
|
2021-11-06 17:35:14 +00:00
|
|
|
&person.source,
|
|
|
|
)),
|
2021-03-10 22:33:55 +00:00
|
|
|
local: Some(false),
|
2021-03-22 14:28:00 +00:00
|
|
|
admin: Some(false),
|
2021-11-06 17:35:14 +00:00
|
|
|
bot_account: Some(person.kind == UserTypes::Service),
|
2020-04-07 21:02:32 +00:00
|
|
|
private_key: None,
|
2021-11-22 15:10:18 +00:00
|
|
|
public_key: person.public_key.public_key_pem,
|
2020-04-07 21:02:32 +00:00
|
|
|
last_refreshed_at: Some(naive_now()),
|
2021-11-06 12:37:55 +00:00
|
|
|
inbox_url: Some(person.inbox.into()),
|
2022-01-17 14:40:47 +00:00
|
|
|
shared_inbox_url: Some(person.endpoints.map(|e| e.shared_inbox.into())),
|
2021-11-06 12:37:55 +00:00
|
|
|
matrix_user_id: Some(person.matrix_user_id),
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
|
|
|
let person = blocking(context.pool(), move |conn| {
|
|
|
|
DbPerson::upsert(conn, &person_form)
|
2020-04-07 21:02:32 +00:00
|
|
|
})
|
2021-08-12 12:48:09 +00:00
|
|
|
.await??;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
let actor_id = person.actor_id.clone().into();
|
|
|
|
fetch_instance_actor_for_object(actor_id, context, request_counter).await;
|
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(person.into())
|
2020-04-07 21:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-10-27 16:03:07 +00:00
|
|
|
impl ActorType for ApubPerson {
|
|
|
|
fn actor_id(&self) -> Url {
|
2021-11-05 00:24:10 +00:00
|
|
|
self.actor_id.to_owned().into()
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 15:10:18 +00:00
|
|
|
fn public_key(&self) -> String {
|
2021-10-27 16:03:07 +00:00
|
|
|
self.public_key.to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn private_key(&self) -> Option<String> {
|
|
|
|
self.private_key.to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inbox_url(&self) -> Url {
|
|
|
|
self.inbox_url.clone().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shared_inbox_url(&self) -> Option<Url> {
|
2021-11-05 00:24:10 +00:00
|
|
|
self.shared_inbox_url.clone().map(|s| s.into())
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 17:25:35 +00:00
|
|
|
#[cfg(test)]
|
2021-11-01 13:05:20 +00:00
|
|
|
pub(crate) mod tests {
|
2021-10-21 17:25:35 +00:00
|
|
|
use super::*;
|
2022-02-07 19:23:12 +00:00
|
|
|
use crate::{
|
|
|
|
objects::{
|
|
|
|
instance::{tests::parse_lemmy_instance, ApubSite},
|
2022-02-17 22:04:01 +00:00
|
|
|
tests::init_context,
|
2022-02-07 19:23:12 +00:00
|
|
|
},
|
2022-02-17 22:04:01 +00:00
|
|
|
protocol::{objects::instance::Instance, tests::file_to_json_object},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{source::site::Site, traits::Crud};
|
2021-10-21 17:25:35 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
pub(crate) async fn parse_lemmy_person(context: &LemmyContext) -> (ApubPerson, ApubSite) {
|
|
|
|
let site = parse_lemmy_instance(context).await;
|
2022-01-17 14:40:47 +00:00
|
|
|
let json = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
|
2021-10-22 16:21:26 +00:00
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let mut request_counter = 0;
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPerson::verify(&json, &url, context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let person = ApubPerson::from_apub(json, context, &mut request_counter)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
2021-11-01 13:05:20 +00:00
|
|
|
assert_eq!(request_counter, 0);
|
2022-02-07 19:23:12 +00:00
|
|
|
(person, site)
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_person() {
|
2022-03-03 18:54:33 +00:00
|
|
|
let context = init_context();
|
2022-02-07 19:23:12 +00:00
|
|
|
let (person, site) = parse_lemmy_person(&context).await;
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(person.display_name, Some("Jean-Luc Picard".to_string()));
|
2021-10-21 17:25:35 +00:00
|
|
|
assert!(!person.local);
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(person.bio.as_ref().unwrap().len(), 39);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
DbPerson::delete(&*context.pool().get().unwrap(), person.id).unwrap();
|
2022-02-07 19:23:12 +00:00
|
|
|
Site::delete(&*context.pool().get().unwrap(), site.id).unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn test_parse_pleroma_person() {
|
2022-03-03 18:54:33 +00:00
|
|
|
let context = init_context();
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
// create and parse a fake pleroma instance actor, to avoid network request during test
|
|
|
|
let mut json: Instance = file_to_json_object("assets/lemmy/objects/instance.json").unwrap();
|
|
|
|
let id = Url::parse("https://queer.hacktivis.me/").unwrap();
|
|
|
|
json.id = ObjectId::new(id);
|
|
|
|
let mut request_counter = 0;
|
|
|
|
let site = ApubSite::from_apub(json, &context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2022-01-17 14:40:47 +00:00
|
|
|
let json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
|
|
|
|
let mut request_counter = 0;
|
2021-11-06 17:35:14 +00:00
|
|
|
ApubPerson::verify(&json, &url, &context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let person = ApubPerson::from_apub(json, &context, &mut request_counter)
|
2021-10-21 17:25:35 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-11-05 00:24:10 +00:00
|
|
|
assert_eq!(person.actor_id, url.into());
|
2021-10-21 17:25:35 +00:00
|
|
|
assert_eq!(person.name, "lanodan");
|
|
|
|
assert!(!person.local);
|
|
|
|
assert_eq!(request_counter, 0);
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(person.bio.as_ref().unwrap().len(), 873);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
DbPerson::delete(&*context.pool().get().unwrap(), person.id).unwrap();
|
2022-02-07 19:23:12 +00:00
|
|
|
Site::delete(&*context.pool().get().unwrap(), site.id).unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|