Commit 656e86c8 authored by nanahira's avatar nanahira

support picsource without thirdeye

parent fd74849b
import { Awaitable, Context } from 'koishi';
import { Awaitable, Context, Schema } from 'koishi';
import {
DefinePlugin,
MultiInstancePlugin,
RegisterSchema,
SchemaProperty,
} from 'koishi-thirdeye';
import { PicResult, PicSourceConfig, PicSourcePlugin } from '../src';
import {
PicResult,
PicSource,
PicSourceConfig,
PicSourceInfo,
PicSourcePlugin,
PlainPicSourcePlugin,
} from '../src';
@RegisterSchema()
class Config {
......@@ -28,6 +35,24 @@ class TestPicSourcePlugin extends PicSourcePlugin(Config) {
}
}
const plainBase = PlainPicSourcePlugin({
name: Schema.string().default('zh-CN'),
endpoint: Schema.string().default('https://cdn02.moecube.com:444'),
});
class TestPlainPicSourcePlugin extends plainBase {
static using = ['pics'] as const;
static Config = plainBase.Config;
randomPic(picTags: string[]): Awaitable<PicResult> {
return {
url: `${this.config.endpoint}/images/ygopro-images-${this.name}/${
picTags[0] || '10000'
}.jpg`,
description: picTags[0] || '10000',
};
}
}
@DefinePlugin()
export class TestMultiPicSourcePlugin extends MultiInstancePlugin(
TestPicSourcePlugin,
......@@ -35,17 +60,10 @@ export class TestMultiPicSourcePlugin extends MultiInstancePlugin(
export default class ExtrasInDev {
constructor(ctx: Context) {
ctx.plugin(TestMultiPicSourcePlugin, {
instances: [
{
// name: 'zh-CN',
isDefault: true,
endpoint: 'https://cdn02.moecube.com:444',
},
{
name: 'en-US',
},
],
ctx.plugin(TestPlainPicSourcePlugin, {
// name: 'zh-CN',
isDefault: true,
// endpoint: 'https://cdn02.moecube.com:444',
});
}
......
import { Awaitable, Logger } from 'koishi';
import { Awaitable, Context, Logger, Schema } from 'koishi';
import { PicMiddlewareConfig } from './config';
import {
BasePlugin,
......@@ -6,8 +6,10 @@ import {
Inject,
InjectLogger,
LifecycleEvents,
PartialDeep,
schemaFromClass,
} from 'koishi-thirdeye';
import PicsContainer from './index';
import PicsContainer, { PicMiddlewareInfo } from './index';
import { PicMiddleware, PicNext } from './def';
export class BasePicMiddlewarePlugin
......@@ -34,3 +36,33 @@ export const PicMiddlewarePlugin = CreatePluginFactory(
BasePicMiddlewarePlugin,
PicMiddlewareConfig,
);
export function PlainPicMiddlewarePlugin<C>(dict: {
[K in keyof C]: Schema<C[K]>;
}) {
const Config = schemaFromClass(PicMiddlewareConfig) as Schema<
PartialDeep<PicMiddlewareConfig & C>,
PicMiddlewareConfig & C
>;
Object.assign(Config.dict, dict);
return class PlainPicMiddlewarePluginBase implements PicMiddleware {
name?: string;
prepend?: boolean;
config: PicMiddlewareInfo & C;
static Config = Config;
static using = ['pics'] as const;
constructor(
public ctx: Context,
config: PartialDeep<C & PicMiddlewareInfo>,
) {
this.config = config as PicMiddlewareInfo & C;
this.name = config.name;
this.prepend = config.prepend;
ctx.pics.middleware(this);
}
use(url: string, next: PicNext): Awaitable<string> {
return next(url);
}
};
}
import { Context, Awaitable, Logger } from 'koishi';
import { Context, Awaitable, Logger, Schema } from 'koishi';
import {
PartialDeep,
InjectConfig,
Inject,
InjectLogger,
CreatePluginFactory,
schemaFromClass,
} from 'koishi-thirdeye';
import PicsContainer from '.';
import { PicSourceConfig } from './config';
......@@ -17,6 +18,15 @@ export class PicSource implements PicSourceInfo {
name = 'default';
description = '';
isDefault = false;
applyConfig(src: Partial<PicSourceInfo>) {
this.name = src.name;
this.tags ??= src.tags;
this.weight ??= src.weight;
this.description ??= src.description;
this.isDefault ??= src.isDefault;
}
randomPic(picTags: string[]): Awaitable<PicResult> {
// For override
throw new Error(`Not implemented`);
......@@ -64,3 +74,24 @@ export const PicSourcePlugin = CreatePluginFactory(
BasePicSourcePlugin,
PicSourceConfig,
);
export function PlainPicSourcePlugin<C>(dict: {
[K in keyof C]: Schema<C[K]>;
}) {
const Config = schemaFromClass(PicSourceConfig) as Schema<
PartialDeep<PicSourceConfig & C>,
PicSourceConfig & C
>;
Object.assign(Config.dict, dict);
return class PlainPicSourcePluginBase extends PicSource {
config: PicSourceInfo & C;
constructor(ctx: Context, config: PartialDeep<C & PicSourceInfo>) {
super(ctx);
this.config = config as PicSourceInfo & C;
this.applyConfig(config);
ctx.pics.addSource(this);
}
static Config = Config;
static using = ['pics'] as const;
};
}
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