Added RPC endpoint for fetching user info.

This commit is contained in:
flash 2024-09-05 20:08:31 +00:00
parent 8a06836985
commit f547812d5a
2 changed files with 84 additions and 0 deletions

View file

@ -292,6 +292,12 @@ class MisuzuContext {
$this->authCtx
));
$rpcServer->register(new Users\UsersRpcActions(
$this->siteInfo,
$this->urls,
$this->usersCtx
));
// This RPC server will eventually despawn when Hanyuu fully owns auth
$hanyuuRpcServer = new RpcServer;
$routingCtx->getRouter()->scopeTo('/_hanyuu')->register($hanyuuRpcServer->createRouteHandler(

View file

@ -0,0 +1,78 @@
<?php
namespace Misuzu\Users;
use RuntimeException;
use Misuzu\SiteInfo;
use Misuzu\URLs\URLRegistry;
use Misuzu\Users\Assets\UserAvatarAsset;
use Aiwass\Server\{RpcActionHandler,RpcQuery};
use Index\Colour\{Colour,ColourRGB};
final class UsersRpcActions extends RpcActionHandler {
public function __construct(
private SiteInfo $siteInfo,
private URLRegistry $urls,
private UsersContext $usersCtx
) {}
#[RpcQuery('misuzu:users:getUser')]
public function queryGetUser(string $userId): array {
try {
$userInfo = $this->usersCtx->getUserInfo($userId, Users::GET_USER_ID);
} catch(RuntimeException) {
return ['error' => 'notfound'];
}
// TODO: there should be some kinda privacy controls for users
$rank = $this->usersCtx->getUserRank($userInfo);
$colour = $this->usersCtx->getUserColour($userInfo);
if($colour->shouldInherit()) {
$colourRaw = null;
$colourCSS = (string)$colour;
} else {
// Index doesn't have a proper toRawRGB func???
$colourRaw = Colour::toMisuzu($colour) & 0xFFFFFF;
$colourCSS = (string)ColourRGB::convert($colour);
}
$baseUrl = $this->siteInfo->getURL();
$avatars = [];
$formatAvatarUrl = fn($res = 0) => (
$baseUrl . $this->urls->format('user-avatar', ['user' => $userInfo->getId(), 'res' => $res])
);
$avatars[] = ['res' => 0, 'url' => $formatAvatarUrl()];
foreach(UserAvatarAsset::DIMENSIONS as $res)
$avatars[] = ['res' => $res, 'url' => $formatAvatarUrl($res)];
$avatars = array_reverse($avatars);
$output = [
'id' => $userInfo->getId(),
'name' => $userInfo->getName(),
'colour_raw' => $colourRaw,
'colour_css' => $colourCSS,
'rank' => $rank,
'country_code' => $userInfo->getCountryCode(),
'avatar_urls' => $avatars,
'profile_url' => $baseUrl . $this->urls->format('user-profile', ['user' => $userInfo->getId()]),
'created_at' => $userInfo->getCreatedAt()->toIso8601ZuluString(),
];
if($userInfo->hasLastActive())
$output['last_active_at'] = $userInfo->getLastActiveAt()->toIso8601ZuluString();
if($userInfo->hasTitle())
$output['title'] = $userInfo->getTitle();
if($userInfo->isSuperUser())
$output['is_super'] = true;
if($userInfo->isDeleted())
$output['is_deleted'] = true;
return $output;
}
}