Implement followers collection

This commit is contained in:
Jeremy Dormitzer 2018-09-24 08:51:50 -04:00
parent 1f871c53df
commit 6120bdde64
3 changed files with 46 additions and 0 deletions

View File

@ -40,6 +40,11 @@ function get_following( $request ) {
return \following\get_following_collection( $actor_slug );
}
function get_followers( $request ) {
$actor_slug = $request['actor'];
return \followers\get_followers_collection( $actor_slug );
}
function get_likes( $request ) {
$object_id = $request['object'];
return \likes\get_likes_collection( $object_id );
@ -75,6 +80,10 @@ function register_routes() {
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_following',
) );
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)/followers', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_followers',
) );
register_rest_route( 'pterotype/v1', '/object/(?P<object>[0-9]+)/likes', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_likes',

View File

@ -34,4 +34,35 @@ function add_follower( $actor_slug, $follower ) {
);
);
}
function get_followers_collection( $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 )
);
}
$followers = $wpdb->get_results(
$wpdb->prepare(
'
SELECT object FROM pterotype_followers
JOIN pterotype_objects ON object_id = pterotype_objects.id
WHERE actor_id = %d
',
$actor_id
),
ARRAY_A
);
if ( !$followers ) {
$followers = array();
}
$collection = \collections\make_ordered_collection( $followers );
$collection['id'] = get_rest_url( null, sprintf(
'/pterotype/v1/actor/%s/followers', $actor_slug
) );
return $collection;
}
?>

View File

@ -24,7 +24,13 @@ function create_local_object( $object ) {
$object_id = $wpdb->insert_id;
$type = $object['type'];
$object_url = get_rest_url( null, sprintf( '/pterotype/v1/object/%d', $object_id ) );
$object_likes = get_rest_url( null, sprintf( '/pterotype/v1/object/%d/likes', $object_id ) );
$object_shares = get_rest_url(
null, sprintf( '/pterotype/v1/object/%d/shares', $object_id )
);
$object['id'] = $object_url;
$object['likes'] = $object_likes;
$object['shares'] = $object_shares;
$res = $wpdb->replace(
'pterotype_objects',
array (