Implement accept for inbox and the following collection endpoint
This commit is contained in:
parent
3fb017f7dc
commit
a0516187c0
40
inc/activities/accept.php
Normal file
40
inc/activities/accept.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace activities\accept;
|
||||
|
||||
require_once plugin_dir_path( __FILE__ ) . '/../following.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/../actors.php';
|
||||
|
||||
function handle_inbox( $actor_slug, $activity ) {
|
||||
if ( !array_key_exists( 'object', $activity ) ) {
|
||||
return new \WP_Error(
|
||||
'invalid_activity',
|
||||
__( 'Activity must have an "object" field', 'pterotype' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
$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'];
|
||||
if ( !array_key_exists( 'id', $follow_object ) ) {
|
||||
break;
|
||||
}
|
||||
$object_id = \objects\get_object_by_activitypub_id( $follow_object['id'] );
|
||||
if ( is_wp_error( $object_id ) ) {
|
||||
break;
|
||||
}
|
||||
$actor_id = \actors\get_actor_id( $actor_slug );
|
||||
\following\accept_follow( $actor_id, $object_id );
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $activity;
|
||||
}
|
||||
?>
|
10
inc/api.php
10
inc/api.php
@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '/actors.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/outbox.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';
|
||||
|
||||
function get_actor( $request ) {
|
||||
$actor = $request['actor'];
|
||||
@ -32,6 +33,11 @@ function get_activity( $request ) {
|
||||
return \activities\get_activity( $id );
|
||||
}
|
||||
|
||||
function get_following( $request ) {
|
||||
$actor_slug = $request['actor'];
|
||||
return \following\get_following_collection( $actor_slug );
|
||||
}
|
||||
|
||||
function register_routes() {
|
||||
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)/outbox', array(
|
||||
'methods' => 'POST',
|
||||
@ -53,5 +59,9 @@ function register_routes() {
|
||||
'methods' => 'GET',
|
||||
'callback' => __NAMESPACE__ . '\get_activity',
|
||||
) );
|
||||
register_rest_route( 'pterotype/v1', '/actor/(?P<actor>[a-zA-Z0-9-]+)', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => __NAMESPACE__ . '\get_following',
|
||||
) );
|
||||
}
|
||||
?>
|
||||
|
@ -1,17 +1,59 @@
|
||||
<?php
|
||||
namespace following;
|
||||
|
||||
$PENDING = 'PENDING';
|
||||
$FOLLOWING = 'FOLLOWING';
|
||||
require_once plugin_dir_path( __FILE__ ) . '/collections.php';
|
||||
|
||||
define( 'PTEROTYPE_FOLLOW_PENDING', 'PENDING' );
|
||||
define( 'PTEROTYPE_FOLLOW_FOLLOWING', 'FOLLOWING' );
|
||||
|
||||
function request_follow( $actor_id, $object_id ) {
|
||||
global $wpdb;
|
||||
return $wpdb->insert(
|
||||
'pterotype_following',
|
||||
array( 'actor_id' => $actor_id,
|
||||
'object_id' => wp_json_encode( $object ),
|
||||
'state' => $PENDING
|
||||
'object_id' => $object_id,
|
||||
'state' => PTEROTYPE_FOLLOW_PENDING
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
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' )
|
||||
);
|
||||
}
|
||||
|
||||
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 ( !$object ) {
|
||||
$objects = array();
|
||||
}
|
||||
$collection = \collections\make_ordered_collection( $objects );
|
||||
$collection['id'] = get_rest_url( null, sprintf(
|
||||
'/pterotype/v1/actor/%s/following', $actor_slug
|
||||
) );
|
||||
return $collection;
|
||||
}
|
||||
?>
|
||||
|
@ -38,6 +38,7 @@ function handle_activity( $actor_slug, $activity ) {
|
||||
$activity = \activities\follow\handle_inbox( $actor_slug, $activity );
|
||||
break;
|
||||
case 'Accept':
|
||||
$activity = \activities\accept\handle_inbox( $actor_slug, $activity );
|
||||
break;
|
||||
case 'Reject':
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user