pterotype/inc/following.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2018-09-04 06:01:34 +00:00
<?php
namespace following;
require_once plugin_dir_path( __FILE__ ) . '/collections.php';
define( 'PTEROTYPE_FOLLOW_PENDING', 'PENDING' );
define( 'PTEROTYPE_FOLLOW_FOLLOWING', 'FOLLOWING' );
2018-09-04 06:01:34 +00:00
2018-09-19 12:54:07 +00:00
function request_follow( $actor_id, $object_id ) {
2018-09-04 06:01:34 +00:00
global $wpdb;
2018-09-26 03:46:31 +00:00
return $wpdb->replace(
2018-09-19 21:33:20 +00:00
'pterotype_following',
2018-09-26 03:46:31 +00:00
array(
'actor_id' => $actor_id,
'object_id' => $object_id,
'state' => PTEROTYPE_FOLLOW_PENDING
)
2018-09-04 06:01:34 +00:00
);
}
function accept_follow( $actor_id, $object_id ) {
global $wpdb;
return $wpdb->update(
'pterotype_following',
array( 'state' => PTEROTYPE_FOLLOW_FOLLOWING ),
array( 'actor_id' => $actor_id, 'object_id' => $object_id ),
array( '%s', '%d', '%d' )
);
}
2018-09-24 00:30:31 +00:00
function reject_follow( $actor_id, $object_id ) {
global $wpdb;
return $wpdb->delete(
'pterotype_following',
array( 'actor_id' => $actor_id, 'object_id' => $object_id ),
'%d'
);
}
function get_following_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 )
);
}
$objects = $wpdb->get_results(
$wpdb->prepare(
'
SELECT object FROM pterotype_following
JOIN pterotype_objects ON pterotype_following.object_id = pterotype_objects.id
WHERE actor_id = %d
AND state = %s;
',
$actor_id, PTEROTYPE_FOLLOW_FOLLOWING
),
ARRAY_A
);
if ( !$objects ) {
$objects = array();
}
$collection = \collections\make_ordered_collection( $objects );
$collection['id'] = get_rest_url( null, sprintf(
'/pterotype/v1/actor/%s/following', $actor_slug
) );
return $collection;
}
2018-09-04 06:01:34 +00:00
?>