feat: system actor domain obj

This commit is contained in:
Aravinth Manivannan 2024-11-19 21:57:07 +05:30
parent 58073520c0
commit 8773036ec1
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 108 additions and 0 deletions

View file

@ -21,6 +21,7 @@ pub mod follow_activity;
pub mod follow_repository;
pub mod followers;
pub mod remote_actor;
pub mod system_actor;
pub mod visibility;
// aggregates
@ -42,6 +43,8 @@ pub enum SupportedActorType {
Patch,
#[display(fmt = "team")]
Team,
#[display(fmt = "system_actor")]
SystemActor,
}
#[derive(Debug, Display, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
@ -59,6 +62,7 @@ impl FromStr for SupportedActorType {
"ticket" => Ok(SupportedActorType::Ticket),
"patch" => Ok(SupportedActorType::Patch),
"team" => Ok(SupportedActorType::Team),
"system_actor" => Ok(SupportedActorType::SystemActor),
_ => Err(SupportedActorError::UnsupportedActor),
}
}
@ -289,6 +293,7 @@ pub mod tests {
assert_eq!("ticket", SupportedActorType::Ticket.to_string());
assert_eq!("patch", SupportedActorType::Patch.to_string());
assert_eq!("team", SupportedActorType::Team.to_string());
assert_eq!("system_actor", SupportedActorType::SystemActor.to_string());
}
pub fn mock_cachable(times: Option<usize>) -> Box<dyn Cachable> {

View file

@ -0,0 +1,103 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use derive_builder::Builder;
use derive_getters::Getters;
use derive_more::Display;
use mockall::predicate::*;
use mockall::*;
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
#[cfg(test)]
pub use tests::*;
use url::Url;
use super::{Keypair, SupportedActorType, WebFingerable};
pub const SYSTEM_ACTOR_PREFERRED_USERNAME: &str = "system_actor";
#[derive(
Debug, Builder, Clone, Getters, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord,
)]
pub struct SystemActor {
preferred_username: String,
actor_id: Url,
html_url: Url,
#[builder(default)]
keys: Keypair,
}
impl Default for SystemActor {
fn default() -> Self {
Self {
preferred_username: SYSTEM_ACTOR_PREFERRED_USERNAME.into(),
html_url: Url::parse(&format!(
"https://example.com/{SYSTEM_ACTOR_PREFERRED_USERNAME}"
))
.unwrap(),
actor_id: Url::parse(&format!(
"https://forgeflux.example.com/system/{SYSTEM_ACTOR_PREFERRED_USERNAME}"
))
.unwrap(),
keys: Keypair::default(),
}
}
}
impl SystemActor {
pub fn generate_keys(&mut self) {
self.keys.generate()
}
}
impl WebFingerable for SystemActor {
fn username(&self) -> String {
self.preferred_username().into()
}
fn html_page(&self) -> &Url {
self.html_url()
}
fn actor_type(&self) -> SupportedActorType {
SupportedActorType::SystemActor
}
fn profile_photo(&self) -> Option<&Url> {
None
}
}
#[automock]
pub trait GenerateSystemActorRoutes: Send + Sync {
fn generate_actor_id(&self, username: &str) -> Result<Url, url::ParseError>;
fn generate_html_url(&self, username: &str) -> Result<Url, url::ParseError>;
}
pub type FederationGenerateSystemActorRoutes = std::sync::Arc<dyn GenerateSystemActorRoutes>;
#[cfg(test)]
pub mod tests {
use super::*;
pub fn mock_generate_system_actor_routes(
times: Option<usize>,
) -> FederationGenerateSystemActorRoutes {
let mut m = MockGenerateSystemActorRoutes::new();
if let Some(times) = times {
m.expect_generate_actor_id()
.times(times)
.returning(|_| Ok(Url::parse("https://mock_obj_id.example.com").unwrap()));
m.expect_generate_html_url()
.times(times)
.returning(|_| Ok(Url::parse("https://mock_obj_id.example.com").unwrap()));
} else {
m.expect_generate_actor_id()
.returning(|_| Ok(Url::parse("https://mock_obj_id.example.com").unwrap()));
m.expect_generate_html_url()
.returning(|_| Ok(Url::parse("https://mock_obj_id.example.com").unwrap()));
}
std::sync::Arc::new(m)
}
}