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
PHP
134 lines
4.3 KiB
PHP
<?php
|
|
namespace Isekai\AIToolbox\Api\AIToolboxBot;
|
|
|
|
use ApiBase;
|
|
use Isekai\AIToolbox\Api\ApiAIToolboxBot;
|
|
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 */
|
|
private $mParent;
|
|
|
|
public function __construct(ApiAIToolboxBot $main, $method) {
|
|
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');
|
|
|
|
$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');
|
|
}
|
|
|
|
$userInfo = [
|
|
'userid' => $user->getId(),
|
|
'username' => $user->getName(),
|
|
'realname' => $user->getRealName(),
|
|
// 'nickname' => $userOptionsLookup->getOption($user, 'nickname'),
|
|
'email' => $user->getEmail(),
|
|
'unlimitedusage' => false
|
|
];
|
|
|
|
// Get user points from IsekaiUserPoints
|
|
$pointType = $this->config->get('IsekaiAIToolboxPointType');
|
|
if ($pointType) {
|
|
/** @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;
|
|
}
|
|
|
|
// Get user avatar
|
|
$avatarTpl = $this->config->get('IsekaiAIToolboxUserAvatar');
|
|
$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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |