From 5b80c555d72afd9cd4b81fc8dc9108d6b46f3c05 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Tue, 9 Oct 2018 08:50:21 -0400 Subject: [PATCH] Squash bugs mercilessly --- includes/client/posts.php | 2 +- includes/init.php | 1 + includes/server/activities/create.php | 6 +++--- includes/server/activities/undo.php | 1 - includes/server/activities/update.php | 6 +++--- includes/server/inbox.php | 9 +++++---- includes/server/objects.php | 9 +++++---- includes/server/outbox.php | 9 +++++---- pterotype.php | 2 +- 9 files changed, 24 insertions(+), 21 deletions(-) diff --git a/includes/client/posts.php b/includes/client/posts.php index 34861c7..eeff745 100644 --- a/includes/client/posts.php +++ b/includes/client/posts.php @@ -60,7 +60,7 @@ function post_to_object( $post ) { function get_existing_object( $permalink ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_objects WHERE object->"$.url" = %s', $permalink + "SELECT * FROM {$wpdb->prefix}pterotype_objects WHERE object->\"$.url\" = %s", $permalink ) ); } ?> diff --git a/includes/init.php b/includes/init.php index 3df46ce..dc28418 100644 --- a/includes/init.php +++ b/includes/init.php @@ -17,6 +17,7 @@ add_action( 'user_register', function( $user_id ) { } ); add_action( 'pterotype_init', function() { + \schema\run_migrations(); \actors\initialize_actors(); } ); diff --git a/includes/server/activities/create.php b/includes/server/activities/create.php index 87fbcd6..aeb35ea 100644 --- a/includes/server/activities/create.php +++ b/includes/server/activities/create.php @@ -50,9 +50,9 @@ function handle_inbox( $actor_slug, $activity ) { ); } $object = $activity['object']; - $object = \objects\upsert_object( $object ); - if ( is_wp_error( $object ) ) { - return $object; + $object_row = \objects\upsert_object( $object ); + if ( is_wp_error( $object_row ) ) { + return $object_row; } return $activity; } diff --git a/includes/server/activities/undo.php b/includes/server/activities/undo.php index 776e149..7653ab2 100644 --- a/includes/server/activities/undo.php +++ b/includes/server/activities/undo.php @@ -2,7 +2,6 @@ namespace activities\undo; require_once plugin_dir_path( __FILE__ ) . '../../util.php'; -require_once plugin_dir_path( __FILE__ ) . '../activities.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../likes.php'; diff --git a/includes/server/activities/update.php b/includes/server/activities/update.php index 57e4fa1..ab2140d 100644 --- a/includes/server/activities/update.php +++ b/includes/server/activities/update.php @@ -72,9 +72,9 @@ function handle_inbox( $actor_slug, $activity ) { if ( is_wp_error( $authorized ) ) { return $authorized; } - $object = \objects\upsert_object( $object ); - if ( is_wp_error( $object ) ) { - return $object; + $object_row = \objects\upsert_object( $object ); + if ( is_wp_error( $object_row ) ) { + return $object_row; } return $activity; } diff --git a/includes/server/inbox.php b/includes/server/inbox.php index 8e3e53f..5769805 100644 --- a/includes/server/inbox.php +++ b/includes/server/inbox.php @@ -122,10 +122,11 @@ function references_local_object( $object, $depth ) { function persist_activity( $actor_slug, $activity ) { global $wpdb; - $activity = \objects\upsert_object( $activity ); - if ( is_wp_error( $activity ) ) { - return $activity; + $row = \objects\upsert_object( $activity ); + if ( is_wp_error( $row ) ) { + return $row; } + $activity = $row->object; $activity_id = \objects\get_object_id( $activity['id'] ); if ( !$activity_id ) { return new \WP_Error( @@ -184,7 +185,7 @@ function get_inbox( $actor_slug ) { ), ARRAY_A ); return \collections\make_ordered_collection( array_map( function ( $result ) { - return json_decode( $result['activity'], true ); + return json_decode( $result['object'], true ); }, $results ) ); diff --git a/includes/server/objects.php b/includes/server/objects.php index 3e94b95..9807f05 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -104,11 +104,12 @@ function upsert_object( $object ) { '%s', '%d' ); + $id = $row->id; $row = new \stdClass(); - $row->id = $wpdb->insert_id; + $row->id = $id; update_referencing_activities( $object ); } - if ( !$res ) { + if ( $res === false ) { return new \WP_Error( 'db_error', __( 'Failed to upsert object row', 'pterotype' ) ); @@ -142,7 +143,7 @@ function update_object( $object ) { $wpdb->prefix . 'pterotype_objects', array( 'object' => $object_json, 'type' => $object['type'] ), array( 'activitypub_id' => $object['id'] ), - '%s', '%d' ); + '%s', '%s' ); if ( !$res ) { return new \WP_Error( 'db_error', __( 'Failed to update object row', 'pterotype' ) @@ -162,7 +163,7 @@ function update_referencing_activities( $object ) { ) ); if ( $referencing_activities ) { foreach ( $referencing_activities as $activity_row ) { - $activity = json_decode( $activity_row->activity, true ); + $activity = json_decode( $activity_row->object, true ); $activity['object'] = $object; update_object( $activity ); } diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 078f673..049af94 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -112,10 +112,11 @@ function handle_activity( $actor_slug, $activity ) { return $activity; } // the activity may have changed while processing side effects, so persist the new version - $activity = \objects\upsert_object( $activity ); - if ( is_wp_error( $activity ) ) { - return $activity; + $row = \objects\upsert_object( $activity ); + if ( is_wp_error( $row) ) { + return $row; } + $activity = $row->object; deliver_activity( $actor_slug, $activity ); $res = new \WP_REST_Response(); $res->set_status(201); @@ -150,7 +151,7 @@ function get_outbox( $actor_slug ) { // TODO return PagedCollection if $activites is too big return \collections\make_ordered_collection( array_map( function ( $result) { - return json_decode( $result['activity'], true); + return json_decode( $result['object'], true); }, $results ) ); diff --git a/pterotype.php b/pterotype.php index 56bf6b0..28edcbc 100644 --- a/pterotype.php +++ b/pterotype.php @@ -14,7 +14,7 @@ function pterotype_init() { flush_rewrite_rules(); } -function pterotype_deactive() { +function pterotype_deactivate() { do_action( 'pterotype_deactivate' ); flush_rewrite_rules(); }