From 5f8e96b9be7377dd636145af338bb8eb930db868 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 14 May 2026 10:52:35 +0200 Subject: [PATCH] feat(domain): ActivityPubRepository port with federation vocabulary --- crates/domain/Cargo.toml | 1 + crates/domain/src/ports.rs | 79 ++++++++++++++++++++++++++++++++++++ crates/domain/src/testing.rs | 67 +++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 1 deletion(-) diff --git a/crates/domain/Cargo.toml b/crates/domain/Cargo.toml index 4aea696..1fbcab9 100644 --- a/crates/domain/Cargo.toml +++ b/crates/domain/Cargo.toml @@ -13,6 +13,7 @@ uuid = { workspace = true } chrono = { workspace = true } serde = { workspace = true } futures = { workspace = true } +url = { workspace = true } [dev-dependencies] tokio = { workspace = true, features = ["full"] } diff --git a/crates/domain/src/ports.rs b/crates/domain/src/ports.rs index 111241e..8457d24 100644 --- a/crates/domain/src/ports.rs +++ b/crates/domain/src/ports.rs @@ -154,3 +154,82 @@ pub trait SearchPort: Send + Sync { page: &PageParams, ) -> Result, DomainError>; } + +/// A local thought ready for AP serialization, with the author's username +/// pre-joined so the handler can build AP URLs without a second query. +#[derive(Debug, Clone)] +pub struct OutboxEntry { + pub thought: crate::models::thought::Thought, + pub author_username: Username, +} + +#[async_trait] +pub trait ActivityPubRepository: Send + Sync { + // ── Outbox (local → remote) ────────────────────────────────────── + + /// All public local thoughts for this actor. Used for outbox totals + /// and full-collection delivery. + async fn outbox_entries_for_actor( + &self, + user_id: &UserId, + ) -> Result, DomainError>; + + /// Cursor page of public local thoughts, newest-first, before `before`. + /// Used for OrderedCollectionPage responses. + async fn outbox_page_for_actor( + &self, + user_id: &UserId, + before: Option>, + limit: usize, + ) -> Result, DomainError>; + + // ── Remote actor resolution ────────────────────────────────────── + + /// Find the local UserId for a remote actor by its AP URL. + async fn find_remote_actor_id( + &self, + actor_ap_url: &url::Url, + ) -> Result, DomainError>; + + /// Ensure a remote actor placeholder exists; create one if absent. + /// Idempotent — safe to call multiple times with the same URL. + async fn intern_remote_actor( + &self, + actor_ap_url: &url::Url, + ) -> Result; + + // ── Inbox processing (remote → local) ─────────────────────────── + + /// Persist an incoming remote Note. Idempotent on ap_id. + async fn accept_note( + &self, + ap_id: &url::Url, + author_id: &UserId, + content: &str, + published: chrono::DateTime, + sensitive: bool, + content_warning: Option, + ) -> Result<(), DomainError>; + + /// Apply an Update to a previously accepted remote Note. + async fn apply_note_update( + &self, + ap_id: &url::Url, + new_content: &str, + ) -> Result<(), DomainError>; + + /// Remove a specific remote Note (Delete activity). Only touches + /// remotely-originated thoughts. + async fn retract_note(&self, ap_id: &url::Url) -> Result<(), DomainError>; + + /// Remove all Notes from a remote actor (actor-level Delete/Tombstone). + async fn retract_actor_notes( + &self, + actor_ap_url: &url::Url, + ) -> Result<(), DomainError>; + + // ── Node-level stats ───────────────────────────────────────────── + + /// Total locally-authored thought count for NodeInfo responses. + async fn count_local_notes(&self) -> Result; +} diff --git a/crates/domain/src/testing.rs b/crates/domain/src/testing.rs index d06fc10..e95464b 100644 --- a/crates/domain/src/testing.rs +++ b/crates/domain/src/testing.rs @@ -1,6 +1,7 @@ use std::sync::{Arc, Mutex}; use async_trait::async_trait; use chrono::Utc; +use url; use crate::{ errors::DomainError, events::DomainEvent, @@ -16,7 +17,7 @@ use crate::{ user::User, }, ports::*, - value_objects::{ApiKeyId, Content, Email, NotificationId, ThoughtId, UserId, Username}, + value_objects::{ApiKeyId, Content, Email, NotificationId, PasswordHash, ThoughtId, UserId, Username}, }; #[derive(Default, Clone)] @@ -291,6 +292,48 @@ pub struct TestStore { } } +#[async_trait] impl ActivityPubRepository for TestStore { + async fn outbox_entries_for_actor(&self, _uid: &UserId) -> Result, DomainError> { + Ok(vec![]) + } + async fn outbox_page_for_actor(&self, _uid: &UserId, _before: Option>, _limit: usize) -> Result, DomainError> { + Ok(vec![]) + } + async fn find_remote_actor_id(&self, actor_ap_url: &url::Url) -> Result, DomainError> { + let url = actor_ap_url.to_string(); + Ok(self.users.lock().unwrap().iter() + .find(|u| u.ap_id.as_deref() == Some(&url)) + .map(|u| u.id.clone())) + } + async fn intern_remote_actor(&self, actor_ap_url: &url::Url) -> Result { + if let Some(uid) = self.find_remote_actor_id(actor_ap_url).await? { + return Ok(uid); + } + let uid = UserId::new(); + let handle = actor_ap_url.path().trim_start_matches('/').replace('/', "_"); + let user = crate::models::user::User { + id: uid.clone(), + username: Username::from_trusted(handle.clone()), + email: Email::from_trusted(format!("{}@remote", uid)), + password_hash: PasswordHash("".into()), + display_name: None, bio: None, avatar_url: None, header_url: None, + custom_css: None, local: false, + ap_id: Some(actor_ap_url.to_string()), + inbox_url: None, public_key: None, private_key: None, + created_at: chrono::Utc::now(), updated_at: chrono::Utc::now(), + }; + self.users.lock().unwrap().push(user); + Ok(uid) + } + async fn accept_note(&self, _ap_id: &url::Url, _author_id: &UserId, _content: &str, _published: chrono::DateTime, _sensitive: bool, _content_warning: Option) -> Result<(), DomainError> { Ok(()) } + async fn apply_note_update(&self, _ap_id: &url::Url, _new_content: &str) -> Result<(), DomainError> { Ok(()) } + async fn retract_note(&self, _ap_id: &url::Url) -> Result<(), DomainError> { Ok(()) } + async fn retract_actor_notes(&self, _actor_ap_url: &url::Url) -> Result<(), DomainError> { Ok(()) } + async fn count_local_notes(&self) -> Result { + Ok(self.thoughts.lock().unwrap().iter().filter(|t| t.local).count() as u64) + } +} + #[async_trait] impl EventPublisher for TestStore { async fn publish(&self, event: &DomainEvent) -> Result<(), DomainError> { self.events.lock().unwrap().push(event.clone()); @@ -303,6 +346,28 @@ pub struct NoOpEventPublisher; async fn publish(&self, _e: &DomainEvent) -> Result<(), DomainError> { Ok(()) } } +#[cfg(test)] +mod ap_repo_tests { + use super::*; + use crate::value_objects::UserId; + + #[tokio::test] + async fn test_store_outbox_returns_empty() { + let store = TestStore::default(); + let result = store.outbox_entries_for_actor(&UserId::new()).await.unwrap(); + assert!(result.is_empty()); + } + + #[tokio::test] + async fn test_store_intern_creates_placeholder() { + let store = TestStore::default(); + let url = url::Url::parse("https://example.com/users/alice").unwrap(); + let id1 = store.intern_remote_actor(&url).await.unwrap(); + let id2 = store.intern_remote_actor(&url).await.unwrap(); + assert_eq!(id1, id2, "intern must be idempotent"); + } +} + #[cfg(test)] mod search_tests { use super::*;