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,
|
2021-10-28 21:17:59 +00:00
|
|
|
objects::get_summary_from_string_or_source,
|
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,
|
|
|
|
Source,
|
|
|
|
},
|
2020-11-24 17:53:43 +00:00
|
|
|
};
|
2021-11-01 13:05:20 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::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-22 16:21:26 +00:00
|
|
|
values::MediaTypeMarkdown,
|
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::{
|
|
|
|
naive_now,
|
2021-03-11 04:43:11 +00:00
|
|
|
source::person::{Person as DbPerson, PersonForm},
|
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 {
|
|
|
|
ApubPerson { 0: p }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
DbPerson::read_from_apub_id(conn, object_id)
|
|
|
|
})
|
|
|
|
.await??
|
|
|
|
.map(Into::into),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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 source = self.bio.clone().map(|bio| Source {
|
|
|
|
content: bio,
|
|
|
|
media_type: MediaTypeMarkdown::Markdown,
|
|
|
|
});
|
2021-11-19 17:47:06 +00:00
|
|
|
let icon = self.avatar.clone().map(ImageObject::new);
|
|
|
|
let image = self.banner.clone().map(ImageObject::new);
|
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)),
|
2021-08-12 12:48:09 +00:00
|
|
|
source,
|
|
|
|
icon,
|
|
|
|
image,
|
|
|
|
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(),
|
2021-08-12 12:48:09 +00:00
|
|
|
endpoints: Endpoints {
|
|
|
|
shared_inbox: self.shared_inbox_url.clone().map(|s| s.into()),
|
|
|
|
},
|
|
|
|
public_key: self.get_public_key()?,
|
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
unparsed: Default::default(),
|
|
|
|
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-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())?;
|
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)?;
|
|
|
|
let bio = get_summary_from_string_or_source(&person.summary, &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-11-06 17:35:14 +00:00
|
|
|
async fn from_apub(
|
|
|
|
person: Person,
|
|
|
|
context: &LemmyContext,
|
|
|
|
_request_counter: &mut i32,
|
|
|
|
) -> 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,
|
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()),
|
2021-11-06 17:35:14 +00:00
|
|
|
bio: Some(get_summary_from_string_or_source(
|
|
|
|
&person.summary,
|
|
|
|
&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-06 12:37:55 +00:00
|
|
|
public_key: Some(Some(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()),
|
2021-11-06 17:35:14 +00:00
|
|
|
shared_inbox_url: Some(person.endpoints.shared_inbox.map(|s| s.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??;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn public_key(&self) -> Option<String> {
|
|
|
|
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::*;
|
|
|
|
use crate::objects::tests::{file_to_json_object, init_context};
|
|
|
|
use lemmy_db_schema::traits::Crud;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2021-11-01 13:05:20 +00:00
|
|
|
pub(crate) async fn parse_lemmy_person(context: &LemmyContext) -> ApubPerson {
|
2021-10-29 14:54:19 +00:00
|
|
|
let json = file_to_json_object("assets/lemmy/objects/person.json");
|
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);
|
|
|
|
person
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_person() {
|
|
|
|
let context = init_context();
|
|
|
|
let person = 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.public_key.is_some());
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
2021-10-22 16:21:26 +00:00
|
|
|
async fn test_parse_pleroma_person() {
|
2021-10-21 17:25:35 +00:00
|
|
|
let context = init_context();
|
2021-10-29 14:54:19 +00:00
|
|
|
let json = file_to_json_object("assets/pleroma/objects/person.json");
|
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.public_key.is_some());
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|