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.
115 lines
3.9 KiB
PHTML
115 lines
3.9 KiB
PHTML
2 years ago
|
<?php
|
||
2 years ago
|
namespace Isekai\AIToolbox\Api\AIToolbox;
|
||
2 years ago
|
|
||
|
use ApiBase;
|
||
2 years ago
|
use Isekai\AIToolbox\Api\ApiAIToolbox;
|
||
|
use Isekai\AIToolbox\AIToolboxUtils;
|
||
2 years ago
|
use MediaWiki\MediaWikiServices;
|
||
|
use Wikimedia\ParamValidator\ParamValidator;
|
||
|
|
||
|
class ApiCheckAccess extends ApiBase {
|
||
|
/** @var MediaWikiServices */
|
||
|
private $services;
|
||
|
|
||
2 years ago
|
/** @var ApiAIToolbox */
|
||
2 years ago
|
private $mParent;
|
||
|
|
||
2 years ago
|
public function __construct(ApiAIToolbox $main, $method) {
|
||
2 years ago
|
parent::__construct($main->getMain(), $method);
|
||
|
|
||
|
$this->mParent = $main;
|
||
|
$this->services = MediaWikiServices::getInstance();
|
||
|
}
|
||
|
|
||
|
public function execute() {
|
||
2 years ago
|
$userAction = strtolower($this->getParameter('ataction'));
|
||
2 years ago
|
$tokens = $this->getParameter('tokens');
|
||
|
$extractLimit = $this->getParameter('extractlines');
|
||
|
|
||
|
$user = $this->getUser();
|
||
|
|
||
|
if (!$user->isRegistered()) {
|
||
2 years ago
|
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
|
||
2 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
$result = $this->getResult();
|
||
|
|
||
|
$permissionMap = [
|
||
|
'embeddingpage' => 'ccembeddingpage',
|
||
|
'chatcomplete' => 'chatcomplete',
|
||
|
];
|
||
|
|
||
|
$permissionManager = $this->services->getPermissionManager();
|
||
|
|
||
|
$permissionKey = $permissionMap[$userAction] ?? null;
|
||
|
if ($permissionKey && !$user->isAllowed($permissionKey)) {
|
||
2 years ago
|
$this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission');
|
||
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', false);
|
||
2 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
$noCost = false;
|
||
2 years ago
|
if ($userAction === 'chatcomplete' && $user->isAllowed('aitoolbox-unlimited')) {
|
||
2 years ago
|
$noCost = true;
|
||
|
}
|
||
|
|
||
|
$isAvailable = true;
|
||
|
|
||
2 years ago
|
$pointCostData = AIToolboxUtils::getPointCost($userAction, $tokens, $extractLimit);
|
||
2 years ago
|
if ($pointCostData && !$noCost) {
|
||
|
list($pointType, $pointCost) = $pointCostData;
|
||
|
|
||
2 years ago
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointtype', $pointType);
|
||
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointcost', $pointCost);
|
||
2 years ago
|
|
||
|
// Check if user have enough points
|
||
|
/** @var \Isekai\UserPoints\Service\IsekaiUserPointsFactory */
|
||
|
$pointFactory = $this->services->getService('IsekaiUserPoints');
|
||
|
$pointService = $pointFactory->newFromUser($user, $pointType);
|
||
|
|
||
|
if (!$pointService->hasEnoughPoints($pointCost)) { // User doesn't have enough points
|
||
2 years ago
|
$this->addError('apierror-isekai-ai-toolbox-notenoughpoints', 'notenoughpoints');
|
||
2 years ago
|
$isAvailable = false;
|
||
|
}
|
||
|
} else {
|
||
2 years ago
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointtype', null);
|
||
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'pointcost', 0);
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
$result->addValue(['aitoolbox', $this->getModuleName()], 'available', $isAvailable);
|
||
2 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public function getAllowedParams($flags = 0) {
|
||
|
return [
|
||
2 years ago
|
'ataction' => [
|
||
2 years ago
|
ParamValidator::PARAM_TYPE => [
|
||
|
'embeddingpage',
|
||
|
'chatcomplete',
|
||
|
],
|
||
|
ParamValidator::PARAM_DEFAULT => null,
|
||
|
ParamValidator::PARAM_REQUIRED => true,
|
||
|
],
|
||
|
'tokens' => [
|
||
|
ParamValidator::PARAM_TYPE => 'integer',
|
||
|
ParamValidator::PARAM_DEFAULT => 100,
|
||
|
ParamValidator::PARAM_REQUIRED => false,
|
||
|
],
|
||
|
'extractlines' => [
|
||
|
ParamValidator::PARAM_TYPE => 'integer',
|
||
|
ParamValidator::PARAM_DEFAULT => 5,
|
||
|
ParamValidator::PARAM_REQUIRED => false,
|
||
|
],
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public function getCacheMode($params) {
|
||
|
return 'private';
|
||
|
}
|
||
|
|
||
|
public function getParent() {
|
||
|
return $this->mParent;
|
||
|
}
|
||
|
}
|