From cfc1f87b71f4925f7f194623ac118d90c0b49d67 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 29 Aug 2018 16:38:10 -0400 Subject: [PATCH] Store activities in a table --- inc/activities.php | 43 +++++++++++++++++++++++++++++++++++++++++++ inc/init.php | 4 +++- inc/objects.php | 16 +++++++++++----- inc/outbox.php | 37 +++++++++++++++++++------------------ 4 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 inc/activities.php diff --git a/inc/activities.php b/inc/activities.php new file mode 100644 index 0000000..c0a7748 --- /dev/null +++ b/inc/activities.php @@ -0,0 +1,43 @@ +get_var( sprintf( + 'SELECT activity FROM activitypub_activities WHERE id = %d', $id + ) ); + if ( is_null( $activity_json ) ) { + return new \WP_Error( + 'not_found', __( 'Activity not found', 'activitypub' ), array( 'status' => 404 ) + ); + } + $activity = json_decode( $activity_json ); + $activity['id'] = get_activity_url( $id ); + return $activity; +} + +function persist_activity( $activity ) { + global $wpdb; + $wpdb->insert( + 'activitypub_activities', array( 'activity' => wp_json_encode( $activity ) ) + ); + $activity["id"] = get_activity_url( $wpdb->insert_id ); + return $activity; +} + +function get_activity_url( $id ) { + return get_rest_url( null, sprintf( '/activitypub/v1/activity/%d', $id ) ); +} + +function create_activities_table() { + global $wpdb; + $wpdb->query( + " + CREATE TABLE IF NOT EXISTS activitypub_activities ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + activity TEXT NOT NULL + ); + " + ); +} +?> diff --git a/inc/init.php b/inc/init.php index afc8598..dcabbb9 100644 --- a/inc/init.php +++ b/inc/init.php @@ -4,13 +4,15 @@ namespace init; require_once plugin_dir_path( __FILE__ ) . '/outbox.php'; require_once plugin_dir_path( __FILE__ ) . '/api.php'; require_once plugin_dir_path( __FILE__ ) . '/objects.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities.php'; add_action( 'rest_api_init', function() { \api\register_routes(); } ); add_action( 'activitypub_init', function() { - \outbox\create_outbox_table(); + \activities\create_activities_table(); \objects\create_object_table(); + \outbox\create_outbox_table(); } ); ?> diff --git a/inc/objects.php b/inc/objects.php index 4bdcdd1..40a5a1b 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -4,21 +4,27 @@ namespace objects; function persist_object( $object ) { global $wpdb; $wpdb->insert( 'activitypub_objects', array( 'object' => wp_json_encode( $object ) ) ); - $object["id"] = get_object_url( $wpdb->insert_id ); + $object['id'] = get_object_url( $wpdb->insert_id ); return $object; } function get_object( $id ) { global $wpdb; - $object = json_decode( $wpdb->get_var( sprintf( + $object_json = $wpdb->get_var( sprintf( 'SELECT object FROM activitypub_objects WHERE id = %d', $id - ) ) ); - $object["id"] = get_object_url( $id ); + ) ); + if ( is_null( $object_json ) ) { + return new \WP_Error( + 404, __( 'Object not found', 'activitypub' ), array ( 'status' => 404 ) + ); + } + $object = json_decode( $object_json ); + $object['id'] = get_object_url( $id ); return $object; } function get_object_url( $id ) { - return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id ); + return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id ) ); } function create_object_table() { diff --git a/inc/outbox.php b/inc/outbox.php index 4513ed3..3fea279 100644 --- a/inc/outbox.php +++ b/inc/outbox.php @@ -13,34 +13,35 @@ When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must namespace outbox; require_once plugin_dir_path( __FILE__ ) . '/activities/create.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities.php'; function handle_activity( $actor, $activity ) { if ( !array_key_exists( "type", $activity ) ) { - return new WP_Error( + return new \WP_Error( 'invalid_activity', __( 'Invalid activity', 'activitypub' ), array( 'status' => 400 ) ); } - switch ( $activity["type"] ) { - case "Create": + switch ( $activity['type'] ) { + case 'Create': $activity = \activites\create\handle( $actor, $activity ); break; - case "Update": + case 'Update': break; - case "Delete": + case 'Delete': break; - case "Follow": + case 'Follow': break; - case "Add": + case 'Add': break; - case "Remove": + case 'Remove': break; - case "Like": + case 'Like': break; - case "Block": + case 'Block': break; - case "Undo": + case 'Undo': break; default: // handle wrapping object in Create activity @@ -60,16 +61,16 @@ function deliver_activity( $activity ) { function persist_activity( $actor, $activity ) { global $wpdb; - $activity_json = wp_json_encode( $activity ); + $activity = \activities\persist_activity( $activity ); + $activity_id = $wpdb->insert_id; $wpdb->insert( 'activitypub_outbox', array( - "actor" => $actor, - "activity" => $activity_json, + 'actor' => $actor, + 'activity_id' => $activity_id, ) ); - // TODO hydrate $activity["id"] with URL to activity using $wpdb->insert_id - $response = new WP_REST_Response( $activity ); + $response = new WP_REST_Response(); $response->set_status( 201 ); - // TODO set location header of response to created activity URL + $response->header( 'Location', $activity['id'] ); return $response; } @@ -80,7 +81,7 @@ function create_outbox_table() { CREATE TABLE IF NOT EXISTS activitypub_outbox ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, actor VARCHAR(128) NOT NULL, - activity TEXT NOT NULL + activity_id INT UNSIGNED FOREIGN KEY REFERENCES activitypub_activities(id) ); " );