magnetar/ext_federation/src/lib.rs

93 lines
2.2 KiB
Rust

use magnetar_common::util::{FediverseTag, ValidName};
use magnetar_host_meta::Xrd;
use magnetar_webfinger::webfinger::WebFinger;
use serde::{Deserialize, Serialize};
use url::{Host, Url};
pub mod client;
pub mod lookup_flow;
/// The *visible* domain of fediverse handles, that gets resolved by WebFinger
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct HostUnmapped(Host);
impl AsRef<Host> for HostUnmapped {
fn as_ref(&self) -> &Host {
&self.0
}
}
/// The real domain of fediverse handles used for federation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct HostMapped(Host);
impl AsRef<Host> for HostMapped {
fn as_ref(&self) -> &Host {
&self.0
}
}
#[async_trait::async_trait]
pub trait HostMetaResolverService: Send + Sync {
type Error;
async fn resolve(&self, host: &HostUnmapped) -> Result<Xrd, Self::Error>;
}
#[async_trait::async_trait]
pub trait WebFingerResolverService: Send + Sync {
type Error;
async fn resolve_url(&self, url: &str) -> Result<WebFinger, Self::Error>;
async fn resolve(
&self,
template_url: &str,
resolved_uri: &str,
) -> Result<WebFinger, Self::Error> {
self.resolve_url(
&template_url.replace(
"{uri}",
&percent_encoding::utf8_percent_encode(
resolved_uri,
percent_encoding::NON_ALPHANUMERIC,
)
.to_string(),
),
)
.await
}
}
#[derive(Debug)]
pub struct UnmappedUser {
pub name: ValidName,
pub host: HostUnmapped,
}
impl From<&UnmappedUser> for FediverseTag {
fn from(user: &UnmappedUser) -> Self {
FediverseTag {
name: user.name.clone(),
host: Some(user.host.as_ref().clone()),
}
}
}
#[derive(Debug)]
pub struct MappedUser {
pub host_meta: Option<Xrd>,
pub webfinger: WebFinger,
pub tag_mapped: FediverseTag,
pub ap_url: Url,
}
#[async_trait::async_trait]
pub trait FederationLookupService {
type Error;
async fn map_fedi_tag(&self, user: &UnmappedUser) -> Result<MappedUser, Self::Error>;
}