[WIP] Start fixing all the bugs

It's still very broken
This commit is contained in:
Jeremy Dormitzer 2018-09-27 21:41:30 -04:00
parent 185070df16
commit d30f4006eb
8 changed files with 79 additions and 15 deletions

View File

@ -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',

View File

@ -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'];

View File

@ -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
) );
}

View File

@ -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<actor>[a-zA-Z0-9-]+/outbox', array(
'methods' => 'POST',
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)/outbox', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_outbox',
) );
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)/inbox', array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\post_to_inbox',
) );
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)/inbox', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_inbox',
) );
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_actor',

View File

@ -8,5 +8,6 @@ function make_ordered_collection( $objects ) {
'totalItems' => count( $objects ),
'orderedItems' => $objects
);
return $ordered_collection;
}
?>

View File

@ -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
) );
}
?>

View File

@ -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
) );

View File

@ -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