Add default query validation rules

This commit is contained in:
Matthias Pfefferle 2023-08-29 12:50:17 +02:00
parent 1397d864bb
commit 8f897f5578
7 changed files with 53 additions and 129 deletions

View file

@ -5,6 +5,7 @@ use WP_Error;
use stdClass;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Validator\Query;
use Activitypub\Collection\Users as User_Collection;
use Activitypub\Collection\Followers as Follower_Collection;
@ -36,7 +37,7 @@ class Followers {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'get' ),
'args' => self::request_parameters(),
'args' => Query::get_default_args(),
'permission_callback' => '__return_true',
),
)
@ -108,42 +109,4 @@ class Followers {
return $response;
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['page'] = array(
'type' => 'integer',
'default' => 1,
);
$params['per_page'] = array(
'type' => 'integer',
'default' => 20,
);
$params['order'] = array(
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
);
$params['user_id'] = array(
'required' => true,
'type' => 'string',
);
$params['context'] = array(
'type' => 'string',
'default' => 'simple',
'enum' => array( 'simple', 'full' ),
);
return $params;
}
}

View file

@ -1,6 +1,7 @@
<?php
namespace Activitypub\Rest;
use Activitypub\Validator\Query;
use Activitypub\Collection\Users as User_Collection;
use function Activitypub\get_rest_url_by_path;
@ -31,7 +32,7 @@ class Following {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( self::class, 'get' ),
'args' => self::request_parameters(),
'args' => Query::get_default_args(),
'permission_callback' => '__return_true',
),
)
@ -78,24 +79,4 @@ class Following {
return $response;
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['user_id'] = array(
'required' => true,
'type' => 'string',
);
return $params;
}
}

View file

@ -4,6 +4,7 @@ namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Validator\Query;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection;
@ -59,7 +60,7 @@ class Inbox {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'user_inbox_get' ),
'args' => self::user_inbox_get_parameters(),
'args' => Query::get_default_args(),
'permission_callback' => '__return_true',
),
)
@ -180,26 +181,6 @@ class Inbox {
return new WP_REST_Response( array(), 202 );
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function user_inbox_get_parameters() {
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['user_id'] = array(
'required' => true,
'type' => 'string',
);
return $params;
}
/**
* The supported parameters
*

View file

@ -5,6 +5,7 @@ use stdClass;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Validator\Query;
use Activitypub\Transformer\Post;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection;
@ -38,7 +39,7 @@ class Outbox {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'user_outbox_get' ),
'args' => self::request_parameters(),
'args' => Query::get_default_args(),
'permission_callback' => '__return_true',
),
)
@ -129,25 +130,4 @@ class Outbox {
return $response;
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['page'] = array(
'type' => 'integer',
'default' => 1,
);
$params['user_id'] = array(
'required' => true,
'type' => 'string',
);
return $params;
}
}

View file

@ -4,6 +4,7 @@ namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Validator\Query;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection;
@ -35,7 +36,7 @@ class Users {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'get' ),
'args' => self::request_parameters(),
'args' => Query::get_default_args(),
'permission_callback' => '__return_true',
),
)
@ -79,24 +80,4 @@ class Users {
return $response;
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['page'] = array(
'type' => 'string',
);
$params['user_id'] = array(
'required' => true,
'type' => 'string',
);
return $params;
}
}

View file

@ -37,7 +37,7 @@ class Webfinger {
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( self::class, 'webfinger' ),
'args' => self::request_parameters(),
'args' => self::request_args(),
'permission_callback' => '__return_true',
),
)
@ -68,16 +68,16 @@ class Webfinger {
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
public static function request_args() {
$args = array();
$params['resource'] = array(
$args['resource'] = array(
'required' => true,
'type' => 'string',
'pattern' => '^acct:(.+)@(.+)$',
);
return $params;
return $args;
}
/**

View file

@ -0,0 +1,38 @@
<?php
namespace Activitypub\Validator;
class Query {
/**
* Validate the query parameters.
*
* @return array A list of arguments and how to validate them.
*/
public static function get_default_args() {
$args = array();
$args['page'] = array(
'type' => 'integer',
'default' => 1,
);
$args['per_page'] = array(
'type' => 'integer',
'default' => 20,
);
$args['order'] = array(
'type' => 'string',
'default' => 'desc',
'enum' => array( 'asc', 'desc' ),
);
$args['context'] = array(
'type' => 'string',
'default' => 'simple',
'enum' => array( 'simple', 'full' ),
);
return $args;
}
}