From 8f897f5578032158b74f958855633173d62eba39 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 29 Aug 2023 12:50:17 +0200 Subject: [PATCH] Add default query validation rules --- includes/rest/class-followers.php | 41 ++---------------------------- includes/rest/class-following.php | 23 ++--------------- includes/rest/class-inbox.php | 23 ++--------------- includes/rest/class-outbox.php | 24 ++--------------- includes/rest/class-users.php | 23 ++--------------- includes/rest/class-webfinger.php | 10 ++++---- includes/validator/class-query.php | 38 +++++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 129 deletions(-) create mode 100644 includes/validator/class-query.php diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index c125fcd..fdaa822 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -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; - } } diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index f212677..6395660 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -1,6 +1,7 @@ \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; - } } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 38969f7..d91b95e 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -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 * diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index b7ff095..b93b5eb 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -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; - } } diff --git a/includes/rest/class-users.php b/includes/rest/class-users.php index b678043..9202c8e 100644 --- a/includes/rest/class-users.php +++ b/includes/rest/class-users.php @@ -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; - } } diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index 4504e0d..86be4d3 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -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; } /** diff --git a/includes/validator/class-query.php b/includes/validator/class-query.php new file mode 100644 index 0000000..32a688d --- /dev/null +++ b/includes/validator/class-query.php @@ -0,0 +1,38 @@ + '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; + } +}