diff options
Diffstat (limited to 'packages/frontend/src/plugin.ts')
| -rw-r--r-- | packages/frontend/src/plugin.ts | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/packages/frontend/src/plugin.ts b/packages/frontend/src/plugin.ts index e6545bb8e7..f32c991828 100644 --- a/packages/frontend/src/plugin.ts +++ b/packages/frontend/src/plugin.ts @@ -7,7 +7,7 @@ import { ref } from 'vue'; import { compareVersions } from 'compare-versions'; import { isSafeMode } from '@@/js/config.js'; import * as Misskey from 'misskey-js'; -import type { Parser, Interpreter, values } from '@syuilo/aiscript'; +import type { Parser, Interpreter, values, utils as utils_TypeReferenceOnly } from '@syuilo/aiscript'; import type { FormWithDefault } from '@/utility/form.js'; import { genId } from '@/utility/id.js'; import { store } from '@/store.js'; @@ -82,22 +82,23 @@ export async function parsePluginMeta(code: string): Promise<AiScriptPluginMeta> } const metadata = meta.get(null); - if (metadata == null) { - throw new Error('Metadata not found'); + if (metadata == null || typeof metadata !== 'object' || Array.isArray(metadata)) { + throw new Error('Metadata not found or invalid'); } const { name, version, author, description, permissions, config } = metadata; + if (name == null || version == null || author == null) { throw new Error('Required property not found'); } return { - name, - version, - author, - description, - permissions, - config, + name: name as string, + version: version as string, + author: author as string, + description: description as string | undefined, + permissions: permissions as string[] | undefined, + config: config as Record<string, any> | undefined, }; } @@ -110,7 +111,7 @@ export async function authorizePlugin(plugin: Plugin) { title: i18n.ts.tokenRequested, information: i18n.ts.pluginTokenRequestedDescription, initialName: plugin.name, - initialPermissions: plugin.permissions, + initialPermissions: plugin.permissions as typeof Misskey.permissions[number][], }, { done: async result => { const { name, permissions } = result; @@ -149,6 +150,7 @@ export async function installPlugin(code: string, meta?: AiScriptPluginMeta) { const plugin = { ...realMeta, + config: realMeta.config ?? {}, installId, active: true, configData: {}, @@ -353,7 +355,9 @@ export function changePluginActive(plugin: Plugin, active: boolean) { async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Promise<Record<string, values.Value>> { const id = opts.plugin.installId; - const { utils, values } = await import('@syuilo/aiscript'); + const ais = await import('@syuilo/aiscript'); + const values = ais.values; + const utils: typeof utils_TypeReferenceOnly = ais.utils; const { createAiScriptEnv } = await import('@/aiscript/api.js'); const config = new Map<string, values.Value>(); @@ -375,7 +379,7 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr utils.assertFunction(handler); addPluginHandler(id, 'post_form_action', { title: title.value, - handler: withContext(ctx => (form, update) => { + handler: (form, update) => withContext(ctx => { ctx.execFn(handler, [utils.jsToVal(form), values.FN_NATIVE(([key, value]) => { if (!key || !value) { return; @@ -391,7 +395,7 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr utils.assertFunction(handler); addPluginHandler(id, 'user_action', { title: title.value, - handler: withContext(ctx => (user) => { + handler: (user) => withContext(ctx => { ctx.execFn(handler, [utils.jsToVal(user)]); }), }); @@ -402,7 +406,7 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr utils.assertFunction(handler); addPluginHandler(id, 'note_action', { title: title.value, - handler: withContext(ctx => (note) => { + handler: (note) => withContext(ctx => { ctx.execFn(handler, [utils.jsToVal(note)]); }), }); @@ -411,8 +415,8 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr 'Plugin:register:note_view_interruptor': values.FN_NATIVE(([handler]) => { utils.assertFunction(handler); addPluginHandler(id, 'note_view_interruptor', { - handler: withContext(ctx => (note) => { - return utils.valToJs(ctx.execFnSync(handler, [utils.jsToVal(note)])); + handler: (note) => withContext(ctx => { + return utils.valToJs(ctx.execFnSync(handler, [utils.jsToVal(note)])) as Misskey.entities.Note | null; }), }); }), @@ -420,8 +424,8 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr 'Plugin:register:note_post_interruptor': values.FN_NATIVE(([handler]) => { utils.assertFunction(handler); addPluginHandler(id, 'note_post_interruptor', { - handler: withContext(ctx => async (note) => { - return utils.valToJs(await ctx.execFn(handler, [utils.jsToVal(note)])); + handler: (note) => withContext(ctx => { + return utils.valToJs(ctx.execFnSync(handler, [utils.jsToVal(note)])); }), }); }), @@ -429,8 +433,8 @@ async function createPluginEnv(opts: { plugin: Plugin; storageKey: string }): Pr 'Plugin:register:page_view_interruptor': values.FN_NATIVE(([handler]) => { utils.assertFunction(handler); addPluginHandler(id, 'page_view_interruptor', { - handler: withContext(ctx => async (page) => { - return utils.valToJs(await ctx.execFn(handler, [utils.jsToVal(page)])); + handler: (page) => withContext(ctx => { + return utils.valToJs(ctx.execFnSync(handler, [utils.jsToVal(page)])) as Misskey.entities.Page; }), }); }), |