diff --git a/includes/client/posts.php b/includes/client/posts.php index abcbbe8..383403f 100644 --- a/includes/client/posts.php +++ b/includes/client/posts.php @@ -4,22 +4,22 @@ namespace posts; require_once plugin_dir_path( __FILE__ ) . '../server/activities/create.php'; function handle_post_status_change( $new_status, $old_status, $post ) { - // TODO find a way to get the id of an existing object for the post, if it exists $actor_slug = PTEROTYPE_BLOG_ACTOR_SLUG; $actor_outbox = get_rest_url( null, sprintf( 'pterotype/v1/actor/%s/outbox', $actor_slug ) ); - $post_object = post_to_object( $post ); $activity = null; if ( $new_status == 'publish' && $old_status != 'publish' ) { // Create + $post_object = post_to_object( $post ); $activity = \activities\create\make_create( $actor_slug, $post_object ); } else if ( $new_status == 'publish' && $old_status == 'publish' ) { // Update + $post_object = post_to_object( $post ); $activity = \activities\update\make_update( $actor_slug, $post_object ); } else if ( $new_status != 'publish' && $old_status == 'publish' ) { // Delete - // TODO delete isn't working + $post_object = post_to_object( $post, true ); $activity = \activities\delete\make_delete( $actor_slug, $post_object ); } if ( $activity && ! is_wp_error( $activity ) ) { @@ -35,7 +35,11 @@ function handle_post_status_change( $new_status, $old_status, $post ) { /** Return an object of type Article */ -function post_to_object( $post ) { +function post_to_object( $post, $deleted = false ) { + $permalink = get_permalink( $post ); + if ( $deleted ) { + $permalink = substr( $permalink, 0, -10 ) . '/'; + } $object = array( '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Article', @@ -44,20 +48,20 @@ function post_to_object( $post ) { 'attributedTo' => get_rest_url( null, sprintf( '/pterotype/v1/actor/%s', PTEROTYPE_BLOG_ACTOR_SLUG ) ), - 'url' => get_permalink( $post ), + 'url' => $permalink, 'summary' => $post->post_excerpt, ); - $existing = get_existing_object( $post ); + $existing = get_existing_object( $permalink ); if ( $existing ) { $object['id'] = $existing->activitypub_id; } return $object; } -function get_existing_object( $post ) { +function get_existing_object( $permalink ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_objects WHERE object->"$.url" = %s', get_permalink( $post ) + 'SELECT * FROM pterotype_objects WHERE object->"$.url" = %s', $permalink ) ); } ?> diff --git a/includes/server/activities/create.php b/includes/server/activities/create.php index 27e19ac..87fbcd6 100644 --- a/includes/server/activities/create.php +++ b/includes/server/activities/create.php @@ -96,7 +96,7 @@ function make_create( $actor_slug, $object ) { return $actor; } $activity = array( - '@context' => 'https://www.w3.org/ns/activitystreams', + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Create', 'actor' => $actor, 'object' => $object diff --git a/includes/server/activities/delete.php b/includes/server/activities/delete.php index c70b12b..5bcf905 100644 --- a/includes/server/activities/delete.php +++ b/includes/server/activities/delete.php @@ -13,10 +13,11 @@ function handle_outbox( $actor, $activity ) { ); } $object = $activity['object']; - $res = \objects\delete_object( $object ); - if ( is_wp_error( $res ) ) { - return $res; + $tombstone = \objects\delete_object( $object ); + if ( is_wp_error( $tombstone ) ) { + return $tombstone; } + $activity['object'] = $tombstone; return $activity; } @@ -74,7 +75,7 @@ function make_delete( $actor_slug, $object ) { return $actor; } return array( - '@context' => 'https://www.w3.org/ns/activitystreams', + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Delete', 'actor' => $actor, 'object' => $object diff --git a/includes/server/activities/follow.php b/includes/server/activities/follow.php index 0acc6ae..a84f9f3 100644 --- a/includes/server/activities/follow.php +++ b/includes/server/activities/follow.php @@ -82,7 +82,7 @@ function make_accept( $actor_slug, $follow ) { ); } $accept = array( - '@context' => 'https://www.w3.org/ns/activitystreams', + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Accept', 'actor' => \actors\get_actor_by_slug( $actor_slug ), 'object' => $follow, diff --git a/includes/server/activities/update.php b/includes/server/activities/update.php index 4b406ed..57e4fa1 100644 --- a/includes/server/activities/update.php +++ b/includes/server/activities/update.php @@ -99,7 +99,7 @@ function make_update( $actor_slug, $object ) { return $actor; } return array( - '@context' => 'https://www.w3.org/ns/activitystreams', + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Update', 'actor' => $actor, 'object' => $object diff --git a/includes/server/collections.php b/includes/server/collections.php index 78b2ca8..fa07f81 100644 --- a/includes/server/collections.php +++ b/includes/server/collections.php @@ -3,7 +3,7 @@ namespace collections; function make_ordered_collection( $objects ) { $ordered_collection = array( - '@context' => 'https://www.w3.org/ns/activitystreams', + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'OrderedCollection', 'totalItems' => count( $objects ), 'orderedItems' => $objects diff --git a/includes/server/objects.php b/includes/server/objects.php index e0b33b2..99312cc 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -105,19 +105,7 @@ function upsert_object( $object ) { ); $row = new \stdClass(); $row->id = $wpdb->insert_id; - $activites_res = $wpdb->query( $wpdb->prepare( - ' - UPDATE pterotype_activities - SET activity = JSON_SET(activity, "$.object", %s) - WHERE activity->"$.object.id" = %s; - ', - wp_json_encode( $object ), $object['id'] - ) ); - if ( $activities_res === false ) { - return new \WP_Error( - 'db_error', __( 'Failed to update associated activities', 'pterotype' ) - ); - } + update_referencing_activities( $object ); } if ( !$res ) { return new \WP_Error( @@ -152,6 +140,12 @@ function update_object( $object ) { 'db_error', __( 'Failed to update object row', 'pterotype' ) ); } + update_referencing_activities( $object ); + return $object; +} + +function update_referencing_activities( $object ) { + global $wpdb; $referencing_activities = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM pterotype_activities WHERE activity->"$.object.id" = %s', $object['id'] @@ -163,7 +157,6 @@ function update_object( $object ) { \activities\persist_activity( $activity ); } } - return $object; } function get_object( $id ) { @@ -234,24 +227,13 @@ function delete_object( $object ) { if ( !$res ) { return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) ); } - $res = $wpdb->query( $wpdb->prepare( - ' - UPDATE pterotype_activities - SET activity = JSON_SET(activity, "$.object", %s) - WHERE activity->"$.object.id" = %s; - ', - wp_json_encode( $tombstone ), $object['id'] - ) ); - if ( $res === false ) { - return new \WP_Error( - 'db_error', __( 'Failed to update associated activities', 'pterotype' ) - ); - } + update_referencing_activities( $tombstone ); return $tombstone; } function make_tombstone( $object ) { $tombstone = array( + '@context' => array( 'https://www.w3.org/ns/activitystreams' ), 'type' => 'Tombstone', 'formerType' => $object['type'], 'id' => $object['id'],