mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: fix types
This commit is contained in:
1 parent
d27a1499d2
commit
1c3dc161b6
13 files changed
+48
-62
No files matched your search
@@ -1,7 +0,0 @@
|
|||||||
## Changes for hydrooj@3:
|
|
||||||
|
|
||||||
- 重构了 server 结构:
|
|
||||||
- - 移除了 `Handler.init` `Handler.finish` 方法。
|
|
||||||
- - 移除了 `handler/finish` 钩子。
|
|
||||||
- - 移除了 `requireCsrfToken` 。
|
|
||||||
- - 移除了 `sockjs`,转为使用原生 `WebSocket` 。
|
|
||||||
@@ -21,7 +21,7 @@ function provideModule<T extends keyof ModuleInterfaces>(type: T, id: string, mo
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Context {
|
export interface Context {
|
||||||
[Context.events]: Events<this>;
|
[Context.events]: Events<Context>;
|
||||||
loader: Loader;
|
loader: Loader;
|
||||||
Route: typeof import('./service/server').Route;
|
Route: typeof import('./service/server').Route;
|
||||||
Connection: typeof import('./service/server').Connection;
|
Connection: typeof import('./service/server').Connection;
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ function locateFile(basePath: string, filenames: string[]) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const getLoader = (type: string, filename: string) => async function loader(pending: string[], fail: string[], ctx: Context) {
|
type LoadTask = 'handler' | 'model' | 'addon' | 'lib' | 'script' | 'service';
|
||||||
|
const getLoader = (type: LoadTask, filename: string) => async function loader(pending: string[], fail: string[], ctx: Context) {
|
||||||
for (const i of pending) {
|
for (const i of pending) {
|
||||||
const p = locateFile(i, [`${filename}.ts`, `${filename}.js`]);
|
const p = locateFile(i, [`${filename}.ts`, `${filename}.js`]);
|
||||||
if (p && !fail.includes(i)) {
|
if (p && !fail.includes(i)) {
|
||||||
|
|||||||
@@ -851,7 +851,6 @@ export class ProblemSolutionHandler extends ProblemDetailHandler {
|
|||||||
this.response.body = {
|
this.response.body = {
|
||||||
psdocs, page, pcount, pscount, udict, pssdict, pdoc: this.pdoc, sid,
|
psdocs, page, pcount, pscount, udict, pssdict, pdoc: this.pdoc, sid,
|
||||||
};
|
};
|
||||||
await bus.parallel('handler/solution/get', this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@param('content', Types.Content)
|
@param('content', Types.Content)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { omit } from 'lodash';
|
import { omit } from 'lodash';
|
||||||
import { FilterQuery, ObjectID } from 'mongodb';
|
import { FilterQuery, ObjectID } from 'mongodb';
|
||||||
import { BlogDoc } from '../interface';
|
import { BlogDoc } from '../interface';
|
||||||
import * as bus from '../service/bus';
|
|
||||||
import { NumberKeys } from '../typeutils';
|
import { NumberKeys } from '../typeutils';
|
||||||
import * as document from './document';
|
import * as document from './document';
|
||||||
|
|
||||||
@@ -18,13 +17,11 @@ export async function add(
|
|||||||
updateAt: new Date(),
|
updateAt: new Date(),
|
||||||
views: 0,
|
views: 0,
|
||||||
};
|
};
|
||||||
await bus.parallel('blog/before-add', payload);
|
|
||||||
const res = await document.add(
|
const res = await document.add(
|
||||||
'system', payload.content!, payload.owner!, document.TYPE_BLOG,
|
'system', payload.content!, payload.owner!, document.TYPE_BLOG,
|
||||||
null, null, null, omit(payload, ['domainId', 'content', 'owner']),
|
null, null, null, omit(payload, ['domainId', 'content', 'owner']),
|
||||||
);
|
);
|
||||||
payload.docId = res;
|
payload.docId = res;
|
||||||
await bus.emit('blog/add', payload);
|
|
||||||
return payload.docId;
|
return payload.docId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ async function updateSort() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apply(ctx: Context) {
|
export function apply(ctx: Context) {
|
||||||
ctx.using(['worker'], async ({ worker }) => {
|
ctx.using(['worker'], async ({ worker }) => {
|
||||||
worker.addHandler('discussion.sort', updateSort);
|
worker.addHandler('discussion.sort', updateSort);
|
||||||
if (!await TaskModel.count({ type: 'schedule', subType: 'discussion.sort' })) {
|
if (!await TaskModel.count({ type: 'schedule', subType: 'discussion.sort' })) {
|
||||||
@@ -305,7 +305,7 @@ export async function apply(ctx: Context) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
global.Hydro.model.DiscussionModel = {
|
global.Hydro.model.discussion = {
|
||||||
typeDisplay,
|
typeDisplay,
|
||||||
PROJECTION_LIST,
|
PROJECTION_LIST,
|
||||||
PROJECTION_PUBLIC,
|
PROJECTION_PUBLIC,
|
||||||
|
|||||||
@@ -2,15 +2,15 @@
|
|||||||
import { omit, sum } from 'lodash';
|
import { omit, sum } from 'lodash';
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import {
|
import {
|
||||||
Collection, FilterQuery, MatchKeysAndValues,
|
FilterQuery, MatchKeysAndValues,
|
||||||
ObjectID, OnlyFieldsOfType, PushOperator,
|
ObjectID, OnlyFieldsOfType, PushOperator,
|
||||||
UpdateQuery,
|
UpdateQuery,
|
||||||
} from 'mongodb';
|
} from 'mongodb';
|
||||||
|
import { Context } from '../context';
|
||||||
import { ProblemNotFoundError } from '../error';
|
import { ProblemNotFoundError } from '../error';
|
||||||
import {
|
import {
|
||||||
FileInfo, JudgeMeta, JudgeRequest, ProblemConfigFile, RecordDoc,
|
FileInfo, JudgeMeta, JudgeRequest, ProblemConfigFile, RecordDoc,
|
||||||
} from '../interface';
|
} from '../interface';
|
||||||
import * as bus from '../service/bus';
|
|
||||||
import db from '../service/db';
|
import db from '../service/db';
|
||||||
import { MaybeArray, NumberKeys } from '../typeutils';
|
import { MaybeArray, NumberKeys } from '../typeutils';
|
||||||
import { ArgMethod, buildProjection, Time } from '../utils';
|
import { ArgMethod, buildProjection, Time } from '../utils';
|
||||||
@@ -18,8 +18,8 @@ import { STATUS } from './builtin';
|
|||||||
import problem from './problem';
|
import problem from './problem';
|
||||||
import task from './task';
|
import task from './task';
|
||||||
|
|
||||||
class RecordModel {
|
export default class RecordModel {
|
||||||
static coll: Collection<RecordDoc> = db.collection('record');
|
static coll = db.collection('record');
|
||||||
static PROJECTION_LIST: (keyof RecordDoc)[] = [
|
static PROJECTION_LIST: (keyof RecordDoc)[] = [
|
||||||
'_id', 'score', 'time', 'memory', 'lang',
|
'_id', 'score', 'time', 'memory', 'lang',
|
||||||
'uid', 'pid', 'rejudged', 'progress', 'domainId',
|
'uid', 'pid', 'rejudged', 'progress', 'domainId',
|
||||||
@@ -230,15 +230,16 @@ class RecordModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark problem as deleted
|
export function apply(ctx: Context) {
|
||||||
bus.on('problem/delete', (domainId, docId) => RecordModel.coll.updateMany({ domainId, pid: docId }, { $set: { pid: -1 } }));
|
// Mark problem as deleted
|
||||||
bus.on('domain/delete', (domainId) => RecordModel.coll.deleteMany({ domainId }));
|
ctx.on('problem/delete', (domainId, docId) => RecordModel.coll.updateMany({ domainId, pid: docId }, { $set: { pid: -1 } }));
|
||||||
bus.on('ready', () => db.ensureIndexes(
|
ctx.on('domain/delete', (domainId) => RecordModel.coll.deleteMany({ domainId }));
|
||||||
RecordModel.coll,
|
ctx.on('ready', () => db.ensureIndexes(
|
||||||
{ key: { domainId: 1, contest: 1, _id: -1 }, name: 'basic' },
|
RecordModel.coll,
|
||||||
{ key: { domainId: 1, contest: 1, uid: 1, _id: -1 }, name: 'withUser' },
|
{ key: { domainId: 1, contest: 1, _id: -1 }, name: 'basic' },
|
||||||
{ key: { domainId: 1, contest: 1, pid: 1, _id: -1 }, name: 'withProblem' },
|
{ key: { domainId: 1, contest: 1, uid: 1, _id: -1 }, name: 'withUser' },
|
||||||
));
|
{ key: { domainId: 1, contest: 1, pid: 1, _id: -1 }, name: 'withProblem' },
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
export default RecordModel;
|
|
||||||
global.Hydro.model.record = RecordModel;
|
global.Hydro.model.record = RecordModel;
|
||||||
@@ -187,7 +187,7 @@ export async function apply(ctx: Context) {
|
|||||||
const handleEvent = async (doc: EventDoc) => {
|
const handleEvent = async (doc: EventDoc) => {
|
||||||
const payload = JSON.parse(doc.payload);
|
const payload = JSON.parse(doc.payload);
|
||||||
if (process.send) process.send({ type: 'hydro:broadcast', data: { event: doc.event, payload } });
|
if (process.send) process.send({ type: 'hydro:broadcast', data: { event: doc.event, payload } });
|
||||||
await bus.parallel(doc.event, ...payload);
|
await (bus.parallel as any)(doc.event, ...payload);
|
||||||
};
|
};
|
||||||
stream.on('change', async (change) => {
|
stream.on('change', async (change) => {
|
||||||
if (change.operationType !== 'insert') return;
|
if (change.operationType !== 'insert') return;
|
||||||
|
|||||||
@@ -12,22 +12,26 @@ import type {
|
|||||||
Tdoc, TrainingDoc, User,
|
Tdoc, TrainingDoc, User,
|
||||||
} from '../interface';
|
} from '../interface';
|
||||||
import type { DocType } from '../model/document';
|
import type { DocType } from '../model/document';
|
||||||
import type { Handler } from './server';
|
import type { ConnectionHandler, Handler } from './server';
|
||||||
|
|
||||||
export type Disposable = () => void;
|
export type Disposable = () => void;
|
||||||
export type VoidReturn = Promise<any> | any;
|
export type VoidReturn = Promise<any> | any;
|
||||||
|
type HookType = 'before-prepare' | 'before' | 'after' | 'finish';
|
||||||
|
type ModuleCategories = 'lib' | 'locale' | 'template' | 'script' | 'model' | 'setting' | 'handler' | 'service' | 'addon';
|
||||||
|
type LifecycleEvents = Record<`app/load/${ModuleCategories}`, () => VoidReturn>;
|
||||||
|
type MapHandlerEvents<N extends string, H extends Handler> = Record<`handler/${HookType}/${N}`, (thisArg: H) => VoidReturn>;
|
||||||
|
type KnownHandlerEvents =
|
||||||
|
MapHandlerEvents<'UserRegister', UserRegisterHandler>
|
||||||
|
& MapHandlerEvents<'ProblemSolution', ProblemSolutionHandler>;
|
||||||
|
type HandlerEvents =
|
||||||
|
KnownHandlerEvents
|
||||||
|
& Record<`handler/${HookType}/${string}`, (thisArg: Handler & Record<string, any>) => VoidReturn>
|
||||||
|
& Record<`handler/${HookType}`, (thisArg: Handler) => VoidReturn>
|
||||||
|
& Record<`connection/${'create' | 'active' | 'close'}`, (thisArg: ConnectionHandler) => VoidReturn>;
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/naming-convention */
|
/* eslint-disable @typescript-eslint/naming-convention */
|
||||||
export interface EventMap {
|
export interface EventMap extends LifecycleEvents, HandlerEvents {
|
||||||
'app/started': () => void
|
'app/started': () => void
|
||||||
'app/load/lib': () => VoidReturn
|
|
||||||
'app/load/locale': () => VoidReturn
|
|
||||||
'app/load/template': () => VoidReturn
|
|
||||||
'app/load/script': () => VoidReturn
|
|
||||||
'app/load/setting': () => VoidReturn
|
|
||||||
'app/load/model': () => VoidReturn
|
|
||||||
'app/load/handler': () => VoidReturn
|
|
||||||
'app/load/service': () => VoidReturn
|
|
||||||
'app/ready': () => VoidReturn
|
'app/ready': () => VoidReturn
|
||||||
'app/exit': () => VoidReturn
|
'app/exit': () => VoidReturn
|
||||||
|
|
||||||
@@ -59,16 +63,7 @@ export interface EventMap {
|
|||||||
|
|
||||||
'handler/create': (thisArg: Handler) => VoidReturn
|
'handler/create': (thisArg: Handler) => VoidReturn
|
||||||
'handler/init': (thisArg: Handler) => VoidReturn
|
'handler/init': (thisArg: Handler) => VoidReturn
|
||||||
'handler/before-prepare/UserRegister': (thisArg: UserRegisterHandler) => VoidReturn
|
|
||||||
'handler/before-prepare': (thisArg: Handler) => VoidReturn
|
|
||||||
'handler/before/UserRegister': (thisArg: UserRegisterHandler) => VoidReturn
|
|
||||||
'handler/before': (thisArg: Handler) => VoidReturn
|
|
||||||
'handler/after/UserRegister': (thisArg: UserRegisterHandler) => VoidReturn
|
|
||||||
'handler/after': (thisArg: Handler) => VoidReturn
|
|
||||||
'handler/finish/UserRegister': (thisArg: UserRegisterHandler) => VoidReturn
|
|
||||||
'handler/finish': (thisArg: Handler) => VoidReturn
|
|
||||||
'handler/error': (thisArg: Handler, e: Error) => VoidReturn
|
'handler/error': (thisArg: Handler, e: Error) => VoidReturn
|
||||||
'handler/solution/get': (thisArg: ProblemSolutionHandler) => VoidReturn
|
|
||||||
|
|
||||||
'discussion/before-add': (payload: Partial<DiscussionDoc>) => VoidReturn
|
'discussion/before-add': (payload: Partial<DiscussionDoc>) => VoidReturn
|
||||||
'discussion/add': (payload: Partial<DiscussionDoc>) => VoidReturn
|
'discussion/add': (payload: Partial<DiscussionDoc>) => VoidReturn
|
||||||
@@ -90,6 +85,8 @@ export interface EventMap {
|
|||||||
'contest/before-add': (payload: Partial<Tdoc>) => VoidReturn
|
'contest/before-add': (payload: Partial<Tdoc>) => VoidReturn
|
||||||
'contest/add': (payload: Partial<Tdoc>, id: ObjectID) => VoidReturn
|
'contest/add': (payload: Partial<Tdoc>, id: ObjectID) => VoidReturn
|
||||||
|
|
||||||
|
'oplog/log': (type: string, handler: Handler, args: any, data: any) => VoidReturn;
|
||||||
|
|
||||||
'training/list': (query: FilterQuery<TrainingDoc>, handler: any) => VoidReturn
|
'training/list': (query: FilterQuery<TrainingDoc>, handler: any) => VoidReturn
|
||||||
'training/get': (tdoc: TrainingDoc, handler: any) => VoidReturn
|
'training/get': (tdoc: TrainingDoc, handler: any) => VoidReturn
|
||||||
|
|
||||||
@@ -104,7 +101,7 @@ export function apply(ctx: Context) {
|
|||||||
pm2.launchBus((err, bus) => {
|
pm2.launchBus((err, bus) => {
|
||||||
if (err) throw new Error();
|
if (err) throw new Error();
|
||||||
bus.on('hydro:broadcast', (packet) => {
|
bus.on('hydro:broadcast', (packet) => {
|
||||||
ctx.parallel(packet.data.event, ...packet.data.payload);
|
(ctx.parallel as any)(packet.data.event, ...packet.data.payload);
|
||||||
});
|
});
|
||||||
ctx.on('bus/broadcast', (event, payload) => {
|
ctx.on('bus/broadcast', (event, payload) => {
|
||||||
process.send({ type: 'hydro:broadcast', data: { event, payload } });
|
process.send({ type: 'hydro:broadcast', data: { event, payload } });
|
||||||
@@ -121,11 +118,11 @@ export default app;
|
|||||||
export const on = (a, b, c?) => app.on(a, b, c);
|
export const on = (a, b, c?) => app.on(a, b, c);
|
||||||
export const off = (a, b) => app.off(a, b);
|
export const off = (a, b) => app.off(a, b);
|
||||||
export const once = (a, b, c?) => app.once(a, b, c);
|
export const once = (a, b, c?) => app.once(a, b, c);
|
||||||
export const parallel = (a, ...b) => app.parallel(a, ...b);
|
export const parallel = app.parallel.bind(app);
|
||||||
export const emit = (a, ...b) => app.parallel(a, ...b);
|
export const emit = app.parallel.bind(app);
|
||||||
export const bail = (a, ...b) => app.bail(a, ...b);
|
export const bail = app.bail.bind(app);
|
||||||
// For backward compatibility
|
// For backward compatibility
|
||||||
export const serial: any = (a, ...b) => app.parallel(a, ...b);
|
export const serial: any = app.parallel.bind(app);
|
||||||
export const broadcast = (a, ...b) => app.broadcast(a, ...b);
|
export const broadcast = app.broadcast.bind(app);
|
||||||
|
|
||||||
global.Hydro.service.bus = app as any;
|
global.Hydro.service.bus = app as any;
|
||||||
@@ -2,9 +2,10 @@
|
|||||||
import {
|
import {
|
||||||
Collection, Db, IndexSpecification, MongoClient,
|
Collection, Db, IndexSpecification, MongoClient,
|
||||||
} from 'mongodb';
|
} from 'mongodb';
|
||||||
import { Collections } from '../interface';
|
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
import options from '../options';
|
import options from '../options';
|
||||||
|
// Note: this might be re-declared so we have to use Collections here
|
||||||
|
import type { Collections } from '../plugin-api';
|
||||||
import * as bus from './bus';
|
import * as bus from './bus';
|
||||||
|
|
||||||
const logger = new Logger('mongo');
|
const logger = new Logger('mongo');
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ export class Handler extends HandlerCommon {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function bail(name: string, ...args: any[]) {
|
async function bail(name: string, ...args: any[]) {
|
||||||
const r = await bus.bail(name, ...args);
|
const r = await (bus.bail as any)(name, ...args);
|
||||||
if (r instanceof Error) throw r;
|
if (r instanceof Error) throw r;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import yaml from 'js-yaml';
|
|||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import Schema from 'schemastery';
|
import Schema from 'schemastery';
|
||||||
import { Logger } from './logger';
|
import { Logger } from './logger';
|
||||||
import * as bus from './service/bus';
|
|
||||||
import { NestKeys } from './typeutils';
|
import { NestKeys } from './typeutils';
|
||||||
|
|
||||||
const defaultPath = process.env.CI ? '/tmp/file'
|
const defaultPath = process.env.CI ? '/tmp/file'
|
||||||
@@ -64,7 +63,7 @@ export async function saveConfig(config: any) {
|
|||||||
Schema.intersect(SystemSettings)(config);
|
Schema.intersect(SystemSettings)(config);
|
||||||
const value = yaml.dump(config);
|
const value = yaml.dump(config);
|
||||||
await global.Hydro.service.db.collection('system').updateOne({ _id: 'config' }, { $set: { value } }, { upsert: true });
|
await global.Hydro.service.db.collection('system').updateOne({ _id: 'config' }, { $set: { value } }, { upsert: true });
|
||||||
bus.broadcast('config/update');
|
loadConfig();
|
||||||
}
|
}
|
||||||
export async function setConfig(key: string, value: any) {
|
export async function setConfig(key: string, value: any) {
|
||||||
const path = key.split('.');
|
const path = key.split('.');
|
||||||
@@ -109,5 +108,3 @@ export function requestConfig<T, S>(s: Schema<T, S>): {
|
|||||||
const builtin = requestConfig(builtinSettings);
|
const builtin = requestConfig(builtinSettings);
|
||||||
export const builtinConfig = builtin.config;
|
export const builtinConfig = builtin.config;
|
||||||
export const setBuiltinConfig = builtin.setConfig;
|
export const setBuiltinConfig = builtin.setConfig;
|
||||||
|
|
||||||
bus.on('config/update', loadConfig);
|
|
||||||
@@ -13,7 +13,7 @@ import { join, resolve } from 'path';
|
|||||||
import convert from 'schemastery-jsonschema';
|
import convert from 'schemastery-jsonschema';
|
||||||
import markdown from './backendlib/markdown';
|
import markdown from './backendlib/markdown';
|
||||||
|
|
||||||
declare module 'hydrooj' {
|
declare module 'hydrooj/src/interface' {
|
||||||
interface UI {
|
interface UI {
|
||||||
esbuildPlugins?: esbuild.Plugin[]
|
esbuildPlugins?: esbuild.Plugin[]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user