From f7351ae7f3de9dbaa77e4830951eaa10b29662aa Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 15 Sep 2018 17:15:17 -0400 Subject: [PATCH] Differentiate inbox/outbox handle functions --- inc/activities/block.php | 2 +- inc/activities/create.php | 2 +- inc/activities/delete.php | 2 +- inc/activities/follow.php | 2 +- inc/activities/like.php | 2 +- inc/activities/update.php | 2 +- inc/api.php | 4 ++-- inc/outbox.php | 28 +++++++++++++++++++--------- 8 files changed, 27 insertions(+), 17 deletions(-) diff --git a/inc/activities/block.php b/inc/activities/block.php index b6e69d4..3e5fe92 100644 --- a/inc/activities/block.php +++ b/inc/activities/block.php @@ -4,7 +4,7 @@ namespace activities\block; require_once plugin_dir_path( __FILE__ ) . '/../blocks.php'; require_once plugin_dir_path( __FILE__ ) . '/../actors.php'; -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( 'invalid_activity', diff --git a/inc/activities/create.php b/inc/activities/create.php index 7a8e415..daddf10 100644 --- a/inc/activities/create.php +++ b/inc/activities/create.php @@ -11,7 +11,7 @@ to the object and vice-versa. Returns either the modified $activity or a WP_Error. */ -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Create') ) { return new \WP_Error( 'invalid_activity', __( 'Expecting a Create activity', 'activitypub' ) diff --git a/inc/activities/delete.php b/inc/activities/delete.php index 6046a7a..90187d8 100644 --- a/inc/activities/delete.php +++ b/inc/activities/delete.php @@ -3,7 +3,7 @@ namespace activities\delete; require_once plugin_dir_path( __FILE__ ) . '/../objects.php'; -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( 'invalid_activity', diff --git a/inc/activities/follow.php b/inc/activities/follow.php index 8d276eb..85047c0 100644 --- a/inc/activities/follow.php +++ b/inc/activities/follow.php @@ -4,7 +4,7 @@ namespace activities\follow; require_once plugin_dir_path( __FILE__ ) . '/../following.php'; require_once plugin_dir_path( __FILE__ ) . '/../actors.php'; -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( 'invalid_activity', diff --git a/inc/activities/like.php b/inc/activities/like.php index 5c3c0eb..c1c461a 100644 --- a/inc/activities/like.php +++ b/inc/activities/like.php @@ -4,7 +4,7 @@ namespace activities\like; require_once plugin_dir_path( __FILE__ ) . '/../likes.php'; require_once plugin_dir_path( __FILE__ ) . '/../actors.php'; -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( 'invalid_activity', diff --git a/inc/activities/update.php b/inc/activities/update.php index 774003e..d6ddbfc 100644 --- a/inc/activities/update.php +++ b/inc/activities/update.php @@ -3,7 +3,7 @@ namespace activities\update; require_once plugin_dir_path( __FILE__ ) . '/../objects.php'; -function handle( $actor, $activity ) { +function handle_outbox( $actor, $activity ) { if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Update') ) { return new \WP_Error( 'invalid_activity', diff --git a/inc/api.php b/inc/api.php index 59a01b5..766526a 100644 --- a/inc/api.php +++ b/inc/api.php @@ -11,7 +11,7 @@ function get_actor( $request ) { return \actors\get_actor_by_slug( $actor ); } -function handle_activity( $request ) { +function handle_outbox( $request ) { $actor = $request['actor']; $activity = json_decode( $request->get_body(), true ); return \outbox\handle_activity( $actor, $activity ); @@ -30,7 +30,7 @@ function get_activity( $request ) { function register_routes() { register_rest_route( 'activitypub/v1', '/actor/(?P[a-zA-Z0-9-]+)/outbox', array( 'methods' => 'POST', - 'callback' => __NAMESPACE__ . '\handle_activity', + 'callback' => __NAMESPACE__ . '\handle_outbox', ) ); register_rest_route( 'activitypub/v1', '/actor/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', diff --git a/inc/outbox.php b/inc/outbox.php index 7bf600c..3e3fa53 100644 --- a/inc/outbox.php +++ b/inc/outbox.php @@ -32,16 +32,16 @@ function handle_activity( $actor, $activity ) { } switch ( $activity['type'] ) { case 'Create': - $activity = \activities\create\handle( $actor, $activity ); + $activity = \activities\create\handle_outbox( $actor, $activity ); break; case 'Update': - $activity = \activities\update\handle( $actor, $activity ); + $activity = \activities\update\handle_outbox( $actor, $activity ); break; case 'Delete': - $activity = \activities\delete\handle( $actor, $activity ); + $activity = \activities\delete\handle_outbox( $actor, $activity ); break; case 'Follow': - $activity = \activities\follow\handle( $actor, $activity ); + $activity = \activities\follow\handle_outbox( $actor, $activity ); break; case 'Add': return new \WP_Error( @@ -58,10 +58,10 @@ function handle_activity( $actor, $activity ) { ); break; case 'Like': - $activity = \activities\like\handle( $actor, $activity ); + $activity = \activities\like\handle_outbox( $actor, $activity ); break; case 'Block': - $activity = \activities\block\handle( $actor, $activity ); + $activity = \activities\block\handle_outbox( $actor, $activity ); break; case 'Undo': return new \WP_Error( @@ -75,7 +75,7 @@ function handle_activity( $actor, $activity ) { if ( is_wp_error( $create_activity ) ) { return $create_activity; } - $activity = \activities\create\handle( $actor, $create_activity ); + $activity = \activities\create\handle_outbox( $actor, $create_activity ); break; } if ( is_wp_error( $activity ) ) { @@ -86,6 +86,16 @@ function handle_activity( $actor, $activity ) { } } +function get_outbox( $actor_id ) { + global $wpdb; + $activities = $wpdb->get_results( $wpdb->prepare( + " + SELECT * FROM activitypub_outbox WHERE + " + ) ); + // $wpdb->num_rows will hold the number of results, once this implements paging +} + function deliver_activity( $activity ) { \deliver\deliver_activity( $activity ); $activity = \activities\strip_private_fields( $activity ); @@ -127,10 +137,10 @@ function create_outbox_table() { " CREATE TABLE IF NOT EXISTS activitypub_outbox ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - actor VARCHAR(128) NOT NULL, + actor_id UNSIGNED INT NOT NULL, activity_id INT UNSIGNED NOT NULL, FOREIGN KEY activity_fk(activity_id) - REFERENCES activitypub_activities(id) + REFERENCES activitypub_activities(id), ); " );