86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?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\XArray;
|
|
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();
|
|
|
|
$roles = XArray::select(
|
|
$this->usersCtx->getRoles()->getRoles(userInfo: $userInfo, hasString: true, orderByRank: true),
|
|
fn($roleInfo) => $roleInfo->getString(),
|
|
);
|
|
if(!empty($roles))
|
|
$output['roles'] = $roles;
|
|
|
|
if($userInfo->hasTitle())
|
|
$output['title'] = $userInfo->getTitle();
|
|
|
|
if($userInfo->isSuperUser())
|
|
$output['is_super'] = true;
|
|
if($userInfo->isDeleted())
|
|
$output['is_deleted'] = true;
|
|
|
|
return $output;
|
|
}
|
|
}
|