From f547812d5aa7fb3d45b3ca23c2853c804ae25c3d Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 5 Sep 2024 20:08:31 +0000 Subject: [PATCH] Added RPC endpoint for fetching user info. --- src/MisuzuContext.php | 6 +++ src/Users/UsersRpcActions.php | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/Users/UsersRpcActions.php diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index d6c9189..f559f3a 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -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( diff --git a/src/Users/UsersRpcActions.php b/src/Users/UsersRpcActions.php new file mode 100644 index 0000000..a514e95 --- /dev/null +++ b/src/Users/UsersRpcActions.php @@ -0,0 +1,78 @@ +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; + } +}