From 02be20b2b31425be6ffabfff973c98e6e18ce9ac Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 20 Sep 2018 18:42:05 -0400 Subject: [PATCH] Update associated activities when objects change; soft delete objects --- inc/objects.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/inc/objects.php b/inc/objects.php index ffd2c5a..61185fb 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -85,6 +85,19 @@ 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' ) + ); + } } if ( !$res ) { return new \WP_Error( @@ -115,6 +128,19 @@ function update_object( $object ) { 'db_error', __( 'Failed to update object row', 'pterotype' ) ); } + $activites_res = $wpdb->query( $wpdb->prepare( + ' + UPDATE pterotype_activities + SET activity = JSON_SET(activity, "$.object", %s) + WHERE activity->"$.object.id" = %s; + ', + $object_json, $object['id'] + ) ); + if ( $activities_res === false ) { + return new \WP_Error( + 'db_error', __( 'Failed to update associated activities', 'pterotype' ) + ); + } return $object; } @@ -153,11 +179,50 @@ function delete_object( $object ) { array( 'status' => 400 ) ); } + if ( !array_key_exists( 'type', $object ) ) { + return new \WP_Error( + 'invalid_object', + __( 'Object must have a "type" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } $activitypub_id = $object['id']; - $res = $wpdb->delete( 'pterotype_objects', array( 'activitypub_id' => $id ), '%s' ); + $tombstone = make_tombstone( $object ); + $res = $wpdb->replace( + 'pterotype_objects', + array( + 'activitypub_id' => $activitypub_id, + 'type' => $tombstone['type'], + 'object' => wp_json_encode( $tombstone ), + ), + ); if ( !$res ) { return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) ); } - return $res; + $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' ) + ); + } + return $tombstone; +} + +function make_tombstone( $object ) { + $tombstone = array( + 'type' => 'Tombstone', + 'formerType' => $object['type'], + 'id' => $object['id'], + 'deleted' => date( \DateTime::ISO8601, time() ), + ); + return $tombstone; } ?> +