feat(worker): handle FetchRemoteActorPosts — fetch and store remote outbox notes
This commit is contained in:
@@ -11,6 +11,8 @@ uuid = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
sha2 = "0.10"
|
||||
hex = "0.4"
|
||||
tracing = { workspace = true }
|
||||
url = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["full"] }
|
||||
|
||||
@@ -2,7 +2,7 @@ use domain::{
|
||||
errors::DomainError,
|
||||
events::DomainEvent,
|
||||
models::thought::{Thought, Visibility},
|
||||
ports::{OutboundFederationPort, ThoughtRepository, UserRepository},
|
||||
ports::{ActivityPubRepository, OutboundFederationPort, ThoughtRepository, UserRepository},
|
||||
value_objects::ThoughtId,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
@@ -12,6 +12,8 @@ pub struct FederationEventService {
|
||||
pub users: Arc<dyn UserRepository>,
|
||||
pub ap: Arc<dyn OutboundFederationPort>,
|
||||
pub base_url: String,
|
||||
pub federation_action: Arc<dyn domain::ports::FederationActionPort>,
|
||||
pub ap_repo: Arc<dyn ActivityPubRepository>,
|
||||
}
|
||||
|
||||
impl FederationEventService {
|
||||
@@ -112,6 +114,49 @@ impl FederationEventService {
|
||||
.await
|
||||
}
|
||||
|
||||
DomainEvent::FetchRemoteActorPosts {
|
||||
actor_ap_url,
|
||||
outbox_url,
|
||||
} => {
|
||||
let notes = match self
|
||||
.federation_action
|
||||
.fetch_outbox_page(outbox_url, 1)
|
||||
.await
|
||||
{
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
tracing::warn!(outbox_url, error = %e, "failed to fetch remote outbox");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let actor_url = url::Url::parse(actor_ap_url)
|
||||
.map_err(|e| DomainError::ExternalService(e.to_string()))?;
|
||||
|
||||
let author_id = self.ap_repo.intern_remote_actor(&actor_url).await?;
|
||||
|
||||
for note in notes {
|
||||
let ap_id = match url::Url::parse(¬e.ap_id) {
|
||||
Ok(u) => u,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let _ = self
|
||||
.ap_repo
|
||||
.accept_note(
|
||||
&ap_id,
|
||||
&author_id,
|
||||
¬e.content,
|
||||
note.published,
|
||||
note.sensitive,
|
||||
note.content_warning,
|
||||
"public",
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
@@ -126,7 +171,7 @@ mod tests {
|
||||
events::DomainEvent,
|
||||
models::thought::{Thought, Visibility},
|
||||
models::user::User,
|
||||
ports::OutboundFederationPort,
|
||||
ports::{ActivityPubRepository, OutboundFederationPort},
|
||||
testing::TestStore,
|
||||
value_objects::*,
|
||||
};
|
||||
@@ -208,6 +253,8 @@ mod tests {
|
||||
users: Arc::new(store.clone()),
|
||||
ap: spy,
|
||||
base_url: "https://example.com".to_string(),
|
||||
federation_action: Arc::new(store.clone()),
|
||||
ap_repo: Arc::new(store.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -531,4 +578,18 @@ mod tests {
|
||||
|
||||
assert!(spy.updated.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fetch_remote_actor_posts_is_noop_when_outbox_empty() {
|
||||
let store = TestStore::default();
|
||||
let spy = Arc::new(SpyPort::default());
|
||||
svc(&store, spy.clone())
|
||||
.process(&DomainEvent::FetchRemoteActorPosts {
|
||||
actor_ap_url: "https://mastodon.social/users/alice".into(),
|
||||
outbox_url: "https://mastodon.social/users/alice/outbox".into(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
// TestStore.fetch_outbox_page returns Ok(vec![]) — no notes, no error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user