diff --git a/inc/activities/accept.php b/inc/activities/accept.php new file mode 100644 index 0000000..852541d --- /dev/null +++ b/inc/activities/accept.php @@ -0,0 +1,40 @@ + 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; +} +?> diff --git a/inc/api.php b/inc/api.php index f5fdebd..d4678ff 100644 --- a/inc/api.php +++ b/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[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[a-zA-Z0-9-]+)', array( + 'methods' => 'GET', + 'callback' => __NAMESPACE__ . '\get_following', + ) ); } ?> diff --git a/inc/following.php b/inc/following.php index 28ac075..7a0a43c 100644 --- a/inc/following.php +++ b/inc/following.php @@ -1,17 +1,59 @@ 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; +} ?> diff --git a/inc/inbox.php b/inc/inbox.php index 0b70a40..3baff52 100644 --- a/inc/inbox.php +++ b/inc/inbox.php @@ -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;