From 8ed8f865b161890ea10fc39161884b7ec25f4783 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Sat, 18 May 2024 08:21:11 +0000 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extension.json | 3 + i18n/zh-hans.json | 5 ++ includes/Api/AIToolbox/ApiCreateToken.php | 16 ++++-- includes/Special/SpecialAIToolbox.php | 70 +++++++++++++++++++++++ modules/ext.isekai.ai-toolbox.launcher.js | 6 ++ 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 includes/Special/SpecialAIToolbox.php diff --git a/extension.json b/extension.json index f9630b9..d6c3fc5 100644 --- a/extension.json +++ b/extension.json @@ -67,6 +67,9 @@ "JobClasses": { "isekaiAIUpdateTitleIndex": "Isekai\\AIToolbox\\Job\\UpdateTitleIndexJob" }, + "SpecialPages": { + "IsekaiAIToolbox": "Isekai\\AIToolbox\\Special\\SpecialAIToolbox" + }, "ResourceModules": { "ext.isekai.ai-toolbox.launcher": { "scripts": [ diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index 74e23d7..8d3a0cf 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -5,6 +5,11 @@ "isekai-ai-toolbox-menubutton": "启动AI工具箱", "isekai-ai-toolbox-loading": "正在加载AI工具箱", + "isekaiaitoolbox": "AI工具箱", + "special-isekai-ai-toolbox": "AI工具箱", + "isekai-ai-toolbox-enter-toolbox": "进入AI工具箱", + "isekai-ai-toolbox-label-continue-curaccount": "你要以此帐号继续吗?", + "action-chatcomplete": "使用 Chat Complete 功能", "right-chatcomplete": "使用 Chat Complete 功能", diff --git a/includes/Api/AIToolbox/ApiCreateToken.php b/includes/Api/AIToolbox/ApiCreateToken.php index 2a7ed08..2e8f7ad 100644 --- a/includes/Api/AIToolbox/ApiCreateToken.php +++ b/includes/Api/AIToolbox/ApiCreateToken.php @@ -36,12 +36,19 @@ class ApiCreateToken extends ApiBase { $result = $this->getResult(); $permissionMap = ApiAIToolbox::$permissionMap; - - if (!$user->isAllowedAny(...array_values($permissionMap))) { - $this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission'); - return false; + + $permissions = []; + foreach ($permissionMap as $key => $permissionKey) { + if ($user->isAllowed($permissionKey)) { + $permissions[] = $key; + } } + // if (!$user->isAllowedAny(...array_values($permissionMap))) { + // $this->addError('apierror-isekai-ai-toolbox-nopermission', 'nopermission'); + // return false; + // } + // Create JWT $tokenId = $this->config->get('IsekaiAIToolboxTokenId'); $token = $this->config->get('IsekaiAIToolboxToken'); @@ -55,6 +62,7 @@ class ApiCreateToken extends ApiBase { 'userName' => $user->getName(), 'iat' => $currentTime, 'exp' => $expireTime, + 'permissions' => $permissions, ]; $jwt = JWT::encode($payload, $token, 'HS256', $tokenId); diff --git a/includes/Special/SpecialAIToolbox.php b/includes/Special/SpecialAIToolbox.php new file mode 100644 index 0000000..a70cf93 --- /dev/null +++ b/includes/Special/SpecialAIToolbox.php @@ -0,0 +1,70 @@ +setHeaders(); + $this->outputHeader(); + + $this->showForm(); + } + + protected function showForm() { + $user = $this->getUser(); + $out = $this->getOutput(); + $out->setPageTitle($this->msg('special-isekai-ai-toolbox')); + $out->addModuleStyles([ + 'mediawiki.special', + 'mediawiki.interface.helpers.styles', + ]); + $out->addModules([ + 'mediawiki.misc-authed-ooui', + 'ext.isekai.ai-toolbox.launcher', + ]); + + $out->enableOOUI(); + + $fields = []; + + $fields[] = new \OOUI\FieldLayout( + new \OOUI\LabelWidget([ + 'label' => $this->msg('isekai-ai-toolbox-label-continue-curaccount')->text(), + 'align' => 'top', + ]) + ); + + $fields[] = new \OOUI\FieldLayout( + new \OOUI\ButtonWidget( [ + 'id' => 'btn-enter-ai-toolbox', + 'label' => $this->msg('ok')->text(), + 'flags' => ['primary', 'progressive'], + 'type' => 'button', + ]), + [ + 'align' => 'top', + ] + ); + + $fieldset = new \OOUI\FieldsetLayout( [ + 'label' => $this->msg('isekai-ai-toolbox-enter-toolbox')->text(), + 'id' => 'mw-aitoolbox-table', + 'items' => $fields, + ] ); + + $out->addHTML( + new \OOUI\PanelLayout([ + 'classes' => ['movepage-wrapper', 'aitoolbox-wrapper'], + 'expanded' => false, + 'padded' => true, + 'framed' => true, + 'content' => $fieldset, + ]) + ); + } +} \ No newline at end of file diff --git a/modules/ext.isekai.ai-toolbox.launcher.js b/modules/ext.isekai.ai-toolbox.launcher.js index d2b6471..4f2137b 100644 --- a/modules/ext.isekai.ai-toolbox.launcher.js +++ b/modules/ext.isekai.ai-toolbox.launcher.js @@ -58,4 +58,10 @@ $(function() { } }); } + + if ($('#btn-enter-ai-toolbox').length > 0) { + $('#btn-enter-ai-toolbox').on('click', function() { + launchAIToolbox(); + }); + } }); \ No newline at end of file