diff --git a/includes/server/activities.php b/includes/server/activities.php index 82ce6de..189a5b8 100644 --- a/includes/server/activities.php +++ b/includes/server/activities.php @@ -95,7 +95,7 @@ function create_local_activity( $activity ) { ); } $activity_id = $wpdb->insert_id; - $activity_url = get_rest_url( null, sprintf( '/pterotype/v1/activity/%d', $id ) ); + $activity_url = get_rest_url( null, sprintf( '/pterotype/v1/activity/%d', $activity_id ) ); $activity['id'] = $activity_url; $res = $wpdb->replace( 'pterotype_activities', diff --git a/includes/server/activities/reject.php b/includes/server/activities/reject.php index 883a0b6..9acccf7 100644 --- a/includes/server/activities/reject.php +++ b/includes/server/activities/reject.php @@ -10,7 +10,7 @@ function handle_inbox( $actor_slug, $activity ) { return new \WP_Error( 'invalid_activity', __( 'Activity must have an "object" field', 'pterotype' ), - array( 'status' => 400 ); + array( 'status' => 400 ) ); } $object = $activity['object']; diff --git a/includes/server/actors.php b/includes/server/actors.php index 50ba34f..aa573b3 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -22,7 +22,7 @@ function get_actor_by_slug ( $slug ) { function get_actor_id( $slug ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( - 'SELECT slug FROM pterotype_actors WHERE slug = %s', $slug + 'SELECT id FROM pterotype_actors WHERE slug = %s', $slug ) ); } diff --git a/includes/server/api.php b/includes/server/api.php index 1056dcf..a9873c9 100644 --- a/includes/server/api.php +++ b/includes/server/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__ ) . 'inbox.php'; require_once plugin_dir_path( __FILE__ ) . 'objects.php'; require_once plugin_dir_path( __FILE__ ) . 'activities.php'; require_once plugin_dir_path( __FILE__ ) . 'following.php'; @@ -25,6 +26,17 @@ function get_outbox( $request ) { return \outbox\get_outbox( $actor_slug ); } +function post_to_inbox( $request ) { + $actor_slug = $request['actor']; + $activity = json_decode( $request->get_body(), true ); + return \inbox\handle_activity( $actor_slug, $activity ); +} + +function get_inbox( $request ) { + $actor_slug = $request['actor']; + return \inbox\get_inbox( $actor_slug ); +} + function get_object( $request ) { $id = $request['id']; return \objects\get_object( $id ); @@ -60,10 +72,18 @@ function register_routes() { 'methods' => 'POST', 'callback' => __NAMESPACE__ . '\post_to_outbox', ) ); - register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+/outbox', array( - 'methods' => 'POST', + register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+)/outbox', array( + 'methods' => 'GET', 'callback' => __NAMESPACE__ . '\get_outbox', ) ); + register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+)/inbox', array( + 'methods' => 'POST', + 'callback' => __NAMESPACE__ . '\post_to_inbox', + ) ); + register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+)/inbox', array( + 'methods' => 'GET', + 'callback' => __NAMESPACE__ . '\get_inbox', + ) ); register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', 'callback' => __NAMESPACE__ . '\get_actor', diff --git a/includes/server/collections.php b/includes/server/collections.php index 288b7a2..78b2ca8 100644 --- a/includes/server/collections.php +++ b/includes/server/collections.php @@ -8,5 +8,6 @@ function make_ordered_collection( $objects ) { 'totalItems' => count( $objects ), 'orderedItems' => $objects ); + return $ordered_collection; } ?> diff --git a/includes/server/inbox.php b/includes/server/inbox.php index adfd827..7f856de 100644 --- a/includes/server/inbox.php +++ b/includes/server/inbox.php @@ -12,6 +12,7 @@ namespace inbox; require_once plugin_dir_path( __FILE__ ) . 'activities.php'; require_once plugin_dir_path( __FILE__ ) . 'objects.php'; require_once plugin_dir_path( __FILE__ ) . 'deliver.php'; +require_once plugin_dir_path( __FILE__ ) . 'collections.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/create.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/update.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/delete.php'; @@ -65,6 +66,7 @@ function handle_activity( $actor_slug, $activity ) { if ( is_wp_error( $activity ) ) { return $activity; } + $res = new \WP_REST_Response(); return $res; } @@ -142,7 +144,7 @@ function references_local_object( $object, $depth ) { return false; } -function persist_activity( $actory_slug, $activity ) { +function persist_activity( $actor_slug, $activity ) { global $wpdb; $activity = \activities\persist_activity( $activity ); if ( is_wp_error( $activity ) ) { @@ -150,11 +152,44 @@ function persist_activity( $actory_slug, $activity ) { } $activity_id = $wpdb->insert_id; $actor_id = \actors\get_actor_id( $actor_slug ); - $wpdb->insert( 'pterotype_inbox', array( + $res = $wpdb->insert( 'pterotype_inbox', array( 'actor_id' => $actor_id, 'activity_id' => $activity_id, ) ); - $response = new \WP_Rest_Response(); - return $response; + if ( !$res ) { + return new \WP_Error( + 'db_error', + __( 'Error persisting inbox record', 'pterotype' ) + ); + } +} + +function get_inbox( $actor_slug ) { + global $wpdb; + $actor_id = \actors\get_actor_id( $actor_slug ); + if ( !$actor_id ) { + return new \WP_Error( + 'not_found', + __( 'Actor not found', 'pterotype' ), + array( 'status' => 404 ) + ); + } + $results = $wpdb->get_results( $wpdb->prepare( + ' + SELECT pterotype_activities.activity FROM pterotype_inbox + JOIN pterotype_actors + ON pterotype_actors.id = pterotype_inbox.actor_id + JOIN pterotype_activities + ON pterotype_activities.id = pterotype_inbox.activity_id + WHERE pterotype_inbox.actor_id = %d + ', + $actor_id + ), ARRAY_A ); + return \collections\make_ordered_collection( array_map( + function ( $result ) { + return json_decode( $result['activity'], true ); + }, + $results + ) ); } ?> diff --git a/includes/server/outbox.php b/includes/server/outbox.php index a92db79..5df68b3 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -119,21 +119,29 @@ function handle_activity( $actor_slug, $activity ) { function get_outbox( $actor_slug ) { global $wpdb; // TODO what sort of joins should these be? + $actor_id = \actors\get_actor_id( $actor_slug ); + if ( !$actor_id ) { + return new \WP_Error( + 'not_found', + __( 'Actor not found', 'pterotype' ), + array( 'status' => 404 ) + ); + } $results = $wpdb->get_results( $wpdb->prepare( - " + ' SELECT pterotype_activities.activity FROM pterotype_outbox JOIN pterotype_actors ON pterotype_actors.id = pterotype_outbox.actor_id JOIN pterotype_activities ON pterotype_activities.id = pterotype_outbox.activity_id - WHERE pterotype_pterotype_outbox.actor_id = %d - ", + WHERE pterotype_outbox.actor_id = %d + ', $actor_id - ) ); + ), ARRAY_A ); // TODO return PagedCollection if $activites is too big return \collections\make_ordered_collection( array_map( function ( $result) { - return json_decode( $result->activity, true); + return json_decode( $result['activity'], true); }, $results ) ); diff --git a/includes/server/shares.php b/includes/server/shares.php index d03179f..775ccd9 100644 --- a/includes/server/shares.php +++ b/includes/server/shares.php @@ -19,7 +19,7 @@ function get_shares_collection( $object_id ) { global $wpdb; $shares = $wpdb->get_results( $wpdb->prepare( - ' + ' SELECT activity FROM pterotype_shares JOIN pterotype_activities ON announce_id = pterotype_activities.id WHERE object_id = %d