2020-11-24 17:53:43 +00:00
|
|
|
use crate::{
|
2022-06-02 14:33:41 +00:00
|
|
|
check_apub_id_valid_with_strictness,
|
2022-10-27 09:24:07 +00:00
|
|
|
fetch_local_site_data,
|
2021-10-06 20:20:05 +00:00
|
|
|
generate_outbox_url,
|
2022-05-10 17:08:13 +00:00
|
|
|
objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
|
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
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2022-06-08 15:45:39 +00:00
|
|
|
core::object_id::ObjectId,
|
|
|
|
traits::{Actor, ApubObject},
|
2022-06-02 14:33:41 +00:00
|
|
|
utils::verify_domains_match,
|
2020-11-24 17:53:43 +00:00
|
|
|
};
|
2021-11-01 13:05:20 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_api_common::utils::{blocking, local_site_opt_to_slur_regex};
|
2020-12-18 18:38:32 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
|
|
|
instance::Instance,
|
|
|
|
person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
|
|
|
|
},
|
|
|
|
traits::{ApubActor, Crud},
|
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::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
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
|
|
|
};
|
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
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
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| {
|
2022-10-27 09:24:07 +00:00
|
|
|
let form = PersonUpdateForm::builder().deleted(Some(true)).build();
|
|
|
|
DbPerson::update(conn, self.id, &form)
|
2021-10-18 21:36:44 +00:00
|
|
|
})
|
|
|
|
.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(),
|
|
|
|
}),
|
2022-06-02 14:33:41 +00:00
|
|
|
public_key: self.get_public_key(),
|
2021-08-12 12:48:09 +00:00
|
|
|
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-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> {
|
2022-10-27 09:24:07 +00:00
|
|
|
let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
|
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
|
2020-05-16 00:23:20 +00:00
|
|
|
|
2021-11-06 17:35:14 +00:00
|
|
|
check_slurs(&person.preferred_username, slur_regex)?;
|
|
|
|
check_slurs_opt(&person.name, slur_regex)?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
verify_domains_match(person.id.inner(), expected_domain)?;
|
|
|
|
check_apub_id_valid_with_strictness(
|
|
|
|
person.id.inner(),
|
|
|
|
false,
|
|
|
|
&local_site_data,
|
|
|
|
context.settings(),
|
|
|
|
)?;
|
|
|
|
|
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> {
|
2022-10-27 09:24:07 +00:00
|
|
|
let apub_id = person.id.inner().to_owned();
|
|
|
|
let instance = blocking(context.pool(), move |conn| {
|
|
|
|
Instance::create_from_actor_id(conn, &apub_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let person_form = PersonInsertForm {
|
2021-11-06 17:35:14 +00:00
|
|
|
name: person.preferred_username,
|
2022-10-27 09:24:07 +00:00
|
|
|
display_name: 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,
|
2022-10-27 09:24:07 +00:00
|
|
|
avatar: person.icon.map(|i| i.url.into()),
|
|
|
|
banner: person.image.map(|i| i.url.into()),
|
2021-11-06 12:37:55 +00:00
|
|
|
published: person.published.map(|u| u.naive_local()),
|
|
|
|
updated: person.updated.map(|u| u.naive_local()),
|
|
|
|
actor_id: Some(person.id.into()),
|
2022-10-27 09:24:07 +00:00
|
|
|
bio: read_from_string_or_source_opt(&person.summary, &None, &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,
|
2022-10-27 09:24:07 +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-10-27 09:24:07 +00:00
|
|
|
shared_inbox_url: person.endpoints.map(|e| e.shared_inbox.into()),
|
|
|
|
matrix_user_id: person.matrix_user_id,
|
|
|
|
instance_id: instance.id,
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
|
|
|
let person = blocking(context.pool(), move |conn| {
|
2022-10-27 09:24:07 +00:00
|
|
|
DbPerson::create(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
|
|
|
}
|
|
|
|
|
|
|
|
fn private_key(&self) -> Option<String> {
|
|
|
|
self.private_key.to_owned()
|
|
|
|
}
|
2022-06-08 15:45:39 +00:00
|
|
|
}
|
2021-10-27 16:03:07 +00:00
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
impl Actor for ApubPerson {
|
|
|
|
fn public_key(&self) -> &str {
|
|
|
|
&self.public_key
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
fn inbox(&self) -> Url {
|
|
|
|
self.inbox_url.clone().into()
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
fn shared_inbox(&self) -> Option<Url> {
|
|
|
|
self.shared_inbox_url.clone().map(|s| s.into())
|
2022-06-02 14:33:41 +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
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
cleanup((person, site), &context);
|
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
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
cleanup((person, site), &context);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) {
|
|
|
|
let conn = &mut context.pool().get().unwrap();
|
|
|
|
DbPerson::delete(conn, data.0.id).unwrap();
|
|
|
|
Site::delete(conn, data.1.id).unwrap();
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|