From c1c2725b049c95760b87ea486c1521517ff866f6 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Tue, 28 Aug 2018 19:26:18 -0400 Subject: [PATCH] Implement object GET url and hydrate object ids --- inc/activities/create.php | 22 +++------------------- inc/api.php | 10 ++++++++++ inc/init.php | 4 ++-- inc/objects.php | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 inc/objects.php diff --git a/inc/activities/create.php b/inc/activities/create.php index 020dec3..d5920ae 100644 --- a/inc/activities/create.php +++ b/inc/activities/create.php @@ -1,6 +1,8 @@ insert( 'activitypub_objects', array( "object" => wp_json_encode( $object ) ) ); - // TODO hydrate $object["id"] to URL of object using $wpdb->insert_id -} - -function create_object_table() { - global $wpdb; - $wpdb->query( - " - CREATE TABLE IF NOT EXISTS activitypub_objects ( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - object TEXT NOT NULL - ); - " - ); -} ?> diff --git a/inc/api.php b/inc/api.php index 0fcd01d..3cc7405 100644 --- a/inc/api.php +++ b/inc/api.php @@ -3,6 +3,7 @@ namespace api; require_once plugin_dir_path( __FILE__ ) . '/actors.php'; require_once plugin_dir_path( __FILE__ ) . '/outbox.php'; +require_once plugin_dir_path( __FILE__ ) . '/objects.php'; function get_user_actor( $request ) { $handle = $request['handle']; @@ -16,6 +17,11 @@ function post_to_outbox( $request ) { return \outbox\persist_activity( $handle, $activity ); } +function get_object( $request ) { + $id = $request['id']; + return \objects\get_object( $id ); +} + function register_routes() { register_rest_route( 'activitypub/v1', '/actor/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', @@ -25,5 +31,9 @@ function register_routes() { 'methods' => 'POST', 'callback' => __NAMESPACE__ . '\post_to_outbox', ) ); + register_rest_route( 'activitypub/v1', '/object/(?P[0-9]+)', array( + 'methods' => 'GET', + 'callback' => __NAMESPACE__ . '\get_object', + ) ); } ?> diff --git a/inc/init.php b/inc/init.php index a498c54..afc8598 100644 --- a/inc/init.php +++ b/inc/init.php @@ -3,7 +3,7 @@ namespace init; require_once plugin_dir_path( __FILE__ ) . '/outbox.php'; require_once plugin_dir_path( __FILE__ ) . '/api.php'; -require_once plugin_dir_path( __FILE__ ) . '/activities/create.php'; +require_once plugin_dir_path( __FILE__ ) . '/objects.php'; add_action( 'rest_api_init', function() { \api\register_routes(); @@ -11,6 +11,6 @@ add_action( 'rest_api_init', function() { add_action( 'activitypub_init', function() { \outbox\create_outbox_table(); - \activities\create\create_object_table(); + \objects\create_object_table(); } ); ?> diff --git a/inc/objects.php b/inc/objects.php new file mode 100644 index 0000000..4bdcdd1 --- /dev/null +++ b/inc/objects.php @@ -0,0 +1,35 @@ +insert( 'activitypub_objects', array( 'object' => wp_json_encode( $object ) ) ); + $object["id"] = get_object_url( $wpdb->insert_id ); + return $object; +} + +function get_object( $id ) { + global $wpdb; + $object = json_decode( $wpdb->get_var( sprintf( + 'SELECT object FROM activitypub_objects WHERE id = %d', $id + ) ) ); + $object["id"] = get_object_url( $id ); + return $object; +} + +function get_object_url( $id ) { + return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id ); +} + +function create_object_table() { + global $wpdb; + $wpdb->query( + " + CREATE TABLE IF NOT EXISTS activitypub_objects ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + object TEXT NOT NULL + ); + " + ); +} +?>