From f51bca41c5f59f9ffce346a3ec32badaf1ccda31 Mon Sep 17 00:00:00 2001
From: かっこかり <67428053+kakkokari-gtyih@users.noreply.github.com>
Date: Sat, 21 Oct 2023 18:41:12 +0900
Subject: Feat: 外部サイトからテーマ・プラグインのインストールができるように
 (#12034)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Feat: 外部サイトからテーマ・プラグインのインストールができるように

* Update Changelog

* Change Changelog

* Remove unnecessary imports

* Update fetch-external-resources.ts

* Update CHANGELOG.md

* Update CHANGELOG.md
---
 .../frontend/src/pages/settings/plugin.install.vue | 117 ++-------------------
 1 file changed, 11 insertions(+), 106 deletions(-)

(limited to 'packages/frontend/src/pages/settings/plugin.install.vue')

diff --git a/packages/frontend/src/pages/settings/plugin.install.vue b/packages/frontend/src/pages/settings/plugin.install.vue
index 47ebe9cfd6..693e02d0ed 100644
--- a/packages/frontend/src/pages/settings/plugin.install.vue
+++ b/packages/frontend/src/pages/settings/plugin.install.vue
@@ -18,130 +18,35 @@ SPDX-License-Identifier: AGPL-3.0-only
 </template>
 
 <script lang="ts" setup>
-import { defineAsyncComponent, nextTick, ref } from 'vue';
-import { compareVersions } from 'compare-versions';
-import { Interpreter, Parser, utils } from '@syuilo/aiscript';
-import { v4 as uuid } from 'uuid';
+import { nextTick, ref } from 'vue';
 import MkTextarea from '@/components/MkTextarea.vue';
 import MkButton from '@/components/MkButton.vue';
 import FormInfo from '@/components/MkInfo.vue';
 import * as os from '@/os.js';
-import { ColdDeviceStorage } from '@/store.js';
+import { installPlugin } from '@/scripts/install-plugin.js';
 import { unisonReload } from '@/scripts/unison-reload.js';
 import { i18n } from '@/i18n.js';
 import { definePageMetadata } from '@/scripts/page-metadata.js';
 
-const parser = new Parser();
-const code = ref(null);
-
-function installPlugin({ id, meta, src, token }) {
-	ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
-		...meta,
-		id,
-		active: true,
-		configData: {},
-		token: token,
-		src: src,
-	}));
-}
-
-function isSupportedAiScriptVersion(version: string): boolean {
-	try {
-		return (compareVersions(version, '0.12.0') >= 0);
-	} catch (err) {
-		return false;
-	}
-}
+const code = ref<string | null>(null);
 
 async function install() {
-	if (code.value == null) return;
+	if (!code.value) return;
 
-	const lv = utils.getLangVersion(code.value);
-	if (lv == null) {
-		os.alert({
-			type: 'error',
-			text: 'No language version annotation found :(',
-		});
-		return;
-	} else if (!isSupportedAiScriptVersion(lv)) {
-		os.alert({
-			type: 'error',
-			text: `aiscript version '${lv}' is not supported :(`,
-		});
-		return;
-	}
-
-	let ast;
 	try {
-		ast = parser.parse(code.value);
-	} catch (err) {
-		os.alert({
-			type: 'error',
-			text: 'Syntax error :(',
-		});
-		return;
-	}
+		await installPlugin(code.value);
+		os.success();
 
-	const meta = Interpreter.collectMetadata(ast);
-	if (meta == null) {
-		os.alert({
-			type: 'error',
-			text: 'No metadata found :(',
+		nextTick(() => {
+			unisonReload();
 		});
-		return;
-	}
-
-	const metadata = meta.get(null);
-	if (metadata == null) {
-		os.alert({
-			type: 'error',
-			text: 'No metadata found :(',
-		});
-		return;
-	}
-
-	const { name, version, author, description, permissions, config } = metadata;
-	if (name == null || version == null || author == null) {
+	} catch (err) {
 		os.alert({
 			type: 'error',
-			text: 'Required property not found :(',
+			title: 'Install failed',
+			text: err.toString() ?? null,
 		});
-		return;
 	}
-
-	const token = permissions == null || permissions.length === 0 ? null : await new Promise((res, rej) => {
-		os.popup(defineAsyncComponent(() => import('@/components/MkTokenGenerateWindow.vue')), {
-			title: i18n.ts.tokenRequested,
-			information: i18n.ts.pluginTokenRequestedDescription,
-			initialName: name,
-			initialPermissions: permissions,
-		}, {
-			done: async result => {
-				const { name, permissions } = result;
-				const { token } = await os.api('miauth/gen-token', {
-					session: null,
-					name: name,
-					permission: permissions,
-				});
-				res(token);
-			},
-		}, 'closed');
-	});
-
-	installPlugin({
-		id: uuid(),
-		meta: {
-			name, version, author, description, permissions, config,
-		},
-		token,
-		src: code.value,
-	});
-
-	os.success();
-
-	nextTick(() => {
-		unisonReload();
-	});
 }
 
 const headerActions = $computed(() => []);
-- 
cgit v1.2.3-freya

