From 310fd0384864f88b2008f24c7a468a2579cb4ca8 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 29 Sep 2018 08:57:28 -0400 Subject: [PATCH] Bug squashing in the outbox --- includes/server/outbox.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 5df68b3..3715cea 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -37,9 +37,12 @@ function handle_activity( $actor_slug, $activity ) { array( 'status' => 400 ) ); } - $res = persist_activity( $actor_slug, $activity ); - if ( is_wp_error( $res ) ) { - return $res; + $persisted = persist_activity( $actor_slug, $activity ); + if ( !$persisted ) { + return new \WP_Error( + 'db_error', + __( 'Error persisting activity' ) + ); } switch ( $activity['type'] ) { case 'Create': @@ -112,7 +115,12 @@ function handle_activity( $actor_slug, $activity ) { if ( is_wp_error( $activity ) ) { return $activity; } + // the activity may have changed while processing side effects, so persist the new version + \activities\persist_activity( $activity ); deliver_activity( $activity ); + $res = new \WP_REST_Response(); + $res->set_status(201); + $res->header( 'Location', $activity['id'] ); return $res; } @@ -159,14 +167,10 @@ function persist_activity( $actor_slug, $activity ) { $activity = \activities\create_local_activity( $activity ); $activity_id = $wpdb->insert_id; $actor_id = \actors\get_actor_id( $actor_slug ); - $wpdb->insert( 'pterotype_outbox', array( + return $wpdb->insert( 'pterotype_outbox', array( 'actor_id' => $actor_id, 'activity_id' => $activity_id, ) ); - $response = new \WP_REST_Response(); - $response->set_status( 201 ); - $response->header( 'Location', $activity['id'] ); - return $response; } function wrap_object_in_create( $actor_slug, $object ) {