2020-12-08 17:38:48 +00:00
|
|
|
use crate::{
|
|
|
|
check_is_apub_id_valid,
|
2021-01-12 16:12:41 +00:00
|
|
|
fetcher::{community::get_or_fetch_and_upsert_community, user::get_or_fetch_and_upsert_user},
|
2020-12-08 17:38:48 +00:00
|
|
|
inbox::community_inbox::check_community_or_site_ban,
|
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
use activitystreams::{
|
2020-11-24 17:53:43 +00:00
|
|
|
base::{AsBase, BaseExt, ExtendsExt},
|
2020-10-14 15:34:11 +00:00
|
|
|
markers::Base,
|
2020-11-24 17:53:43 +00:00
|
|
|
mime::{FromStrError, Mime},
|
|
|
|
object::{ApObjectExt, Object, ObjectExt, Tombstone, TombstoneExt},
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
2020-10-14 15:34:11 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
2020-10-12 14:10:09 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-02-10 13:01:02 +00:00
|
|
|
use diesel::result::Error::NotFound;
|
2021-03-01 13:08:41 +00:00
|
|
|
use lemmy_api_structs::blocking;
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{ApubObject, Crud, DbPool};
|
2021-03-02 12:41:48 +00:00
|
|
|
use lemmy_db_schema::{source::community::Community, DbUrl};
|
2021-02-05 13:43:18 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
location_info,
|
2021-03-01 17:24:11 +00:00
|
|
|
settings::structs::Settings,
|
2021-02-05 13:43:18 +00:00
|
|
|
utils::{convert_datetime, markdown_to_html},
|
|
|
|
LemmyError,
|
|
|
|
};
|
2020-12-08 17:38:48 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2020-10-14 15:34:11 +00:00
|
|
|
use url::Url;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-11-16 15:44:04 +00:00
|
|
|
pub(crate) mod comment;
|
|
|
|
pub(crate) mod community;
|
|
|
|
pub(crate) mod post;
|
|
|
|
pub(crate) mod private_message;
|
|
|
|
pub(crate) mod user;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2020-12-08 17:38:48 +00:00
|
|
|
/// Trait for converting an object or actor into the respective ActivityPub type.
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait ToApub {
|
|
|
|
type ApubType;
|
|
|
|
async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
|
|
|
|
fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait FromApub {
|
|
|
|
type ApubType;
|
|
|
|
/// Converts an object from ActivityPub type to Lemmy internal type.
|
|
|
|
///
|
|
|
|
/// * `apub` The object to read from
|
|
|
|
/// * `context` LemmyContext which holds DB pool, HTTP client etc
|
2021-03-12 15:43:01 +00:00
|
|
|
/// * `expected_domain` Domain where the object was received from. None in case of mod action.
|
2021-03-16 17:26:19 +00:00
|
|
|
/// * `is_mod_action` True if the object was sent in a mod activity, ignore `expected_domain` in this case
|
2020-12-08 17:38:48 +00:00
|
|
|
async fn from_apub(
|
|
|
|
apub: &Self::ApubType,
|
|
|
|
context: &LemmyContext,
|
2021-03-16 17:26:19 +00:00
|
|
|
expected_domain: Url,
|
2020-12-08 17:38:48 +00:00
|
|
|
request_counter: &mut i32,
|
2021-03-16 17:26:19 +00:00
|
|
|
is_mod_action: bool,
|
2020-12-08 17:38:48 +00:00
|
|
|
) -> Result<Self, LemmyError>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(in crate::objects) trait FromApubToForm<ApubType> {
|
|
|
|
async fn from_apub(
|
|
|
|
apub: &ApubType,
|
|
|
|
context: &LemmyContext,
|
2021-03-16 17:26:19 +00:00
|
|
|
expected_domain: Url,
|
2020-12-08 17:38:48 +00:00
|
|
|
request_counter: &mut i32,
|
2021-03-16 17:26:19 +00:00
|
|
|
is_mod_action: bool,
|
2020-12-08 17:38:48 +00:00
|
|
|
) -> Result<Self, LemmyError>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
/// Updated is actually the deletion time
|
|
|
|
fn create_tombstone<T>(
|
|
|
|
deleted: bool,
|
2021-01-27 16:42:23 +00:00
|
|
|
object_id: Url,
|
2020-10-12 14:10:09 +00:00
|
|
|
updated: Option<NaiveDateTime>,
|
|
|
|
former_type: T,
|
|
|
|
) -> Result<Tombstone, LemmyError>
|
|
|
|
where
|
|
|
|
T: ToString,
|
|
|
|
{
|
|
|
|
if deleted {
|
|
|
|
if let Some(updated) = updated {
|
|
|
|
let mut tombstone = Tombstone::new();
|
2021-01-27 16:42:23 +00:00
|
|
|
tombstone.set_id(object_id);
|
2020-10-12 14:10:09 +00:00
|
|
|
tombstone.set_former_type(former_type.to_string());
|
|
|
|
tombstone.set_deleted(convert_datetime(updated));
|
|
|
|
Ok(tombstone)
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 15:34:11 +00:00
|
|
|
|
|
|
|
pub(in crate::objects) fn check_object_domain<T, Kind>(
|
|
|
|
apub: &T,
|
2020-12-08 17:38:48 +00:00
|
|
|
expected_domain: Url,
|
2021-03-02 12:41:48 +00:00
|
|
|
) -> Result<DbUrl, LemmyError>
|
2020-10-14 15:34:11 +00:00
|
|
|
where
|
|
|
|
T: Base + AsBase<Kind>,
|
|
|
|
{
|
2020-12-08 17:38:48 +00:00
|
|
|
let domain = expected_domain.domain().context(location_info!())?;
|
|
|
|
let object_id = apub.id(domain)?.context(location_info!())?;
|
2021-01-27 16:42:23 +00:00
|
|
|
check_is_apub_id_valid(object_id)?;
|
|
|
|
Ok(object_id.to_owned().into())
|
2020-10-14 15:34:11 +00:00
|
|
|
}
|
2020-11-24 17:53:43 +00:00
|
|
|
|
|
|
|
pub(in crate::objects) fn set_content_and_source<T, Kind1, Kind2>(
|
|
|
|
object: &mut T,
|
|
|
|
markdown_text: &str,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
2020-11-25 13:07:04 +00:00
|
|
|
T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
|
2020-11-24 17:53:43 +00:00
|
|
|
{
|
|
|
|
let mut source = Object::<()>::new_none_type();
|
|
|
|
source
|
|
|
|
.set_content(markdown_text)
|
|
|
|
.set_media_type(mime_markdown()?);
|
|
|
|
object.set_source(source.into_any_base()?);
|
2020-11-25 13:07:04 +00:00
|
|
|
|
2021-02-05 13:43:18 +00:00
|
|
|
object.set_content(markdown_to_html(markdown_text));
|
|
|
|
object.set_media_type(mime_html()?);
|
2020-11-24 17:53:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::objects) fn get_source_markdown_value<T, Kind1, Kind2>(
|
|
|
|
object: &T,
|
|
|
|
) -> Result<Option<String>, LemmyError>
|
|
|
|
where
|
2020-11-25 13:07:04 +00:00
|
|
|
T: ApObjectExt<Kind1> + ObjectExt<Kind2> + AsBase<Kind2>,
|
2020-11-24 17:53:43 +00:00
|
|
|
{
|
|
|
|
let content = object
|
|
|
|
.content()
|
2021-03-09 13:18:24 +00:00
|
|
|
.map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
|
|
|
|
.flatten();
|
2020-11-24 17:53:43 +00:00
|
|
|
if content.is_some() {
|
2021-02-05 13:43:18 +00:00
|
|
|
let source = object.source().context(location_info!())?;
|
|
|
|
let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
|
|
|
|
check_is_markdown(source.media_type())?;
|
|
|
|
let source_content = source
|
|
|
|
.content()
|
2021-03-09 13:18:24 +00:00
|
|
|
.map(|s| s.as_single_xsd_string().map(|s2| s2.to_string()))
|
2021-02-05 13:43:18 +00:00
|
|
|
.flatten()
|
2021-03-09 13:18:24 +00:00
|
|
|
.context(location_info!())?;
|
2021-02-05 13:43:18 +00:00
|
|
|
return Ok(Some(source_content));
|
2020-11-24 17:53:43 +00:00
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:43:18 +00:00
|
|
|
fn mime_markdown() -> Result<Mime, FromStrError> {
|
2020-11-24 17:53:43 +00:00
|
|
|
"text/markdown".parse()
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:43:18 +00:00
|
|
|
fn mime_html() -> Result<Mime, FromStrError> {
|
|
|
|
"text/html".parse()
|
|
|
|
}
|
|
|
|
|
2020-11-24 17:53:43 +00:00
|
|
|
pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
|
|
|
|
let mime = mime.context(location_info!())?;
|
|
|
|
if !mime.eq(&mime_markdown()?) {
|
|
|
|
Err(LemmyError::from(anyhow!(
|
|
|
|
"Lemmy only supports markdown content"
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 17:38:48 +00:00
|
|
|
|
|
|
|
/// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
|
|
|
|
/// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
|
|
|
|
/// the apub object is parsed, inserted and returned.
|
|
|
|
pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm>(
|
|
|
|
from: &From,
|
|
|
|
context: &LemmyContext,
|
2021-03-16 17:26:19 +00:00
|
|
|
expected_domain: Url,
|
2020-12-08 17:38:48 +00:00
|
|
|
request_counter: &mut i32,
|
2021-03-16 17:26:19 +00:00
|
|
|
is_mod_action: bool,
|
2020-12-08 17:38:48 +00:00
|
|
|
) -> Result<To, LemmyError>
|
|
|
|
where
|
|
|
|
From: BaseExt<Kind>,
|
|
|
|
To: ApubObject<ToForm> + Crud<ToForm> + Send + 'static,
|
|
|
|
ToForm: FromApubToForm<From> + Send + 'static,
|
|
|
|
{
|
|
|
|
let object_id = from.id_unchecked().context(location_info!())?.to_owned();
|
|
|
|
let domain = object_id.domain().context(location_info!())?;
|
|
|
|
|
|
|
|
// if its a local object, return it directly from the database
|
2021-03-01 17:24:11 +00:00
|
|
|
if Settings::get().hostname() == domain {
|
2020-12-08 17:38:48 +00:00
|
|
|
let object = blocking(context.pool(), move |conn| {
|
2021-01-27 16:42:23 +00:00
|
|
|
To::read_from_apub_id(conn, &object_id.into())
|
2020-12-08 17:38:48 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
Ok(object)
|
|
|
|
}
|
|
|
|
// otherwise parse and insert, assuring that it comes from the right domain
|
|
|
|
else {
|
2021-03-16 17:26:19 +00:00
|
|
|
let to_form = ToForm::from_apub(
|
|
|
|
&from,
|
|
|
|
context,
|
|
|
|
expected_domain,
|
|
|
|
request_counter,
|
|
|
|
is_mod_action,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-12-08 17:38:48 +00:00
|
|
|
|
|
|
|
let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??;
|
|
|
|
Ok(to)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
|
|
|
|
object: &T,
|
2021-02-23 18:00:47 +00:00
|
|
|
community_id: i32,
|
2020-12-08 17:38:48 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError>
|
|
|
|
where
|
|
|
|
T: ObjectExt<Kind>,
|
|
|
|
{
|
|
|
|
let user_id = object
|
|
|
|
.attributed_to()
|
|
|
|
.context(location_info!())?
|
|
|
|
.as_single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
|
|
|
let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?;
|
2021-02-23 18:00:47 +00:00
|
|
|
check_community_or_site_ban(&user, community_id, context.pool()).await
|
2021-02-10 13:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::objects) async fn get_to_community<T, Kind>(
|
|
|
|
object: &T,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<Community, LemmyError>
|
|
|
|
where
|
|
|
|
T: ObjectExt<Kind>,
|
|
|
|
{
|
|
|
|
let community_ids = object
|
2020-12-08 17:38:48 +00:00
|
|
|
.to()
|
|
|
|
.context(location_info!())?
|
2021-02-10 13:01:02 +00:00
|
|
|
.as_many()
|
|
|
|
.context(location_info!())?
|
|
|
|
.iter()
|
|
|
|
.map(|a| a.as_xsd_any_uri().context(location_info!()))
|
|
|
|
.collect::<Result<Vec<&Url>, anyhow::Error>>()?;
|
|
|
|
for cid in community_ids {
|
|
|
|
let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await;
|
|
|
|
if community.is_ok() {
|
|
|
|
return community;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(NotFound.into())
|
2020-12-08 17:38:48 +00:00
|
|
|
}
|