2022-02-07 19:23:12 +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::read_from_string_or_source_opt,
|
2022-04-12 17:10:35 +00:00
|
|
|
protocol::{
|
2022-12-09 16:21:17 +00:00
|
|
|
objects::{instance::Instance, LanguageTag},
|
2022-04-12 17:10:35 +00:00
|
|
|
ImageObject,
|
|
|
|
Source,
|
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::actor::ApplicationType,
|
|
|
|
protocol::{values::MediaTypeHtml, verification::verify_domains_match},
|
|
|
|
traits::{Actor, Object},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2024-01-25 14:22:11 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
utils::{local_site_opt_to_slur_regex, process_markdown_opt, proxy_image_link_opt_apub},
|
|
|
|
};
|
2022-02-07 19:23:12 +00:00
|
|
|
use lemmy_db_schema::{
|
2023-03-01 02:36:57 +00:00
|
|
|
newtypes::InstanceId,
|
2022-10-06 18:27:58 +00:00
|
|
|
source::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activity::ActorType,
|
2022-10-06 18:27:58 +00:00
|
|
|
actor_language::SiteLanguage,
|
2022-10-27 09:24:07 +00:00
|
|
|
instance::Instance as DbInstance,
|
2024-01-25 14:22:11 +00:00
|
|
|
local_site::LocalSite,
|
2022-10-27 09:24:07 +00:00
|
|
|
site::{Site, SiteInsertForm},
|
2022-10-06 18:27:58 +00:00
|
|
|
},
|
2022-10-27 09:24:07 +00:00
|
|
|
traits::Crud,
|
2023-09-09 16:25:03 +00:00
|
|
|
utils::naive_now,
|
2022-02-07 19:23:12 +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},
|
|
|
|
},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for ApubSite {
|
2022-02-07 19:23:12 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = Instance;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2022-02-07 19:23:12 +00:00
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2022-02-07 19:23:12 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2022-02-07 19:23:12 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
2023-07-11 13:09:59 +00:00
|
|
|
Site::read_from_apub_id(&mut data.pool(), &object_id.into())
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2022-02-07 19:23:12 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, _data: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2022-02-07 19:23:12 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Self::Kind, LemmyError> {
|
2022-10-06 18:27:58 +00:00
|
|
|
let site_id = self.id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let langs = SiteLanguage::read(&mut data.pool(), site_id).await?;
|
|
|
|
let language = LanguageTag::new_multiple(langs, &mut data.pool()).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
let instance = Instance {
|
2022-12-09 16:21:17 +00:00
|
|
|
kind: ApplicationType::Application,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: self.id().into(),
|
2022-02-07 19:23:12 +00:00
|
|
|
name: self.name.clone(),
|
2024-03-08 15:23:15 +00:00
|
|
|
preferred_username: data.domain().to_string(),
|
2022-02-07 19:23:12 +00:00
|
|
|
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))?,
|
2023-03-21 15:03:05 +00:00
|
|
|
public_key: self.public_key(),
|
2022-10-06 18:27:58 +00:00
|
|
|
language,
|
2024-02-16 12:24:35 +00:00
|
|
|
content_warning: self.content_warning.clone(),
|
2023-10-17 17:25:35 +00:00
|
|
|
published: self.published,
|
|
|
|
updated: self.updated,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn verify(
|
2023-03-21 15:03:05 +00:00
|
|
|
apub: &Self::Kind,
|
2022-02-07 19:23:12 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2022-02-07 19:23:12 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2023-07-05 15:08:02 +00:00
|
|
|
check_apub_id_valid_with_strictness(apub.id.inner(), true, data).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
verify_domains_match(expected_domain, apub.id.inner())?;
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site_data = local_site_data_cached(&mut data.pool()).await?;
|
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)?;
|
2023-07-05 15:08:02 +00:00
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2024-01-25 14:22:11 +00:00
|
|
|
async fn from_json(apub: Self::Kind, context: &Data<Self::DataType>) -> Result<Self, LemmyError> {
|
2023-03-01 02:36:57 +00:00
|
|
|
let domain = apub.id.inner().domain().expect("group id has domain");
|
2024-01-25 14:22:11 +00:00
|
|
|
let instance = DbInstance::read_or_create(&mut context.pool(), domain.to_string()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await.ok();
|
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
|
2023-07-26 18:01:15 +00:00
|
|
|
let sidebar = read_from_string_or_source_opt(&apub.content, &None, &apub.source);
|
2024-01-25 14:22:11 +00:00
|
|
|
let sidebar = process_markdown_opt(&sidebar, slur_regex, context).await?;
|
|
|
|
let icon = proxy_image_link_opt_apub(apub.icon.map(|i| i.url), context).await?;
|
|
|
|
let banner = proxy_image_link_opt_apub(apub.image.map(|i| i.url), context).await?;
|
2023-07-26 18:01:15 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
let site_form = SiteInsertForm {
|
2022-02-07 19:23:12 +00:00
|
|
|
name: apub.name.clone(),
|
2023-07-26 18:01:15 +00:00
|
|
|
sidebar,
|
2023-08-24 15:27:00 +00:00
|
|
|
updated: apub.updated,
|
2024-01-25 14:22:11 +00:00
|
|
|
icon,
|
|
|
|
banner,
|
2023-10-11 14:48:19 +00:00
|
|
|
description: apub.summary,
|
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,
|
2024-02-16 12:24:35 +00:00
|
|
|
content_warning: apub.content_warning,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2024-01-25 14:22:11 +00:00
|
|
|
let languages =
|
|
|
|
LanguageTag::to_language_id_multiple(apub.language, &mut context.pool()).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
let site = Site::create(&mut context.pool(), &site_form).await?;
|
|
|
|
SiteLanguage::update(&mut context.pool(), languages, &site).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(site.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
impl Actor for ApubSite {
|
|
|
|
fn id(&self) -> Url {
|
|
|
|
self.actor_id.inner().clone()
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn public_key_pem(&self) -> &str {
|
2022-06-02 14:33:41 +00:00
|
|
|
&self.public_key
|
|
|
|
}
|
2022-06-08 15:45:39 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
|
|
|
self.private_key.clone()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-09-09 16:25:03 +00:00
|
|
|
impl GetActorType for ApubSite {
|
|
|
|
fn actor_type(&self) -> ActorType {
|
|
|
|
ActorType::Site
|
|
|
|
}
|
|
|
|
}
|
2022-06-02 14:33:41 +00:00
|
|
|
|
2023-03-01 02:36:57 +00:00
|
|
|
/// Try to fetch the instance actor (to make things like instance rules available).
|
|
|
|
pub(in crate::objects) async fn fetch_instance_actor_for_object<T: Into<Url> + Clone>(
|
|
|
|
object_id: &T,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2023-03-01 02:36:57 +00:00
|
|
|
) -> Result<InstanceId, LemmyError> {
|
|
|
|
let object_id: Url = object_id.clone().into();
|
2022-11-28 14:29:33 +00:00
|
|
|
let instance_id = Site::instance_actor_id_from_url(object_id);
|
2023-03-21 15:03:05 +00:00
|
|
|
let site = ObjectId::<ApubSite>::from(instance_id.clone())
|
|
|
|
.dereference(context)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await;
|
2023-03-01 02:36:57 +00:00
|
|
|
match site {
|
|
|
|
Ok(s) => Ok(s.instance_id),
|
|
|
|
Err(e) => {
|
|
|
|
// Failed to fetch instance actor, its probably not a lemmy instance
|
|
|
|
debug!("Failed to dereference site for {}: {}", &instance_id, e);
|
|
|
|
let domain = instance_id.domain().expect("has domain");
|
|
|
|
Ok(
|
2023-07-11 13:09:59 +00:00
|
|
|
DbInstance::read_or_create(&mut context.pool(), domain.to_string())
|
2023-03-01 02:36:57 +00:00
|
|
|
.await?
|
|
|
|
.id,
|
|
|
|
)
|
|
|
|
}
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use super::*;
|
2024-01-25 14:22:11 +00:00
|
|
|
use crate::protocol::tests::file_to_json_object;
|
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;
|
2022-02-07 19:23:12 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
pub(crate) async fn parse_lemmy_instance(context: &Data<LemmyContext>) -> LemmyResult<ApubSite> {
|
|
|
|
let json: Instance = file_to_json_object("assets/lemmy/objects/instance.json")?;
|
|
|
|
let id = Url::parse("https://enterprise.lemmy.ml/")?;
|
|
|
|
ApubSite::verify(&json, &id, context).await?;
|
|
|
|
let site = ApubSite::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(site)
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2022-02-07 19:23:12 +00:00
|
|
|
#[serial]
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn test_parse_lemmy_instance() -> LemmyResult<()> {
|
2024-01-25 14:22:11 +00:00
|
|
|
let context = LemmyContext::init_test_context().await;
|
2023-11-17 03:51:33 +00:00
|
|
|
let site = parse_lemmy_instance(&context).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
assert_eq!(site.name, "Enterprise");
|
2023-11-17 03:51:33 +00:00
|
|
|
assert_eq!(
|
|
|
|
site.description.as_ref().map(std::string::String::len),
|
|
|
|
Some(15)
|
|
|
|
);
|
2022-02-07 19:23:12 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
Site::delete(&mut context.pool(), site.id).await?;
|
|
|
|
Ok(())
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|