2020-11-24 17:53:43 +00:00
|
|
|
use crate::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activities::GetActorType,
|
2022-06-02 14:33:41 +00:00
|
|
|
check_apub_id_valid_with_strictness,
|
2023-07-05 15:08:02 +00:00
|
|
|
local_site_data_cached,
|
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
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
protocol::verification::verify_domains_match,
|
|
|
|
traits::{Actor, Object},
|
2020-11-24 17:53:43 +00:00
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
2023-10-11 14:48:19 +00:00
|
|
|
utils::{generate_outbox_url, local_site_opt_to_slur_regex},
|
2022-11-28 14:29:33 +00:00
|
|
|
};
|
2020-12-18 18:38:32 +00:00
|
|
|
use lemmy_db_schema::{
|
2023-09-09 16:25:03 +00:00
|
|
|
source::{
|
|
|
|
activity::ActorType,
|
|
|
|
person::{Person as DbPerson, PersonInsertForm, PersonUpdateForm},
|
|
|
|
},
|
2022-10-27 09:24:07 +00:00
|
|
|
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,
|
2023-02-16 04:05:14 +00:00
|
|
|
utils::{
|
|
|
|
markdown::markdown_to_html,
|
|
|
|
slurs::{check_slurs, check_slurs_opt},
|
|
|
|
},
|
2020-09-14 15:29:50 +00:00
|
|
|
};
|
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)]
|
2022-11-23 23:40:47 +00:00
|
|
|
pub struct ApubPerson(pub(crate) DbPerson);
|
2021-10-18 21:36:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for ApubPerson {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = Person;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-10-18 21:36:44 +00:00
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2021-10-18 21:36:44 +00:00
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
2023-07-11 13:09:59 +00:00
|
|
|
DbPerson::read_from_apub_id(&mut context.pool(), &object_id.into())
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2021-10-18 21:36:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = PersonUpdateForm {
|
|
|
|
deleted: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
DbPerson::update(&mut context.pool(), self.id, &form).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-24 21:30:27 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, _context: &Data<Self::DataType>) -> 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,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: self.actor_id.clone().into(),
|
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(),
|
2023-10-17 17:25:35 +00:00
|
|
|
published: Some(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(),
|
|
|
|
}),
|
2023-03-21 15:03:05 +00:00
|
|
|
public_key: self.public_key(),
|
2023-10-17 17:25:35 +00:00
|
|
|
updated: self.updated,
|
2021-08-12 12:48:09 +00:00
|
|
|
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,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site_data = local_site_data_cached(&mut context.pool()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
|
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)?;
|
2023-07-05 15:08:02 +00:00
|
|
|
check_apub_id_valid_with_strictness(person.id.inner(), false, context).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
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)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(
|
2021-11-06 17:35:14 +00:00
|
|
|
person: Person,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<ApubPerson, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let instance_id = fetch_instance_actor_for_object(&person.id, context).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-07-26 18:01:15 +00:00
|
|
|
let bio = read_from_string_or_source_opt(&person.summary, &None, &person.source);
|
|
|
|
|
2023-06-05 11:56:53 +00:00
|
|
|
// Some Mastodon users have `name: ""` (empty string), need to convert that to `None`
|
|
|
|
// https://github.com/mastodon/mastodon/issues/25233
|
2023-10-11 14:48:19 +00:00
|
|
|
let display_name = person.name.filter(|n| !n.is_empty());
|
2023-06-05 11:56:53 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let person_form = PersonInsertForm {
|
2023-10-11 14:48:19 +00:00
|
|
|
name: person.preferred_username,
|
2023-06-05 11:56:53 +00:00
|
|
|
display_name,
|
2020-11-09 14:29:36 +00:00
|
|
|
banned: None,
|
2022-01-08 12:37:07 +00:00
|
|
|
ban_expires: None,
|
2022-11-03 13:39:30 +00:00
|
|
|
deleted: Some(false),
|
2022-10-27 09:24:07 +00:00
|
|
|
avatar: person.icon.map(|i| i.url.into()),
|
|
|
|
banner: person.image.map(|i| i.url.into()),
|
2023-08-24 15:27:00 +00:00
|
|
|
published: person.published.map(Into::into),
|
|
|
|
updated: person.updated.map(Into::into),
|
2021-11-06 12:37:55 +00:00
|
|
|
actor_id: Some(person.id.into()),
|
2023-07-26 18:01:15 +00:00
|
|
|
bio,
|
2021-03-10 22:33:55 +00:00
|
|
|
local: 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,
|
2023-03-01 02:36:57 +00:00
|
|
|
instance_id,
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
let person = DbPerson::upsert(&mut context.pool(), &person_form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
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
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
impl Actor for ApubPerson {
|
|
|
|
fn id(&self) -> Url {
|
|
|
|
self.actor_id.inner().clone()
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn public_key_pem(&self) -> &str {
|
|
|
|
&self.public_key
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
|
|
|
self.private_key.clone()
|
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> {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.shared_inbox_url.clone().map(Into::into)
|
2022-06-02 14:33:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 16:25:03 +00:00
|
|
|
impl GetActorType for ApubPerson {
|
|
|
|
fn actor_type(&self) -> ActorType {
|
|
|
|
ActorType::Person
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::fetch::object_id::ObjectId;
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::{source::site::Site, traits::Crud};
|
2023-11-17 03:51:33 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-10-21 17:25:35 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
pub(crate) async fn parse_lemmy_person(
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> LemmyResult<(ApubPerson, ApubSite)> {
|
|
|
|
let site = parse_lemmy_instance(context).await?;
|
|
|
|
let json = file_to_json_object("assets/lemmy/objects/person.json")?;
|
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/u/picard")?;
|
|
|
|
ApubPerson::verify(&json, &url, context).await?;
|
|
|
|
let person = ApubPerson::from_json(json, context).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2023-11-17 03:51:33 +00:00
|
|
|
Ok((person, site))
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-11-01 13:05:20 +00:00
|
|
|
#[serial]
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn test_parse_lemmy_person() -> LemmyResult<()> {
|
|
|
|
let context = init_context().await?;
|
|
|
|
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);
|
2023-11-17 03:51:33 +00:00
|
|
|
assert_eq!(person.bio.as_ref().map(std::string::String::len), Some(39));
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
cleanup((person, site), &context).await?;
|
|
|
|
Ok(())
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-10-21 17:25:35 +00:00
|
|
|
#[serial]
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn test_parse_pleroma_person() -> LemmyResult<()> {
|
|
|
|
let context = init_context().await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
// create and parse a fake pleroma instance actor, to avoid network request during test
|
2023-11-17 03:51:33 +00:00
|
|
|
let mut json: Instance = file_to_json_object("assets/lemmy/objects/instance.json")?;
|
|
|
|
json.id = ObjectId::parse("https://queer.hacktivis.me/")?;
|
|
|
|
let url = Url::parse("https://queer.hacktivis.me/users/lanodan")?;
|
|
|
|
ApubSite::verify(&json, &url, &context).await?;
|
|
|
|
let site = ApubSite::from_json(json, &context).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
let json = file_to_json_object("assets/pleroma/objects/person.json")?;
|
|
|
|
ApubPerson::verify(&json, &url, &context).await?;
|
|
|
|
let person = ApubPerson::from_json(json, &context).await?;
|
2021-10-21 17:25:35 +00:00
|
|
|
|
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);
|
2023-03-21 15:03:05 +00:00
|
|
|
assert_eq!(context.request_count(), 0);
|
2023-11-17 03:51:33 +00:00
|
|
|
assert_eq!(person.bio.as_ref().map(std::string::String::len), Some(873));
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
cleanup((person, site), &context).await?;
|
|
|
|
Ok(())
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn cleanup(data: (ApubPerson, ApubSite), context: &LemmyContext) -> LemmyResult<()> {
|
|
|
|
DbPerson::delete(&mut context.pool(), data.0.id).await?;
|
|
|
|
Site::delete(&mut context.pool(), data.1.id).await?;
|
|
|
|
Ok(())
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|