2023-03-16 20:41:29 +00:00
|
|
|
use crate::{config::Data, error::Error, fetch::fetch_object_http, traits::Collection};
|
2023-03-16 01:11:48 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::{
|
|
|
|
fmt::{Debug, Display, Formatter},
|
|
|
|
marker::PhantomData,
|
|
|
|
};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
/// Typed wrapper for Activitypub Collection ID which helps with dereferencing.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(transparent)]
|
|
|
|
pub struct CollectionId<Kind>(Box<Url>, PhantomData<Kind>)
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>;
|
2023-03-16 01:11:48 +00:00
|
|
|
|
|
|
|
impl<Kind> CollectionId<Kind>
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
/// Construct a new CollectionId instance
|
2023-11-22 14:39:53 +00:00
|
|
|
pub fn parse(url: &str) -> Result<Self, url::ParseError> {
|
2023-11-20 15:40:46 +00:00
|
|
|
Ok(Self(Box::new(Url::parse(url)?), PhantomData::<Kind>))
|
2023-03-16 01:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetches collection over HTTP
|
|
|
|
///
|
2023-06-20 09:54:14 +00:00
|
|
|
/// Unlike [ObjectId::dereference](crate::fetch::object_id::ObjectId::dereference) this method doesn't do
|
2023-03-16 01:11:48 +00:00
|
|
|
/// any caching.
|
|
|
|
pub async fn dereference(
|
|
|
|
&self,
|
2023-03-16 20:41:29 +00:00
|
|
|
owner: &<Kind as Collection>::Owner,
|
|
|
|
data: &Data<<Kind as Collection>::DataType>,
|
|
|
|
) -> Result<Kind, <Kind as Collection>::Error>
|
2023-03-16 01:11:48 +00:00
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
<Kind as Collection>::Error: From<Error>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
2023-10-24 08:53:59 +00:00
|
|
|
let res = fetch_object_http(&self.0, data).await?;
|
|
|
|
let redirect_url = &res.url;
|
|
|
|
Kind::verify(&res.object, redirect_url, data).await?;
|
|
|
|
Kind::from_json(res.object, owner, data).await
|
2023-03-16 01:11:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Need to implement clone manually, to avoid requiring Kind to be Clone
|
|
|
|
impl<Kind> Clone for CollectionId<Kind>
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
CollectionId(self.0.clone(), self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Kind> Display for CollectionId<Kind>
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Kind> Debug for CollectionId<Kind>
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<Kind> From<CollectionId<Kind>> for Url
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
fn from(id: CollectionId<Kind>) -> Self {
|
|
|
|
*id.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Kind> From<Url> for CollectionId<Kind>
|
|
|
|
where
|
2023-03-16 20:41:29 +00:00
|
|
|
Kind: Collection + Send + 'static,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
2023-03-16 01:11:48 +00:00
|
|
|
{
|
|
|
|
fn from(url: Url) -> Self {
|
|
|
|
CollectionId(Box::new(url), PhantomData::<Kind>)
|
|
|
|
}
|
|
|
|
}
|
2023-11-13 09:38:58 +00:00
|
|
|
|
|
|
|
impl<Kind> PartialEq for CollectionId<Kind>
|
|
|
|
where
|
|
|
|
Kind: Collection,
|
|
|
|
for<'de2> <Kind as Collection>::Kind: serde::Deserialize<'de2>,
|
|
|
|
{
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0.eq(&other.0) && self.1 == other.1
|
|
|
|
}
|
|
|
|
}
|