You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.3 KiB
PHTML

2 years ago
<?php
namespace Isekai\AIToolbox\Api\AIToolboxBot;
2 years ago
use ApiBase;
use Isekai\AIToolbox\Api\ApiAIToolboxBot;
2 years ago
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
use Message;
class ApiUserInfo extends ApiBase {
const DEFAULT_CC_ACTION = 'chatcomplete';
/** @var MediaWikiServices */
private $services;
/** @var Config */
private $config;
/** @var ApiAIToolboxBot */
2 years ago
private $mParent;
public function __construct(ApiAIToolboxBot $main, $method) {
2 years ago
parent::__construct($main->getMain(), $method);
$this->mParent = $main;
$this->services = MediaWikiServices::getInstance();
$this->config = $this->services->getMainConfig();
}
public function requireParameter(array $paramKeys) {
$missing = [];
foreach ($paramKeys as $paramKey) {
if ($this->getParameter($paramKey) === null) {
$missing[] = $paramKey;
}
}
if (count($missing) > 0) {
$this->dieWithError( [
'apierror-missingparam',
Message::listParam( array_map(
function ( $p ) {
return '<var>' . $this->encodeParamName( $p ) . '</var>';
},
$missing
) ),
count( $missing ),
], 'missingparam' );
}
}
public function execute() {
// Check permission
$this->checkUserRightsAny('aitoolboxbot');
2 years ago
$userFactory = $this->services->getUserFactory();
$userOptionsLookup = $this->services->getUserOptionsLookup();
$userId = $this->getParameter('userid');
$user = $userFactory->newFromId($userId);
if (!$user->isRegistered()) {
$this->dieWithError('apierror-ai-toolbox-user-not-found', 'user-not-found');
2 years ago
}
$userInfo = [
'userid' => $user->getId(),
'username' => $user->getName(),
'realname' => $user->getRealName(),
// 'nickname' => $userOptionsLookup->getOption($user, 'nickname'),
'email' => $user->getEmail(),
'unlimitedusage' => false
2 years ago
];
// Get user points from IsekaiUserPoints
$pointType = $this->config->get('IsekaiAIToolboxPointType');
if ($pointType) {
2 years ago
/** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */
$pointFactory = $this->services->getService('IsekaiUserPoints');
$pointService = $pointFactory->newFromUserId($userId, $pointType);
if ($pointService) {
$userInfo['points'] = $pointService->points;
}
}
if ($user->isAllowed('aitoolbox-unlimited')) {
$userInfo['unlimitedusage'] = true;
}
2 years ago
// Get user avatar
$avatarTpl = $this->config->get('IsekaiAIToolboxUserAvatar');
2 years ago
$avatarUrl = null;
if (is_array($avatarTpl) && isset($avatarTpl['type'])) {
if ($avatarTpl['type'] === 'gravatar') {
$gravatarUrl = $avatarTpl['mirror'] ?? 'https://secure.gravatar.com/avatar/';
$gravatarUrl .= md5(strtolower(trim($user->getEmail()))) . '?s=256';
$avatarUrl = $gravatarUrl;
} elseif ($avatarTpl['type'] === 'local') {
$avatarUrl = $avatarTpl['url'] ?? '';
$avatarUrl = str_replace(['{username}', '{userid}'], [$user->getName(), $user->getId()], $avatarUrl);
}
}
if ($avatarUrl) {
if (strpos($avatarUrl, 'http://') === false && strpos($avatarUrl, 'https://') === false) {
$avatarUrl = $this->config->get(MainConfigNames::Server) . $avatarUrl;
}
$userInfo['avatar'] = $avatarUrl;
}
$result = $this->getResult();
$result->addValue(['aitoolboxbot'], $this->getModuleName(), $userInfo);
2 years ago
}
public function getAllowedParams($flags = 0) {
return [
'userid' => [
ParamValidator::PARAM_TYPE => 'integer',
ParamValidator::PARAM_DEFAULT => null,
ParamValidator::PARAM_REQUIRED => true,
],
];
}
public function getCacheMode($params) {
return 'private';
}
public function getParent() {
return $this->mParent;
}
}