From e8899425f73e9e8264352be96910ad408be9906b Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 19 Sep 2018 07:56:57 -0400 Subject: [PATCH] Hydrate object/activity ids on create --- inc/activities.php | 30 ++++++++++++++++++++++++++++++ inc/activities/create.php | 3 +-- inc/objects.php | 27 ++++++++++++++++++--------- inc/outbox.php | 2 +- 4 files changed, 50 insertions(+), 12 deletions(-) diff --git a/inc/activities.php b/inc/activities.php index 783f661..b786973 100644 --- a/inc/activities.php +++ b/inc/activities.php @@ -56,6 +56,36 @@ function persist_activity( $activity ) { return $activity; } +function create_local_activity( $activity ) { + global $wpdb; + $res = $wpdb->insert( 'activitypub_activities', array( + 'activity' => wp_json_encode( $activity ) + ) ); + if ( !$res ) { + return new \WP_Error( + 'db_error', __( 'Failed to insert activity row', 'activitypub' ) + ); + } + $activity_id = $wpdb->insert_id; + $activity_url = get_rest_url( null, sprintf( '/activitypub/v1/activity/%d', $id ) ); + $activity['id'] = $activity_url; + $res = $wpdb->replace( + 'activitypub_activities', + array( + 'id' => $activity_id, + 'activitypub_id' => $activity_url, + 'activity' => $activity + ), + array( '%d', '%s', '%s' ) + ); + if ( !$res ) { + return new \WP_Error( + 'db_error', __( 'Failed to hydrate activity id', 'activitypub' ) + ); + } + return $activity; +} + function create_activities_table() { global $wpdb; $wpdb->query( diff --git a/inc/activities/create.php b/inc/activities/create.php index b9cf651..5299cea 100644 --- a/inc/activities/create.php +++ b/inc/activities/create.php @@ -23,7 +23,6 @@ function handle_outbox( $actor, $activity ) { ); } if ( !array_key_exists( 'actor', $activity ) ) { - // TODO validate that $activity['actor'] is the URL of the $actor return new \WP_Error( 'invalid_actor', __( 'Expecting a valid actor', 'activitypub' ) ); @@ -33,7 +32,7 @@ function handle_outbox( $actor, $activity ) { $object['attributedTo'] = $attributed_actor; reconcile_receivers( $object, $activity ); $object = scrub_object( $object ); - $object = \objects\create_object( $object ); + $object = \objects\create_local_object( $object ); if ( is_wp_error( $object ) ) { return $object; } diff --git a/inc/objects.php b/inc/objects.php index 597d034..f2e7fb1 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -4,17 +4,9 @@ namespace objects; // TODO for 'post' objects, store a post id instead of the full post text, // then hydrate the text on read -function create_object( $object ) { +function create_local_object( $object ) { global $wpdb; - if ( !array_key_exists( 'id', $object ) ) { - return new \WP_Error( - 'invalid_object', - __( 'Object must have an "id" field', 'activitypub' ), - array( 'status' => 400 ) - ); - } $res = $wpdb->insert( 'activitypub_objects', array( - 'activitypub_id' => $object['id'], 'object' => wp_json_encode( $object ) ) ); if ( !$res ) { @@ -22,6 +14,23 @@ function create_object( $object ) { 'db_error', __( 'Failed to insert object row', 'activitypub' ) ); } + $object_id = $wpdb->insert_id; + $object_url = get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $object_id ) ); + $object['id'] = $object_url; + $res = $wpdb->replace( + 'activitypub_objects', + array ( + 'id' => $object_id, + 'activitypub_id' => $object_url, + 'object' => wp_json_encode( $object ) + ), + array( '%d', '%s', '%s' ) + ); + if ( !$res ) { + return new \WP_Error( + 'db_error', __( 'Failed to hydrate object id', 'activitypub' ) + ); + } return $object; } diff --git a/inc/outbox.php b/inc/outbox.php index d3e0e2d..1cbf8b9 100644 --- a/inc/outbox.php +++ b/inc/outbox.php @@ -117,7 +117,7 @@ function deliver_activity( $activity ) { function persist_activity( $actor_slug, $activity ) { global $wpdb; - $activity = \activities\persist_activity( $activity ); + $activity = \activities\create_local_activity( $activity ); $activity_id = $wpdb->insert_id; $actor_id = \actors\get_actor_id( $actor_slug ); $wpdb->insert( 'activitypub_outbox', array(