diff --git a/includes/server/activities/accept.php b/includes/server/activities/accept.php index 2f2d6ec..ccd29b4 100644 --- a/includes/server/activities/accept.php +++ b/includes/server/activities/accept.php @@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '../following.php'; require_once plugin_dir_path( __FILE__ ) . '../followers.php'; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; +require_once plugin_dir_path( __FILE__ ) . '../../util.php'; function handle_inbox( $actor_slug, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { @@ -14,14 +15,14 @@ function handle_inbox( $actor_slug, $activity ) { array( 'status' => 400 ) ); } - $object = $activity['object']; + $object = \util\dereference_object( $activity['object'] ); if ( array_key_exists( 'type', $object ) ) { switch ( $object['type'] ) { case 'Follow': if ( !array_key_exists( 'object', $object ) ) { break; } - $follow_object = $object['object']; + $follow_object = \util\dereference_object( $object['object'] ); if ( !array_key_exists( 'id', $follow_object ) ) { break; } @@ -47,14 +48,14 @@ function handle_outbox( $actor_slug, $activity ) { array( 'status' => 400 ) ); } - $object = $activity['object']; + $object = \util\dereference_object( $activity['object'] ); if ( array_key_exists( 'type', $object ) ) { switch ( $object['type'] ) { case 'Follow': if ( !array_key_exists( 'actor', $object ) ) { break; } - $follower = $object['actor']; + $follower = \util\dereference_object( $object['actor'] ); \followers\add_follower( $actor_slug, $follower ); break; } diff --git a/includes/server/activities/follow.php b/includes/server/activities/follow.php index a84f9f3..0108353 100644 --- a/includes/server/activities/follow.php +++ b/includes/server/activities/follow.php @@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '../following.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../outbox.php'; +require_once plugin_dir_path( __FILE__ ) . '../../util.php'; function handle_outbox( $actor_slug, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { @@ -36,7 +37,7 @@ function handle_inbox( $actor_slug, $activity ) { array( 'status' => 400 ) ); } - $follower = $activity['actor']; + $follower = \util\dereference_object( $activity['actor'] ); \objects\upsert_object( $follower ); $accept = make_accept( $actor_slug, $activity ); if ( is_wp_error( $accept ) ) { @@ -61,7 +62,7 @@ function actor_is_object( $actor_slug, $activity ) { if ( is_wp_error( $actor ) ) { return false; } - $object = $activity['object']; + $object = \util\dereference_object( $activity['object'] ); if ( !array_key_exists( 'type', $object ) ) { return false; } diff --git a/includes/server/deliver.php b/includes/server/deliver.php index 3013bd9..d86e5eb 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -128,11 +128,12 @@ function post_activity_to_inboxes( $actor_id, $activity, $recipients ) { $server->dispatch( $request ); } else { $args = array( - 'body' => $activity, + 'body' => wp_json_encode( $activity ), 'headers' => array( 'Content-Type' => 'application/ld+json', 'Signature' => get_signing_string( $inbox, $actor_id ), - ) + ), + 'data_format' => 'body', ); wp_remote_post( $inbox, $args ); } diff --git a/includes/server/followers.php b/includes/server/followers.php index c720b11..dd59f5a 100644 --- a/includes/server/followers.php +++ b/includes/server/followers.php @@ -28,13 +28,10 @@ function add_follower( $actor_slug, $follower ) { $row = \objects\upsert_object( $follower ); $object_id = $row->id; } - $wpdb->insert( - $wpdb->prefix . 'pterotype_followers', - array( - 'actor_id' => $actor_id, - 'object_id' => $object_id, - ) - ); + return $wpdb->query( $wpdb->prepare( + "INSERT IGNORE INTO {$wpdb->prefix}pterotype_followers(actor_id, object_id) + VALUES(%d, %d)", $actor_id, $object_id + ) ); } function remove_follower( $actor_slug, $follower ) { @@ -85,14 +82,14 @@ function get_followers_collection( $actor_slug ) { $followers = $wpdb->get_results( $wpdb->prepare( " - SELECT object FROM {$wpdb->prefix}pterotype_followers - JOIN {$wpdb->prefix}pterotype_objects - ON object_id = {$wpdb->prefix}pterotype_objects.id - WHERE actor_id = %d - ", + SELECT object FROM {$wpdb->prefix}pterotype_followers + JOIN {$wpdb->prefix}pterotype_objects + ON object_id = {$wpdb->prefix}pterotype_objects.id + WHERE actor_id = %d + ", $actor_id ), - ARRAY_A + ARRAY_N ); if ( !$followers ) { $followers = array(); diff --git a/includes/server/inbox.php b/includes/server/inbox.php index 5769805..96c71e1 100644 --- a/includes/server/inbox.php +++ b/includes/server/inbox.php @@ -23,6 +23,8 @@ require_once plugin_dir_path( __FILE__ ) . 'activities/undo.php'; require_once plugin_dir_path( __FILE__ ) . '../util.php'; function handle_activity( $actor_slug, $activity ) { + // TODO how should I handle duplicate activities getting posted here and in the outbox? + // Is it okay to just drop them if I already have the activity id in the objects table? // TODO verify the authenticity of the activity $activity = \util\dereference_object( $activity ); if ( !array_key_exists( 'type', $activity ) ) { @@ -137,7 +139,7 @@ function persist_activity( $actor_slug, $activity ) { $actor_id = \actors\get_actor_id( $actor_slug ); $seen_before = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}pterotype_inbox - WHERE actor_id = %d AND activity_id = %d", + WHERE actor_id = %d AND object_id = %d", $actor_id, $activity_id ) ); @@ -148,7 +150,7 @@ function persist_activity( $actor_slug, $activity ) { $wpdb->prefix . 'pterotype_inbox', array( 'actor_id' => $actor_id, - 'activity_id' => $activity_id, + 'object_id' => $activity_id, ), '%d' ); diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 049af94..9dce741 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -77,7 +77,7 @@ function handle_activity( $actor_slug, $activity ) { $activity = \activities\undo\handle_outbox( $actor_slug, $activity ); break; case 'Accept': - $activity = \activities\accept\handle_inbox( $actor_slug, $activity ); + $activity = \activities\accept\handle_outbox( $actor_slug, $activity ); break; // For the other activities, just persist and deliver case 'Reject': diff --git a/includes/util.php b/includes/util.php index 557fdd0..3861007 100644 --- a/includes/util.php +++ b/includes/util.php @@ -36,7 +36,11 @@ function get_object_from_url_helper( $url, $depth ) { if ( is_local_url( $url ) ) { return retrieve_local_object( $url ); } - $response = wp_remote_get( $url ); + $response = wp_remote_get( $url, array( + 'headers' => array( + 'Accept' => 'application/ld+json', + ), + ) ); if ( is_wp_error( $response ) ) { return $response; }