2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2021-02-06 13:05:00 +01:00
|
|
|
<template>
|
2023-01-06 05:40:17 +01:00
|
|
|
<div class="_gaps_m">
|
2023-01-05 13:04:56 +01:00
|
|
|
<FormInfo warn>{{ i18n.ts._plugin.installWarn }}</FormInfo>
|
2021-02-06 13:05:00 +01:00
|
|
|
|
2023-01-07 07:09:46 +01:00
|
|
|
<MkTextarea v-model="code" tall>
|
2022-05-04 03:10:52 +02:00
|
|
|
<template #label>{{ i18n.ts.code }}</template>
|
2023-01-07 07:09:46 +01:00
|
|
|
</MkTextarea>
|
2021-02-06 13:05:00 +01:00
|
|
|
|
2023-01-05 13:04:56 +01:00
|
|
|
<div>
|
2023-01-06 01:41:14 +01:00
|
|
|
<MkButton :disabled="code == null" primary inline @click="install"><i class="ti ti-check"></i> {{ i18n.ts.install }}</MkButton>
|
2022-01-04 09:52:44 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-06 13:05:00 +01:00
|
|
|
</template>
|
|
|
|
|
2022-05-04 03:10:52 +02:00
|
|
|
<script lang="ts" setup>
|
2022-06-20 10:38:49 +02:00
|
|
|
import { defineAsyncComponent, nextTick, ref } from 'vue';
|
2023-01-03 07:51:49 +01:00
|
|
|
import { Interpreter, Parser, utils } from '@syuilo/aiscript';
|
2021-02-06 13:05:00 +01:00
|
|
|
import { v4 as uuid } from 'uuid';
|
2023-01-07 07:09:46 +01:00
|
|
|
import MkTextarea from '@/components/MkTextarea.vue';
|
2023-01-06 01:41:14 +01:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2022-09-06 11:21:49 +02:00
|
|
|
import FormInfo from '@/components/MkInfo.vue';
|
2021-11-11 18:02:25 +01:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { ColdDeviceStorage } from '@/store';
|
|
|
|
import { unisonReload } from '@/scripts/unison-reload';
|
2022-05-04 03:10:52 +02:00
|
|
|
import { i18n } from '@/i18n';
|
2022-06-20 10:38:49 +02:00
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata';
|
2021-02-06 13:05:00 +01:00
|
|
|
|
2023-01-03 07:51:49 +01:00
|
|
|
const parser = new Parser();
|
2022-05-04 03:10:52 +02:00
|
|
|
const code = ref(null);
|
2021-02-06 13:05:00 +01:00
|
|
|
|
2023-01-03 07:51:49 +01:00
|
|
|
function installPlugin({ id, meta, src, token }) {
|
2022-05-04 03:10:52 +02:00
|
|
|
ColdDeviceStorage.set('plugins', ColdDeviceStorage.get('plugins').concat({
|
|
|
|
...meta,
|
|
|
|
id,
|
|
|
|
active: true,
|
|
|
|
configData: {},
|
|
|
|
token: token,
|
2023-01-03 07:51:49 +01:00
|
|
|
src: src,
|
2022-05-04 03:10:52 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function install() {
|
2023-01-03 07:51:49 +01:00
|
|
|
if (code.value == null) return;
|
|
|
|
|
|
|
|
const lv = utils.getLangVersion(code.value);
|
|
|
|
if (lv == null) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: 'No language version annotation found :(',
|
|
|
|
});
|
|
|
|
return;
|
2023-03-15 10:26:59 +01:00
|
|
|
} else if (!(lv.startsWith('0.12.') || lv.startsWith('0.13.'))) {
|
2023-01-03 07:51:49 +01:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
|
|
|
text: `aiscript version '${lv}' is not supported :(`,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 03:10:52 +02:00
|
|
|
let ast;
|
|
|
|
try {
|
2023-01-03 07:51:49 +01:00
|
|
|
ast = parser.parse(code.value);
|
2022-05-26 15:53:09 +02:00
|
|
|
} catch (err) {
|
2022-05-04 03:10:52 +02:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 10:38:49 +02:00
|
|
|
text: 'Syntax error :(',
|
2022-05-04 03:10:52 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:51:49 +01:00
|
|
|
const meta = Interpreter.collectMetadata(ast);
|
2022-05-04 03:10:52 +02:00
|
|
|
if (meta == null) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 10:38:49 +02:00
|
|
|
text: 'No metadata found :(',
|
2022-05-04 03:10:52 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
const metadata = meta.get(null);
|
|
|
|
if (metadata == null) {
|
2022-05-04 03:10:52 +02:00
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 10:38:49 +02:00
|
|
|
text: 'No metadata found :(',
|
2022-05-04 03:10:52 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 15:53:09 +02:00
|
|
|
const { name, version, author, description, permissions, config } = metadata;
|
2022-05-04 03:10:52 +02:00
|
|
|
if (name == null || version == null || author == null) {
|
|
|
|
os.alert({
|
|
|
|
type: 'error',
|
2022-06-20 10:38:49 +02:00
|
|
|
text: 'Required property not found :(',
|
2022-05-04 03:10:52 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const token = permissions == null || permissions.length === 0 ? null : await new Promise((res, rej) => {
|
2022-08-30 17:24:33 +02:00
|
|
|
os.popup(defineAsyncComponent(() => import('@/components/MkTokenGenerateWindow.vue')), {
|
2022-05-04 03:10:52 +02:00
|
|
|
title: i18n.ts.tokenRequested,
|
|
|
|
information: i18n.ts.pluginTokenRequestedDescription,
|
|
|
|
initialName: name,
|
2022-06-20 10:38:49 +02:00
|
|
|
initialPermissions: permissions,
|
2022-05-04 03:10:52 +02:00
|
|
|
}, {
|
|
|
|
done: async result => {
|
|
|
|
const { name, permissions } = result;
|
|
|
|
const { token } = await os.api('miauth/gen-token', {
|
|
|
|
session: null,
|
|
|
|
name: name,
|
|
|
|
permission: permissions,
|
2021-02-06 13:05:00 +01:00
|
|
|
});
|
2022-05-04 03:10:52 +02:00
|
|
|
res(token);
|
2022-06-20 10:38:49 +02:00
|
|
|
},
|
2022-05-04 03:10:52 +02:00
|
|
|
}, 'closed');
|
|
|
|
});
|
|
|
|
|
|
|
|
installPlugin({
|
|
|
|
id: uuid(),
|
|
|
|
meta: {
|
2022-06-20 10:38:49 +02:00
|
|
|
name, version, author, description, permissions, config,
|
2022-05-04 03:10:52 +02:00
|
|
|
},
|
|
|
|
token,
|
2023-01-03 07:51:49 +01:00
|
|
|
src: code.value,
|
2022-05-04 03:10:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
os.success();
|
|
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
unisonReload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-20 10:38:49 +02:00
|
|
|
const headerActions = $computed(() => []);
|
|
|
|
|
|
|
|
const headerTabs = $computed(() => []);
|
|
|
|
|
|
|
|
definePageMetadata({
|
|
|
|
title: i18n.ts._plugin.install,
|
2022-12-19 11:01:30 +01:00
|
|
|
icon: 'ti ti-download',
|
2021-02-06 13:05:00 +01:00
|
|
|
});
|
|
|
|
</script>
|