2023-07-27 00:31:52 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2020-04-12 05:38:19 -05:00
|
|
|
import { utils, values } from '@syuilo/aiscript';
|
2023-09-19 02:37:43 -05:00
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { $i } from '@/account.js';
|
|
|
|
import { miLocalStorage } from '@/local-storage.js';
|
|
|
|
import { customEmojis } from '@/custom-emojis.js';
|
2023-09-29 19:39:21 -05:00
|
|
|
import { url, lang } from '@/config.js';
|
2023-10-25 03:46:04 -05:00
|
|
|
import { nyaize } from '@/scripts/nyaize.js';
|
2020-04-12 05:38:19 -05:00
|
|
|
|
2020-10-17 06:12:00 -05:00
|
|
|
export function createAiScriptEnv(opts) {
|
2020-04-12 05:38:19 -05:00
|
|
|
return {
|
2020-12-18 19:55:52 -06:00
|
|
|
USER_ID: $i ? values.STR($i.id) : values.NULL,
|
|
|
|
USER_NAME: $i ? values.STR($i.name) : values.NULL,
|
|
|
|
USER_USERNAME: $i ? values.STR($i.username) : values.NULL,
|
2023-01-16 03:39:58 -06:00
|
|
|
CUSTOM_EMOJIS: utils.jsToVal(customEmojis.value),
|
2023-08-20 21:53:06 -05:00
|
|
|
LOCALE: values.STR(lang),
|
2023-09-29 19:39:21 -05:00
|
|
|
SERVER_URL: values.STR(url),
|
2020-04-12 05:38:19 -05:00
|
|
|
'Mk:dialog': values.FN_NATIVE(async ([title, text, type]) => {
|
2021-11-18 03:45:58 -06:00
|
|
|
await os.alert({
|
2020-04-12 05:38:19 -05:00
|
|
|
type: type ? type.value : 'info',
|
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
2023-05-01 19:56:20 -05:00
|
|
|
return values.NULL;
|
2020-04-12 05:38:19 -05:00
|
|
|
}),
|
2020-07-28 05:02:28 -05:00
|
|
|
'Mk:confirm': values.FN_NATIVE(async ([title, text, type]) => {
|
2021-11-18 03:45:58 -06:00
|
|
|
const confirm = await os.confirm({
|
2020-07-28 05:02:28 -05:00
|
|
|
type: type ? type.value : 'question',
|
2020-04-12 05:38:19 -05:00
|
|
|
title: title.value,
|
|
|
|
text: text.value,
|
|
|
|
});
|
2020-05-10 03:25:16 -05:00
|
|
|
return confirm.canceled ? values.FALSE : values.TRUE;
|
2020-04-12 05:38:19 -05:00
|
|
|
}),
|
|
|
|
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
|
2023-09-24 00:41:41 -05:00
|
|
|
utils.assertString(ep);
|
|
|
|
if (ep.value.includes('://')) throw new Error('invalid endpoint');
|
2023-02-05 05:38:33 -06:00
|
|
|
if (token) {
|
|
|
|
utils.assertString(token);
|
|
|
|
// バグがあればundefinedもあり得るため念のため
|
|
|
|
if (typeof token.value !== 'string') throw new Error('invalid token');
|
|
|
|
}
|
2023-09-24 00:41:41 -05:00
|
|
|
const actualToken: string|null = token?.value ?? opts.token ?? null;
|
|
|
|
return os.api(ep.value, utils.valToJs(param), actualToken).then(res => {
|
2023-09-18 00:23:33 -05:00
|
|
|
return utils.jsToVal(res);
|
|
|
|
}, err => {
|
|
|
|
return values.ERROR('request_failed', utils.jsToVal(err));
|
|
|
|
});
|
2020-04-12 05:38:19 -05:00
|
|
|
}),
|
2023-09-29 01:21:45 -05:00
|
|
|
'Mk:apiExternal': values.FN_NATIVE(async ([host, ep, param, token]) => {
|
|
|
|
utils.assertString(host);
|
|
|
|
utils.assertString(ep);
|
|
|
|
if (token) utils.assertString(token);
|
|
|
|
return os.apiExternal(host.value, ep.value, utils.valToJs(param), token?.value).then(res => {
|
|
|
|
return utils.jsToVal(res);
|
|
|
|
}, err => {
|
|
|
|
return values.ERROR('request_failed', utils.jsToVal(err));
|
|
|
|
});
|
|
|
|
}),
|
2020-04-12 05:57:18 -05:00
|
|
|
'Mk:save': values.FN_NATIVE(([key, value]) => {
|
|
|
|
utils.assertString(key);
|
2023-01-06 19:13:02 -06:00
|
|
|
miLocalStorage.setItem(`aiscript:${opts.storageKey}:${key.value}`, JSON.stringify(utils.valToJs(value)));
|
2020-04-12 05:57:18 -05:00
|
|
|
return values.NULL;
|
|
|
|
}),
|
|
|
|
'Mk:load': values.FN_NATIVE(([key]) => {
|
|
|
|
utils.assertString(key);
|
2023-01-06 19:13:02 -06:00
|
|
|
return utils.jsToVal(JSON.parse(miLocalStorage.getItem(`aiscript:${opts.storageKey}:${key.value}`)));
|
2020-04-12 05:57:18 -05:00
|
|
|
}),
|
2023-07-26 19:00:22 -05:00
|
|
|
'Mk:url': values.FN_NATIVE(() => {
|
|
|
|
return values.STR(window.location.href);
|
|
|
|
}),
|
2023-10-25 03:46:04 -05:00
|
|
|
'Mk:nyaize': values.FN_NATIVE(([text]) => {
|
|
|
|
utils.assertString(text);
|
|
|
|
return values.STR(nyaize(text.value));
|
|
|
|
}),
|
2020-04-12 05:38:19 -05:00
|
|
|
};
|
|
|
|
}
|