From d0102327c08d36107f8047062c89963f12088910 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 3 Feb 2019 09:30:31 -0500 Subject: [PATCH] Add /servers/surprise endpoint --- docs/Protocol.md | 8 +++++ src/classes/Controllers/Api/Surprise.php | 39 ++++++++++++++++++++++++ src/classes/Routes/Http/Surprise.php | 16 ++++++++++ src/routes.php | 1 + 4 files changed, 64 insertions(+) create mode 100644 src/classes/Controllers/Api/Surprise.php create mode 100644 src/classes/Routes/Http/Surprise.php diff --git a/docs/Protocol.md b/docs/Protocol.md index 3dafd02..ec2ffca 100644 --- a/docs/Protocol.md +++ b/docs/Protocol.md @@ -1,5 +1,13 @@ # Friendica Directory Protocol +## Surprise + +``` +GET /servers/surprise +``` + +Redirects to the base URL of a random server with open registration policy and above 75 health. + ## Search ``` diff --git a/src/classes/Controllers/Api/Surprise.php b/src/classes/Controllers/Api/Surprise.php new file mode 100644 index 0000000..8a2da45 --- /dev/null +++ b/src/classes/Controllers/Api/Surprise.php @@ -0,0 +1,39 @@ + + */ +class Surprise +{ + /** + * @var \Atlas\Pdo\Connection + */ + private $atlas; + + public function __construct( + \Atlas\Pdo\Connection $atlas + ) + { + $this->atlas = $atlas; + } + + public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response + { + $redirectUrl = ''; + + $sql = 'SELECT base_url FROM server WHERE reg_policy = "REGISTER_OPEN" AND health_score > 75 ORDER BY health_score DESC, RAND()'; + + $server = $this->atlas->fetchOne($sql); + if ($server) { + $redirectUrl = $server['base_url']; + } + + return $response->withRedirect($redirectUrl); + } +} diff --git a/src/classes/Routes/Http/Surprise.php b/src/classes/Routes/Http/Surprise.php new file mode 100644 index 0000000..d7fc6e4 --- /dev/null +++ b/src/classes/Routes/Http/Surprise.php @@ -0,0 +1,16 @@ + + */ +class Surprise extends BaseRoute +{ + public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response + { + return (new \Friendica\Directory\Controllers\Api\Surprise( + $this->container->atlas + ))->render($request, $response, $args); + } +} diff --git a/src/routes.php b/src/routes.php index 7718cc7..b703b5e 100644 --- a/src/routes.php +++ b/src/routes.php @@ -9,6 +9,7 @@ use Slim\Http\Response; * @var $app \Slim\App */ +$app->get('/servers/surprise', \Friendica\Directory\Routes\Http\Surprise::class); $app->get('/servers', \Friendica\Directory\Routes\Web\Servers::class); $app->get('/search[/{account_type}]', function (Request $request, Response $response, $args) {