2022-02-07 19:23:12 +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,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2022-05-10 17:08:13 +00:00
|
|
|
objects::read_from_string_or_source_opt,
|
2022-04-12 17:10:35 +00:00
|
|
|
protocol::{
|
2022-10-06 18:27:58 +00:00
|
|
|
objects::{
|
|
|
|
instance::{Instance, InstanceType},
|
|
|
|
LanguageTag,
|
|
|
|
},
|
2022-04-12 17:10:35 +00:00
|
|
|
ImageObject,
|
|
|
|
Source,
|
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2022-06-08 15:45:39 +00:00
|
|
|
core::object_id::ObjectId,
|
2022-06-02 14:33:41 +00:00
|
|
|
deser::values::MediaTypeHtml,
|
2022-06-08 15:45:39 +00:00
|
|
|
traits::{Actor, ApubObject},
|
2022-06-02 14:33:41 +00:00
|
|
|
utils::verify_domains_match,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
use chrono::NaiveDateTime;
|
2022-11-09 10:05:00 +00:00
|
|
|
use lemmy_api_common::utils::local_site_opt_to_slur_regex;
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
|
|
|
actor_language::SiteLanguage,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance::Instance as DbInstance,
|
|
|
|
site::{Site, SiteInsertForm},
|
2022-10-06 18:27:58 +00:00
|
|
|
},
|
2022-10-27 09:24:07 +00:00
|
|
|
traits::Crud,
|
2022-06-08 15:45:39 +00:00
|
|
|
utils::{naive_now, DbPool},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
use lemmy_utils::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
2022-02-07 19:23:12 +00:00
|
|
|
utils::{check_slurs, check_slurs_opt, convert_datetime, markdown_to_html},
|
|
|
|
};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use tracing::debug;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ApubSite(Site);
|
|
|
|
|
|
|
|
impl Deref for ApubSite {
|
|
|
|
type Target = Site;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Site> for ApubSite {
|
|
|
|
fn from(s: Site) -> Self {
|
2022-03-30 14:58:03 +00:00
|
|
|
ApubSite(s)
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ApubObject for ApubSite {
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
type ApubType = Instance;
|
2022-03-23 21:27:51 +00:00
|
|
|
type DbType = Site;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn read_from_apub_id(
|
|
|
|
object_id: Url,
|
|
|
|
data: &Self::DataType,
|
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
2022-11-09 10:05:00 +00:00
|
|
|
Site::read_from_apub_id(data.pool(), object_id)
|
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2022-02-07 19:23:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete(self, _data: &Self::DataType) -> Result<(), LemmyError> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2022-10-06 18:27:58 +00:00
|
|
|
async fn into_apub(self, data: &Self::DataType) -> Result<Self::ApubType, LemmyError> {
|
|
|
|
let site_id = self.id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let langs = SiteLanguage::read(data.pool(), site_id).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
let language = LanguageTag::new_multiple(langs, data.pool()).await?;
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
let instance = Instance {
|
2022-04-12 17:10:35 +00:00
|
|
|
kind: InstanceType::Service,
|
2022-02-07 19:23:12 +00:00
|
|
|
id: ObjectId::new(self.actor_id()),
|
|
|
|
name: self.name.clone(),
|
|
|
|
content: self.sidebar.as_ref().map(|d| markdown_to_html(d)),
|
2022-04-01 18:25:19 +00:00
|
|
|
source: self.sidebar.clone().map(Source::new),
|
2022-02-07 19:23:12 +00:00
|
|
|
summary: self.description.clone(),
|
|
|
|
media_type: self.sidebar.as_ref().map(|_| MediaTypeHtml::Html),
|
|
|
|
icon: self.icon.clone().map(ImageObject::new),
|
|
|
|
image: self.banner.clone().map(ImageObject::new),
|
|
|
|
inbox: self.inbox_url.clone().into(),
|
|
|
|
outbox: Url::parse(&format!("{}/site_outbox", self.actor_id))?,
|
2022-06-02 14:33:41 +00:00
|
|
|
public_key: self.get_public_key(),
|
2022-10-06 18:27:58 +00:00
|
|
|
language,
|
2022-02-07 19:23:12 +00:00
|
|
|
published: convert_datetime(self.published),
|
|
|
|
updated: self.updated.map(convert_datetime),
|
|
|
|
};
|
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn verify(
|
|
|
|
apub: &Self::ApubType,
|
|
|
|
expected_domain: &Url,
|
|
|
|
data: &Self::DataType,
|
|
|
|
_request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site_data = fetch_local_site_data(data.pool()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
check_apub_id_valid_with_strictness(apub.id.inner(), true, &local_site_data, data.settings())?;
|
2022-02-07 19:23:12 +00:00
|
|
|
verify_domains_match(expected_domain, apub.id.inner())?;
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
check_slurs(&apub.name, slur_regex)?;
|
|
|
|
check_slurs_opt(&apub.summary, slur_regex)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn from_apub(
|
|
|
|
apub: Self::ApubType,
|
|
|
|
data: &Self::DataType,
|
|
|
|
_request_counter: &mut i32,
|
|
|
|
) -> Result<Self, LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
let apub_id = apub.id.inner().clone();
|
2022-11-09 10:05:00 +00:00
|
|
|
let instance = DbInstance::create_from_actor_id(data.pool(), &apub_id).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let site_form = SiteInsertForm {
|
2022-02-07 19:23:12 +00:00
|
|
|
name: apub.name.clone(),
|
2022-10-27 09:24:07 +00:00
|
|
|
sidebar: read_from_string_or_source_opt(&apub.content, &None, &apub.source),
|
2022-02-07 19:23:12 +00:00
|
|
|
updated: apub.updated.map(|u| u.clone().naive_local()),
|
2022-10-27 09:24:07 +00:00
|
|
|
icon: apub.icon.clone().map(|i| i.url.into()),
|
|
|
|
banner: apub.image.clone().map(|i| i.url.into()),
|
|
|
|
description: apub.summary.clone(),
|
2022-02-07 19:23:12 +00:00
|
|
|
actor_id: Some(apub.id.clone().into()),
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
inbox_url: Some(apub.inbox.clone().into()),
|
|
|
|
public_key: Some(apub.public_key.public_key_pem.clone()),
|
2022-10-27 09:24:07 +00:00
|
|
|
private_key: None,
|
|
|
|
instance_id: instance.id,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2022-10-06 18:27:58 +00:00
|
|
|
let languages = LanguageTag::to_language_id_multiple(apub.language, data.pool()).await?;
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let site = Site::create(data.pool(), &site_form).await?;
|
|
|
|
SiteLanguage::update(data.pool(), languages, &site).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(site.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActorType for ApubSite {
|
|
|
|
fn actor_id(&self) -> Url {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.actor_id.clone().into()
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
fn private_key(&self) -> Option<String> {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.private_key.clone()
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
impl Actor for ApubSite {
|
2022-06-02 14:33:41 +00:00
|
|
|
fn public_key(&self) -> &str {
|
|
|
|
&self.public_key
|
|
|
|
}
|
2022-06-08 15:45:39 +00:00
|
|
|
|
|
|
|
fn inbox(&self) -> Url {
|
|
|
|
self.inbox_url.clone().into()
|
|
|
|
}
|
2022-06-02 14:33:41 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
/// Instance actor is at the root path, so we simply need to clear the path and other unnecessary
|
|
|
|
/// parts of the url.
|
|
|
|
pub fn instance_actor_id_from_url(mut url: Url) -> Url {
|
|
|
|
url.set_fragment(None);
|
|
|
|
url.set_path("");
|
|
|
|
url.set_query(None);
|
|
|
|
url
|
|
|
|
}
|
|
|
|
|
|
|
|
/// try to fetch the instance actor (to make things like instance rules available)
|
|
|
|
pub(in crate::objects) async fn fetch_instance_actor_for_object(
|
|
|
|
object_id: Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) {
|
|
|
|
// try to fetch the instance actor (to make things like instance rules available)
|
|
|
|
let instance_id = instance_actor_id_from_url(object_id);
|
|
|
|
let site = ObjectId::<ApubSite>::new(instance_id.clone())
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await;
|
|
|
|
if let Err(e) = site {
|
|
|
|
debug!("Failed to dereference site for {}: {}", instance_id, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
pub(crate) async fn remote_instance_inboxes(pool: &DbPool) -> Result<Vec<Url>, LemmyError> {
|
|
|
|
Ok(
|
2022-11-09 10:05:00 +00:00
|
|
|
Site::read_remote_sites(pool)
|
|
|
|
.await?
|
2022-06-08 15:45:39 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|s| ApubSite::from(s).shared_inbox_or_inbox())
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{objects::tests::init_context, protocol::tests::file_to_json_object};
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::traits::Crud;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
pub(crate) async fn parse_lemmy_instance(context: &LemmyContext) -> ApubSite {
|
|
|
|
let json: Instance = file_to_json_object("assets/lemmy/objects/instance.json").unwrap();
|
|
|
|
let id = Url::parse("https://enterprise.lemmy.ml/").unwrap();
|
|
|
|
let mut request_counter = 0;
|
|
|
|
ApubSite::verify(&json, &id, context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let site = ApubSite::from_apub(json, context, &mut request_counter)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(request_counter, 0);
|
|
|
|
site
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_parse_lemmy_instance() {
|
2022-11-09 10:05:00 +00:00
|
|
|
let context = init_context().await;
|
2022-02-07 19:23:12 +00:00
|
|
|
let site = parse_lemmy_instance(&context).await;
|
|
|
|
|
|
|
|
assert_eq!(site.name, "Enterprise");
|
|
|
|
assert_eq!(site.description.as_ref().unwrap().len(), 15);
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Site::delete(context.pool(), site.id).await.unwrap();
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|