From f99c9615b14092d2599ee37b4d124969faa3325b Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 20 Sep 2018 14:45:14 -0400 Subject: [PATCH] Store object/activities as json columns with type at top level Also a better migration API --- inc/activities.php | 22 ++++++++++++++++++++-- inc/migrations.php | 40 +++++++++++++++++++++++++++++++++------- inc/objects.php | 22 ++++++++++++++++++++-- pterotype.php | 2 +- 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/inc/activities.php b/inc/activities.php index 8d12fb6..108e358 100644 --- a/inc/activities.php +++ b/inc/activities.php @@ -48,9 +48,18 @@ function persist_activity( $activity ) { array( 'status' => 400 ) ); } + if ( !array_key_exists( 'type', $activity ) ) { + return new \WP_Error( + 'invalid_activity', + __( 'Activity must have a "type" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } $activitypub_id = $activity['id']; + $type = $activity['type']; $wpdb->replace( 'pterotype_activities', array( 'activitypub_id' => $activitypub_id, + 'type' => $type, 'activity' => wp_json_encode( $activity ) ) ); return $activity; @@ -58,6 +67,14 @@ function persist_activity( $activity ) { function create_local_activity( $activity ) { global $wpdb; + if ( !array_key_exists( 'type', $activity ) ) { + return new \WP_Error( + 'invalid_activity', + __( 'Activity must have a "type" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } + $type = $activity['type']; $res = $wpdb->insert( 'pterotype_activities', array( 'activity' => wp_json_encode( $activity ) ) ); @@ -74,9 +91,10 @@ function create_local_activity( $activity ) { array( 'id' => $activity_id, 'activitypub_id' => $activity_url, - 'activity' => $activity + 'type' => $type, + 'activity' => wp_json_encode( $activity ), ), - array( '%d', '%s', '%s' ) + array( '%d', '%s', '%s', '%s' ) ); if ( !$res ) { return new \WP_Error( diff --git a/inc/migrations.php b/inc/migrations.php index fe79650..ecea4ee 100644 --- a/inc/migrations.php +++ b/inc/migrations.php @@ -4,23 +4,31 @@ Poor man's migration system */ namespace migrations; +$previous_version = get_option( 'pterotype_previously_migrated_version' ); +if ( !$previous_version ) { + $previous_version = '0.0.0'; +} + /* It's okay to add new queries to this function, but don't ever delete queries. */ function run_migrations() { - $previous_version = get_option( 'pterotype_previously_migrated_version' ); - if ( !$previous_version ) { - $previous_version = '0.0.0'; - } + global $previous_version; if ( version_compare( $previous_version, PTEROTYPE_VERSION, '>=' ) ) { return; } - if ( version_compare( $previous_version, '0.0.1', '<' ) ) { - migration_0_0_1(); - } + apply_migration( '0.0.1', 'migration_0_0_1' ); + apply_migration( '0.0.2', 'migration_0_0_2' ); update_option( 'pterotype_previously_migrated_version', PTEROTYPE_VERSION ); } +function apply_migration( $version, $migration_func ) { + global $previous_version; + if ( version_compare( $previous_version, $version, '<' ) ) { + call_user_func( __NAMESPACE__ . '\\' . $migration_func ); + } +} + function migration_0_0_1() { global $wpdb; $wpdb->query( @@ -130,4 +138,22 @@ function migration_0_0_1() { " ); } + +function migration_0_0_2() { + global $wpdb; + $wpdb->query( + " + ALTER TABLE pterotype_objects + MODIFY object JSON NOT NULL, + ADD type VARCHAR(50) NOT NULL; + " + ); + $wpdb->query( + " + ALTER TABLE pterotype_activities + MODIFY activity JSON NOT NULL, + ADD type VARCHAR(50) NOT NULL; + " + ); +} ?> diff --git a/inc/objects.php b/inc/objects.php index a4f6440..ffd2c5a 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -6,6 +6,13 @@ namespace objects; function create_local_object( $object ) { global $wpdb; + if ( !array_key_exists( 'type', $object) ) { + return new \WP_Error( + 'invalid_object', + __( 'Object must have a "type" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } $res = $wpdb->insert( 'pterotype_objects', array( 'object' => wp_json_encode( $object ) ) ); @@ -15,6 +22,7 @@ function create_local_object( $object ) { ); } $object_id = $wpdb->insert_id; + $type = $object['type']; $object_url = get_rest_url( null, sprintf( '/pterotype/v1/object/%d', $object_id ) ); $object['id'] = $object_url; $res = $wpdb->replace( @@ -22,9 +30,10 @@ function create_local_object( $object ) { array ( 'id' => $object_id, 'activitypub_id' => $object_url, + 'type' => $type, 'object' => wp_json_encode( $object ) ), - array( '%d', '%s', '%s' ) + array( '%d', '%s', '%s', '%s' ) ); if ( !$res ) { return new \WP_Error( @@ -43,6 +52,13 @@ function upsert_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 ) + ); + } $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM pterotype_objects WHERE activitypub_url = %s', $object['id'] ) ); @@ -52,6 +68,7 @@ function upsert_object( $object ) { 'pterotype_objects', array( 'activitypub_id' => $object['id'], + 'type' => $object['type'], 'object' => wp_json_encode( $object ) ) ); @@ -61,9 +78,10 @@ function upsert_object( $object ) { array( 'id' => $row->id, 'activitypub_id' => $object['id'], + 'type' => $object['type'], 'object' => wp_json_encode( $object ) ), - array( '%d', '%s', '%s' ) + array( '%d', '%s', '%s', '%s' ) ); $row = new stdClass(); $row->id = $wpdb->insert_id; diff --git a/pterotype.php b/pterotype.php index 93a10f1..c4e911e 100644 --- a/pterotype.php +++ b/pterotype.php @@ -4,7 +4,7 @@ Plugin Name: Pterotype */ require_once plugin_dir_path( __FILE__ ) . 'inc/init.php'; -define( 'PTEROTYPE_VERSION', '0.0.1' ); +define( 'PTEROTYPE_VERSION', '0.0.2' ); function pterotype_init() { do_action( 'pterotype_init' );