From 78049c61bff6a76e118214d674e9f26c16c0d5ed Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 29 Sep 2018 09:37:09 -0400 Subject: [PATCH] Fix posting to outbox --- includes/server/deliver.php | 2 +- includes/server/outbox.php | 25 +++++++++++++++++-------- includes/util.php | 6 +++--- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/includes/server/deliver.php b/includes/server/deliver.php index ac131a3..e7f5853 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -19,7 +19,7 @@ function deliver_activity( $activity ) { $recipients = array_unique( $recipients ); if ( array_key_exists( 'actor', $activity ) ) { $actor = \util\dereference_object( $activity['actor'] ); - $recipients = remove_actor_inbox_from_recipients( $activity['actor'], $recipients ); + $recipients = remove_actor_inbox_from_recipients( $actor, $recipients ); } $activity = \activities\strip_private_fields( $activity ); post_activity_to_inboxes( $activity, $recipients ); diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 3715cea..4b6a792 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -37,12 +37,9 @@ function handle_activity( $actor_slug, $activity ) { array( 'status' => 400 ) ); } - $persisted = persist_activity( $actor_slug, $activity ); - if ( !$persisted ) { - return new \WP_Error( - 'db_error', - __( 'Error persisting activity' ) - ); + $activity = persist_activity( $actor_slug, $activity ); + if ( is_wp_error( $activity ) ) { + return $activity; } switch ( $activity['type'] ) { case 'Create': @@ -116,11 +113,16 @@ function handle_activity( $actor_slug, $activity ) { return $activity; } // the activity may have changed while processing side effects, so persist the new version - \activities\persist_activity( $activity ); + // TODO why was 'id' missing from the activity here? + $activity = \activities\persist_activity( $activity ); + if ( is_wp_error( $activity ) ) { + return $activity; + } deliver_activity( $activity ); $res = new \WP_REST_Response(); $res->set_status(201); $res->header( 'Location', $activity['id'] ); + $res->set_data( $activity ); return $res; } @@ -167,10 +169,17 @@ 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 ); - return $wpdb->insert( 'pterotype_outbox', array( + $res = $wpdb->insert( 'pterotype_outbox', array( 'actor_id' => $actor_id, 'activity_id' => $activity_id, ) ); + if ( !$res ) { + return new \WP_Error( + 'db_error', + __( 'Error inserting outbox row', 'pterotype' ) + ); + } + return $activity; } function wrap_object_in_create( $actor_slug, $object ) { diff --git a/includes/util.php b/includes/util.php index aa6865e..03e1cca 100644 --- a/includes/util.php +++ b/includes/util.php @@ -37,7 +37,7 @@ function get_object_from_url( $url ) { function get_object_from_url_helper( $url, $depth ) { if ( is_local_url( $url ) ) { - return retrieve_local_url( $url ); + return retrieve_local_object( $url ); } $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { @@ -57,9 +57,9 @@ function get_object_from_url_helper( $url, $depth ) { function retrieve_local_object( $url ) { $server = rest_get_server(); - $request = new \WP_REST_Request( 'GET', $url ); + $request = \WP_REST_Request::from_url( $url ); $response = $server->dispatch( $request ); - is ( $response->is_error() ) { + if ( $response->is_error() ) { return $response->as_error(); } else { return $response->get_data();