Commit 64744c23 authored by nanahira's avatar nanahira

reimplement selection and add @Isolate

parent 7b78f3b3
...@@ -3,4 +3,5 @@ export * from './src/register'; ...@@ -3,4 +3,5 @@ export * from './src/register';
export * from './src/decorators'; export * from './src/decorators';
export * from './src/http-decorators'; export * from './src/http-decorators';
export * from './src/def/interfaces'; export * from './src/def/interfaces';
export * from './src/def/select';
export * from './src/utility'; export * from './src/utility';
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
"typescript": "^4.6.4" "typescript": "^4.6.4"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.7.4" "koishi": "^4.7.5"
} }
}, },
"node_modules/@babel/code-frame": { "node_modules/@babel/code-frame": {
...@@ -1000,15 +1000,15 @@ ...@@ -1000,15 +1000,15 @@
} }
}, },
"node_modules/@koishijs/core": { "node_modules/@koishijs/core": {
"version": "4.7.4", "version": "4.7.5",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.4.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.5.tgz",
"integrity": "sha512-S4pPaO05WC0htkQVcTMTluBZv4g8sEEBJ2ItHN2zJA8syet3iKJidT+s2gYt8d2rBo0Ndlvo1gBLGLNl/Zc9yg==", "integrity": "sha512-So8tBtSTVIq8dTGs/0o92Om3bQhgYUhmzVhnYTqJbx+kW3kSowl/ORiBhJFK3GR6Dj+JyKLptsqQNE4DjZdNhw==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koishijs/utils": "^5.4.5", "@koishijs/utils": "^5.4.5",
"cordis": "^1.3.3", "cordis": "^1.5.5",
"fastest-levenshtein": "^1.0.12", "fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0" "minato": "^1.2.1"
}, },
"engines": { "engines": {
"node": ">=12.0.0" "node": ">=12.0.0"
...@@ -2280,9 +2280,9 @@ ...@@ -2280,9 +2280,9 @@
"peer": true "peer": true
}, },
"node_modules/cordis": { "node_modules/cordis": {
"version": "1.3.3", "version": "1.5.5",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.3.3.tgz", "resolved": "https://registry.npmjs.org/cordis/-/cordis-1.5.5.tgz",
"integrity": "sha512-mpyBWAEVYX8/YHoGWIqaOjrOk6DmtrOWZfsBSinNR+nDBlB39Ks6aC1B5oZ7AdnVoRE2ZYBCGecoHrlxcRxd6w==", "integrity": "sha512-vCw6FA+OLO5e40so5XjhRnIw1ldGH1L74q/wlnuckqFtIq8teYkfnnmlNTS24RJNcgPz2DWdbVgCnoc+5Lc+UQ==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"cosmokit": "^1.1.2" "cosmokit": "^1.1.2"
...@@ -4764,13 +4764,13 @@ ...@@ -4764,13 +4764,13 @@
} }
}, },
"node_modules/koishi": { "node_modules/koishi": {
"version": "4.7.4", "version": "4.7.5",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.4.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.5.tgz",
"integrity": "sha512-VVHf3TyFsTuKrB8IZffxwmwiGx+zyhDo+DNNEFQjvomgqFdGEceAnLxNIf10K48aB1CDmkmi37b6uaURi4Z1Hw==", "integrity": "sha512-Q+czD5hZIsikYwdoKUOZFZIuvXoamikXK6iX4FXnHUPsoXX16PjqX3D1sBKd9JLVMA49KSAGoFW0fN9IMHykYw==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.4", "@koishijs/core": "^4.7.5",
"@koishijs/utils": "^5.4.5", "@koishijs/utils": "^5.4.5",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
...@@ -4779,7 +4779,7 @@ ...@@ -4779,7 +4779,7 @@
"file-type": "^16.5.3", "file-type": "^16.5.3",
"koa": "^2.13.4", "koa": "^2.13.4",
"koa-bodyparser": "^4.3.0", "koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2", "ns-require": "^1.1.4",
"parseurl": "^1.3.3", "parseurl": "^1.3.3",
"path-to-regexp": "^6.2.1", "path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28", "portfinder": "^1.0.28",
...@@ -5005,13 +5005,13 @@ ...@@ -5005,13 +5005,13 @@
} }
}, },
"node_modules/minato": { "node_modules/minato": {
"version": "1.1.0", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.1.0.tgz", "resolved": "https://registry.npmjs.org/minato/-/minato-1.2.1.tgz",
"integrity": "sha512-by1KMWxWlzq0iDV7tMkqS3thDRzYNQoS2ECfX5HRpMGaPQJqSgm5aY9IAky7knwAMoW3cMxQ/IhpvEShQqyj6A==", "integrity": "sha512-BYY18rr82aXAAVtMKkN4yDFsTUtsAmeDmyTRVOXG3mEzG74sNF5TU1nVT1QSC9dpvAcCFFs/vZO2rJoC8vSetQ==",
"peer": true, "peer": true,
"dependencies": { "dependencies": {
"cosmokit": "^1.1.2", "cosmokit": "^1.1.2",
"ns-require": "^1.1.2" "ns-require": "^1.1.4"
} }
}, },
"node_modules/minimatch": { "node_modules/minimatch": {
...@@ -5114,9 +5114,9 @@ ...@@ -5114,9 +5114,9 @@
} }
}, },
"node_modules/ns-require": { "node_modules/ns-require": {
"version": "1.1.3", "version": "1.1.4",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.3.tgz", "resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.4.tgz",
"integrity": "sha512-PFQNIXhJS4ISAcuzkVRq+D9nZDfIYyCRR/9rJQAcL5iRgHfQVc8SsjjGnV43FhttA9F7w+Khk+OGxs8OCMrtWQ==", "integrity": "sha512-Zk25pQj4u5i6DS0vaNO5aSSXewybVqqVVjz8AOxFy9DNPtmu3jlexMz6kUXLV2oB+X6iQeAnHXSzj5Qz/IeDaQ==",
"peer": true "peer": true
}, },
"node_modules/nwsapi": { "node_modules/nwsapi": {
...@@ -7662,15 +7662,15 @@ ...@@ -7662,15 +7662,15 @@
} }
}, },
"@koishijs/core": { "@koishijs/core": {
"version": "4.7.4", "version": "4.7.5",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.4.tgz", "resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.5.tgz",
"integrity": "sha512-S4pPaO05WC0htkQVcTMTluBZv4g8sEEBJ2ItHN2zJA8syet3iKJidT+s2gYt8d2rBo0Ndlvo1gBLGLNl/Zc9yg==", "integrity": "sha512-So8tBtSTVIq8dTGs/0o92Om3bQhgYUhmzVhnYTqJbx+kW3kSowl/ORiBhJFK3GR6Dj+JyKLptsqQNE4DjZdNhw==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koishijs/utils": "^5.4.5", "@koishijs/utils": "^5.4.5",
"cordis": "^1.3.3", "cordis": "^1.5.5",
"fastest-levenshtein": "^1.0.12", "fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0" "minato": "^1.2.1"
} }
}, },
"@koishijs/plugin-adapter-onebot": { "@koishijs/plugin-adapter-onebot": {
...@@ -8684,9 +8684,9 @@ ...@@ -8684,9 +8684,9 @@
"peer": true "peer": true
}, },
"cordis": { "cordis": {
"version": "1.3.3", "version": "1.5.5",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.3.3.tgz", "resolved": "https://registry.npmjs.org/cordis/-/cordis-1.5.5.tgz",
"integrity": "sha512-mpyBWAEVYX8/YHoGWIqaOjrOk6DmtrOWZfsBSinNR+nDBlB39Ks6aC1B5oZ7AdnVoRE2ZYBCGecoHrlxcRxd6w==", "integrity": "sha512-vCw6FA+OLO5e40so5XjhRnIw1ldGH1L74q/wlnuckqFtIq8teYkfnnmlNTS24RJNcgPz2DWdbVgCnoc+5Lc+UQ==",
"peer": true, "peer": true,
"requires": { "requires": {
"cosmokit": "^1.1.2" "cosmokit": "^1.1.2"
...@@ -10561,13 +10561,13 @@ ...@@ -10561,13 +10561,13 @@
} }
}, },
"koishi": { "koishi": {
"version": "4.7.4", "version": "4.7.5",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.4.tgz", "resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.5.tgz",
"integrity": "sha512-VVHf3TyFsTuKrB8IZffxwmwiGx+zyhDo+DNNEFQjvomgqFdGEceAnLxNIf10K48aB1CDmkmi37b6uaURi4Z1Hw==", "integrity": "sha512-Q+czD5hZIsikYwdoKUOZFZIuvXoamikXK6iX4FXnHUPsoXX16PjqX3D1sBKd9JLVMA49KSAGoFW0fN9IMHykYw==",
"peer": true, "peer": true,
"requires": { "requires": {
"@koa/router": "^10.1.1", "@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.4", "@koishijs/core": "^4.7.5",
"@koishijs/utils": "^5.4.5", "@koishijs/utils": "^5.4.5",
"@types/koa": "*", "@types/koa": "*",
"@types/koa__router": "*", "@types/koa__router": "*",
...@@ -10576,7 +10576,7 @@ ...@@ -10576,7 +10576,7 @@
"file-type": "^16.5.3", "file-type": "^16.5.3",
"koa": "^2.13.4", "koa": "^2.13.4",
"koa-bodyparser": "^4.3.0", "koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2", "ns-require": "^1.1.4",
"parseurl": "^1.3.3", "parseurl": "^1.3.3",
"path-to-regexp": "^6.2.1", "path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28", "portfinder": "^1.0.28",
...@@ -10741,13 +10741,13 @@ ...@@ -10741,13 +10741,13 @@
"dev": true "dev": true
}, },
"minato": { "minato": {
"version": "1.1.0", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.1.0.tgz", "resolved": "https://registry.npmjs.org/minato/-/minato-1.2.1.tgz",
"integrity": "sha512-by1KMWxWlzq0iDV7tMkqS3thDRzYNQoS2ECfX5HRpMGaPQJqSgm5aY9IAky7knwAMoW3cMxQ/IhpvEShQqyj6A==", "integrity": "sha512-BYY18rr82aXAAVtMKkN4yDFsTUtsAmeDmyTRVOXG3mEzG74sNF5TU1nVT1QSC9dpvAcCFFs/vZO2rJoC8vSetQ==",
"peer": true, "peer": true,
"requires": { "requires": {
"cosmokit": "^1.1.2", "cosmokit": "^1.1.2",
"ns-require": "^1.1.2" "ns-require": "^1.1.4"
} }
}, },
"minimatch": { "minimatch": {
...@@ -10829,9 +10829,9 @@ ...@@ -10829,9 +10829,9 @@
} }
}, },
"ns-require": { "ns-require": {
"version": "1.1.3", "version": "1.1.4",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.3.tgz", "resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.4.tgz",
"integrity": "sha512-PFQNIXhJS4ISAcuzkVRq+D9nZDfIYyCRR/9rJQAcL5iRgHfQVc8SsjjGnV43FhttA9F7w+Khk+OGxs8OCMrtWQ==", "integrity": "sha512-Zk25pQj4u5i6DS0vaNO5aSSXewybVqqVVjz8AOxFy9DNPtmu3jlexMz6kUXLV2oB+X6iQeAnHXSzj5Qz/IeDaQ==",
"peer": true "peer": true
}, },
"nwsapi": { "nwsapi": {
......
...@@ -67,6 +67,6 @@ ...@@ -67,6 +67,6 @@
"typed-reflector": "^1.0.10" "typed-reflector": "^1.0.10"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.7.4" "koishi": "^4.7.5"
} }
} }
...@@ -24,7 +24,6 @@ import { ...@@ -24,7 +24,6 @@ import {
Events, Events,
FieldCollector, FieldCollector,
I18n, I18n,
Selection,
Session, Session,
} from 'koishi'; } from 'koishi';
import { Metadata } from '../meta/metadata.decorators'; import { Metadata } from '../meta/metadata.decorators';
...@@ -34,7 +33,7 @@ import { ...@@ -34,7 +33,7 @@ import {
applyOptionToCommand, applyOptionToCommand,
registerTemplate, registerTemplate,
} from '../utility'; } from '../utility';
import { Observable } from 'rxjs'; import { selectContext, Selection } from '../def/select';
// Register method // Register method
...@@ -123,7 +122,10 @@ export const OnPrivate = (...values: string[]) => ...@@ -123,7 +122,10 @@ export const OnPrivate = (...values: string[]) =>
OnContext((ctx) => ctx.private(...values)); OnContext((ctx) => ctx.private(...values));
export const OnSelection = (selection: Selection) => export const OnSelection = (selection: Selection) =>
OnContext((ctx) => ctx.select(selection)); OnContext((ctx) => selectContext(ctx, selection));
export const Isolate = (...values: string[]) =>
OnContext((ctx) => ctx.isolate(values));
// Command definition // Command definition
......
export * from './interfaces'; export * from './interfaces';
export * from './constants'; export * from './constants';
export * from './select';
...@@ -7,12 +7,12 @@ import { ...@@ -7,12 +7,12 @@ import {
Events, Events,
I18n, I18n,
Plugin, Plugin,
Selection,
} from 'koishi'; } from 'koishi';
import type { DefaultContext, DefaultState, ParameterizedContext } from 'koa'; import type { DefaultContext, DefaultState, ParameterizedContext } from 'koa';
import type { RouterParamContext } from '@koa/router'; import type { RouterParamContext } from '@koa/router';
import { CommandPut } from '../registry'; import { CommandPut } from '../registry';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { Selection } from './select';
export interface Type<T = any> extends Function { export interface Type<T = any> extends Function {
new (...args: any[]): T; new (...args: any[]): T;
......
import { Context, makeArray, MaybeArray } from 'koishi';
const selectors = [
'user',
'guild',
'channel',
'self',
'private',
'platform',
] as const;
export type SelectorType = typeof selectors[number];
export type SelectorValue = boolean | MaybeArray<string | number>;
export type BaseSelection = { [K in SelectorType]?: SelectorValue };
export interface Selection extends BaseSelection {
and?: Selection[];
or?: Selection[];
not?: Selection;
}
export function selectContext(root: Context, options: Selection) {
let ctx = root;
// basic selectors
for (const type of selectors) {
const value = options[type];
if (value === true) {
ctx = ctx[type]();
} else if (value === false) {
ctx = ctx.exclude(ctx[type]());
} else if (value !== undefined) {
// we turn everything into string
ctx = ctx[type](...makeArray(value).map((item) => '' + item));
}
}
// intersect
if (options.and) {
for (const selection of options.and) {
ctx = ctx.intersect(selectContext(root, selection));
}
}
// union
if (options.or) {
let ctx2: Context = ctx.never();
for (const selection of options.or) {
ctx2 = ctx2.union(selectContext(root, selection));
}
ctx = ctx.intersect(ctx2);
}
// exclude
if (options.not) {
ctx = ctx.exclude(selectContext(root, options.not));
}
return ctx;
}
export * from './utility'; export * from './utility';
export * from './native-type-mapping'; export * from './native-type-mapping';
export * from '../def/select';
...@@ -4,6 +4,7 @@ import { ...@@ -4,6 +4,7 @@ import {
ContextSelector, ContextSelector,
OnContextFunction, OnContextFunction,
ParamRenderer, ParamRenderer,
selectContext,
TemplateConfig, TemplateConfig,
} from '../def'; } from '../def';
import { applyNativeTypeToArg } from './native-type-mapping'; import { applyNativeTypeToArg } from './native-type-mapping';
...@@ -19,7 +20,7 @@ export function applySelector( ...@@ -19,7 +20,7 @@ export function applySelector(
} }
let targetCtx = ctx; let targetCtx = ctx;
if (selector.select) { if (selector.select) {
targetCtx = targetCtx.select(selector.select); targetCtx = selectContext(ctx, selector.select);
} }
if (selector.useSelector) { if (selector.useSelector) {
targetCtx = selector.useSelector(targetCtx) || targetCtx; targetCtx = selector.useSelector(targetCtx) || targetCtx;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment