From 4f0af179b1d6199613ad1b9c522e2c502af870d0 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 16 Jun 2025 16:23:29 +0200 Subject: [PATCH] separate methods --- src/actix_web/inbox.rs | 64 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/src/actix_web/inbox.rs b/src/actix_web/inbox.rs index da90a7f..354f488 100644 --- a/src/actix_web/inbox.rs +++ b/src/actix_web/inbox.rs @@ -17,7 +17,27 @@ use tracing::debug; /// Handles incoming activities, verifying HTTP signatures and other checks /// /// After successful validation, activities are passed to respective [trait@ActivityHandler]. -pub async fn receive_activity( +pub async fn receive_activity( + request: HttpRequest, + body: Bytes, + data: &Data, +) -> Result::Error> +where + Activity: ActivityHandler + DeserializeOwned + Send + 'static, + ActorT: Object + Actor + Send + 'static, + for<'de2> ::Kind: serde::Deserialize<'de2>, + ::Error: From + From<::Error>, + ::Error: From, + Datatype: Clone, +{ + let (activity, _) = do_stuff::(request, body, data).await?; + + do_more_stuff(activity, data).await +} + +/// Same as [receive_activity], only that it calls the provided hook function before +/// calling activity verify and receive functions. +pub async fn receive_activity_with_hook( request: HttpRequest, body: Bytes, hook: impl FnOnce(Activity, ActorT, Data) -> Fut, @@ -31,6 +51,26 @@ where ::Error: From, Datatype: Clone, Fut: Future::Error>>, +{ + let (activity, actor) = do_stuff::(request, body, data).await?; + + hook(activity.clone(), actor.clone(), data.clone()).await?; + + do_more_stuff(activity, data).await +} + +async fn do_stuff( + request: HttpRequest, + body: Bytes, + data: &Data, +) -> Result<(Activity, ActorT), ::Error> +where + Activity: ActivityHandler + DeserializeOwned + Send + 'static, + ActorT: Object + Actor + Send + 'static, + for<'de2> ::Kind: serde::Deserialize<'de2>, + ::Error: From + From<::Error>, + ::Error: From, + Datatype: Clone, { let digest_header = request .headers() @@ -45,8 +85,17 @@ where let uri = http_compat::uri(request.uri()); verify_signature(&headers, &method, &uri, actor.public_key_pem())?; - hook(activity.clone(), actor.clone(), data.clone()).await?; + Ok((activity, actor)) +} +async fn do_more_stuff( + activity: Activity, + data: &Data, +) -> Result::Error> +where + Activity: ActivityHandler + DeserializeOwned + Send + 'static, + Datatype: Clone, +{ debug!("Receiving activity {}", activity.id().to_string()); activity.verify(data).await?; activity.receive(data).await?; @@ -83,7 +132,7 @@ mod test { #[tokio::test] async fn test_receive_activity_hook() { let (body, incoming_request, config) = setup_receive_test().await; - receive_activity::( + receive_activity_with_hook::( incoming_request.to_http_request(), body, inbox_activity_hook, @@ -94,15 +143,15 @@ mod test { } async fn inbox_activity_hook( - activity: Activity, + _activity: Activity, _actor: ActorT, - context: Data, + _context: Data, ) -> Result<(), Error> { - todo!(); + // TODO: test that this actually gets called + //todo!(); Ok(()) } - /* #[tokio::test] async fn test_receive_activity_invalid_body_signature() { let (_, incoming_request, config) = setup_receive_test().await; @@ -167,7 +216,6 @@ mod test { _ => unreachable!(), } } - */ async fn construct_request(body: &Bytes, actor: &Url) -> TestRequest { let inbox = "https://example.com/inbox";