Compare commits

...

2 commits

Author SHA1 Message Date
b4288353f0 Added emotes endpoint. 2024-11-14 02:45:53 +00:00
2896148124 Added CORS stuff for /v1/me. 2024-11-14 01:53:35 +00:00
5 changed files with 40 additions and 6 deletions

6
composer.lock generated
View file

@ -8,11 +8,11 @@
"packages": [
{
"name": "flashii/rpcii",
"version": "v2.0.0",
"version": "v2.0.1",
"source": {
"type": "git",
"url": "https://patchii.net/flashii/rpcii-php.git",
"reference": "93ec139171d023f210f0b7464266b6fc42b4d838"
"reference": "1cbc1edb061612dc1d014a82e24b741d2a0bc11a"
},
"require": {
"ext-msgpack": ">=2.2",
@ -43,7 +43,7 @@
],
"description": "HTTP RPC client/server library.",
"homepage": "https://railgun.sh/rpcii",
"time": "2024-11-13T23:17:29+00:00"
"time": "2024-11-14T02:22:09+00:00"
},
{
"name": "flashwave/index",

View file

@ -29,7 +29,7 @@ class RpcClientWrapper implements RpcClient {
return new ScopedRpcClient($this, $prefix);
}
public function query(string $action, array $params): mixed {
public function query(string $action, array $params = []): mixed {
foreach($this->clients as $prefix => $client)
if(str_starts_with($action, $prefix))
return $client->query($action, $params);
@ -37,7 +37,7 @@ class RpcClientWrapper implements RpcClient {
throw new RuntimeException(sprintf('Unable to handle this query: %s', $action));
}
public function action(string $action, array $params): mixed {
public function action(string $action, array $params = []): mixed {
foreach($this->clients as $prefix => $client)
if(str_starts_with($action, $prefix))
return $client->action($action, $params);

25
src/V1/V1EmotesRoutes.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace Syokuhou\V1;
use Index\Http\Routing\{HttpGet,RouteHandler,RouteHandlerTrait};
use RPCii\Client\RpcClient;
class V1EmotesRoutes implements RouteHandler {
use RouteHandlerTrait;
public function __construct(
private RpcClient $rpc
) {}
#[HttpGet('/')]
public function getEmotes($response, $request) {
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
$response->setHeader('Cache-Control', 'public, max-age=3600');
return $this->rpc->query('all', [
'includeId' => !empty($request->getParam('include_id')),
'includeOrder' => !empty($request->getParam('include_order')),
]);
}
}

View file

@ -15,7 +15,12 @@ class V1Routes implements RouteHandler {
$router->get('/', fn() => ['status' => 'operational']);
$router->scopeTo('/emotes')->register(
new V1EmotesRoutes($this->ctx->getRpcClient()->scopeTo('misuzu:emotes:'))
);
$usersRoutes = new V1UsersRoutes($this->ctx, $this->ctx->getRpcClient()->scopeTo('misuzu:users:'));
$router->options('/me', $usersRoutes->getMe(...));
$router->get('/me', $usersRoutes->getMe(...));
$router->scopeTo('/users')->register($usersRoutes);
}

View file

@ -12,7 +12,11 @@ class V1UsersRoutes implements RouteHandler {
private RpcClient $rpc
) {}
public function getMe() {
public function getMe($response) {
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
$response->setHeader('Cache-Control', 'no-store');
$authz = $this->ctx->getAuthzContext();
if(!$authz->hasScope('identify'))
return 403;