structural refactor and codebase improvements
This commit is contained in:
62
crates/domain/src/value_objects/instance.rs
Normal file
62
crates/domain/src/value_objects/instance.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use super::{SocialIdentity, UserId};
|
||||
|
||||
/// This instance's own identity on the network. Owns every URL and handle
|
||||
/// derivation that used to be spelled with a bare `base_url: &str`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct InstanceIdentity {
|
||||
base_url: String,
|
||||
}
|
||||
|
||||
impl InstanceIdentity {
|
||||
/// Normalizes away a trailing slash so every derived URL has exactly one.
|
||||
pub fn new(base_url: impl Into<String>) -> Self {
|
||||
Self {
|
||||
base_url: base_url.into().trim_end_matches('/').to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn base_url(&self) -> &str {
|
||||
&self.base_url
|
||||
}
|
||||
|
||||
/// Host and port, as it appears in a fediverse handle.
|
||||
pub fn host(&self) -> &str {
|
||||
self.base_url
|
||||
.split("://")
|
||||
.nth(1)
|
||||
.and_then(|s| s.split('/').next())
|
||||
.unwrap_or("localhost")
|
||||
}
|
||||
|
||||
pub fn actor_url_for(&self, user: &UserId) -> String {
|
||||
format!("{}/users/{}", self.base_url, user.value())
|
||||
}
|
||||
|
||||
pub fn handle_for(&self, username: &str) -> String {
|
||||
format!("@{}@{}", username, self.host())
|
||||
}
|
||||
|
||||
pub fn image_url_for(&self, path: &str) -> String {
|
||||
format!("{}/images/{}", self.base_url, path)
|
||||
}
|
||||
|
||||
pub fn actor_url_of(&self, identity: &SocialIdentity) -> String {
|
||||
match identity {
|
||||
SocialIdentity::Local(uid) => self.actor_url_for(uid),
|
||||
SocialIdentity::Remote { actor_url } => actor_url.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inverse of `actor_url_of`: decides whether an actor URL is ours.
|
||||
pub fn identify(&self, actor_url: &str) -> SocialIdentity {
|
||||
let prefix = format!("{}/users/", self.base_url);
|
||||
if let Some(uuid_str) = actor_url.strip_prefix(&prefix)
|
||||
&& let Ok(uuid) = uuid::Uuid::parse_str(uuid_str)
|
||||
{
|
||||
return SocialIdentity::Local(UserId::from_uuid(uuid));
|
||||
}
|
||||
SocialIdentity::Remote {
|
||||
actor_url: actor_url.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
mod ids;
|
||||
mod instance;
|
||||
mod movie;
|
||||
mod review;
|
||||
mod social;
|
||||
mod user;
|
||||
|
||||
pub use ids::*;
|
||||
pub use instance::*;
|
||||
pub use movie::*;
|
||||
pub use review::*;
|
||||
pub use social::*;
|
||||
|
||||
@@ -7,18 +7,6 @@ pub enum SocialIdentity {
|
||||
}
|
||||
|
||||
impl SocialIdentity {
|
||||
pub fn from_actor_url(actor_url: &str, base_url: &str) -> Self {
|
||||
let prefix = format!("{}/users/", base_url);
|
||||
if let Some(uuid_str) = actor_url.strip_prefix(&prefix)
|
||||
&& let Ok(uuid) = uuid::Uuid::parse_str(uuid_str)
|
||||
{
|
||||
return Self::Local(UserId::from_uuid(uuid));
|
||||
}
|
||||
Self::Remote {
|
||||
actor_url: actor_url.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_local(&self) -> bool {
|
||||
matches!(self, Self::Local(_))
|
||||
}
|
||||
@@ -26,19 +14,6 @@ impl SocialIdentity {
|
||||
pub fn is_remote(&self) -> bool {
|
||||
matches!(self, Self::Remote { .. })
|
||||
}
|
||||
|
||||
pub fn format_local_handle(username: &str, base_url: &str) -> String {
|
||||
let host = Self::host_from_base_url(base_url);
|
||||
format!("@{}@{}", username, host)
|
||||
}
|
||||
|
||||
pub fn host_from_base_url(base_url: &str) -> &str {
|
||||
base_url
|
||||
.split("://")
|
||||
.nth(1)
|
||||
.and_then(|s| s.split('/').next())
|
||||
.unwrap_or("localhost")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -61,3 +36,11 @@ pub struct SocialActor {
|
||||
pub display_name: Option<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
}
|
||||
|
||||
/// Both directions of the follow edge between a viewer and a target.
|
||||
/// `None` means no edge at all; `Some(Pending)` means requested but not accepted.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
pub struct FollowRelation {
|
||||
pub following: Option<FollowStatus>,
|
||||
pub followed_by: Option<FollowStatus>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user