diff --git a/package.json b/package.json index c339c0585..beb1ab94a 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "pub": "node build/publish", "jest": "node build/jest", "start": "node --async-stack-traces --trace-deprecation --enable-source-maps build/start", - "clean": "zsh -c \"rm -r tsconfig.build.tsbuildinfo ./packages/*/tsconfig.tsbuildinfo ./packages/*/dist ./coverage\"" + "clean": "zsh -c \"rm -r tsconfig.build.tsbuildinfo ./packages/*/tsconfig.tsbuildinfo ./packages/*/dist ./.coverage\"" }, "version": "1.0.0", "license": "AGPL-3.0-only", diff --git a/packages/html2md/lib.ts b/packages/html2md/lib.ts deleted file mode 100644 index a4cf55161..000000000 --- a/packages/html2md/lib.ts +++ /dev/null @@ -1,20 +0,0 @@ -import 'hydrooj'; -import TurndownService from 'turndown'; - -declare module 'hydrooj' { - interface Lib { - convertHTML: (str: string) => string, - } -} - -const turndownService = new TurndownService({ - headingStyle: 'atx', - codeBlockStyle: 'fenced', - bulletListMarker: '-', -}); - -export function convertHTML(html: string) { - return turndownService.turndown(html); -} - -global.Hydro.lib.convertHTML = convertHTML; diff --git a/packages/html2md/package.json b/packages/html2md/package.json deleted file mode 100644 index 7b60ab181..000000000 --- a/packages/html2md/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "@hydrooj/html2md", - "version": "1.0.2", - "description": "turn html into markdown", - "main": "lib.js", - "typings": "lib.d.ts", - "repository": "https://github.com/hydro-dev/Hydro.git", - "author": "undefined ", - "license": "AGPL-3.0-only", - "dependencies": { - "turndown": "^6.0.0" - }, - "devDependencies": { - "@types/turndown": "^5.0.0" - }, - "peerDependencies": { - "hydrooj": "^2.12.22" - }, - "scripts": { - "build": "tsc", - "lint": "eslint . --ext ts --fix" - } -} \ No newline at end of file diff --git a/packages/html2md/test/lib.spec.ts b/packages/html2md/test/lib.spec.ts deleted file mode 100644 index eb19eca43..000000000 --- a/packages/html2md/test/lib.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import 'hydrooj'; -import { convertHTML } from '@hydrooj/html2md/lib'; - -describe('html2md', () => { - test('convertHTML', () => { - expect(convertHTML('

Test

test

')).toStrictEqual('# Test\n\ntest'); - }); -}); diff --git a/packages/html2md/tsconfig.json b/packages/html2md/tsconfig.json deleted file mode 100644 index aa7d73857..000000000 --- a/packages/html2md/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compilerOptions": { - "target": "es2019", - "module": "commonjs", - "esModuleInterop": true, - "moduleResolution": "node", - "declaration": true, - "sourceMap": true, - "composite": true, - "strictBindCallApply": true, - "experimentalDecorators": true, - "outDir": ".", - "rootDir": "." - }, - "include": [ - "*.ts" - ], - "exclude": [] -} \ No newline at end of file diff --git a/packages/hydrooj/package.json b/packages/hydrooj/package.json index 8b831a3be..fe2f88391 100644 --- a/packages/hydrooj/package.json +++ b/packages/hydrooj/package.json @@ -1,6 +1,6 @@ { "name": "hydrooj", - "version": "2.13.9", + "version": "2.13.10", "bin": "bin/hydrooj.js", "main": "dist/loader.js", "typings": "dist/loader.d.ts", diff --git a/packages/hydrooj/src/check.ts b/packages/hydrooj/src/check.ts index c2e62047d..67ee9a8a8 100644 --- a/packages/hydrooj/src/check.ts +++ b/packages/hydrooj/src/check.ts @@ -43,7 +43,7 @@ const checks: Dictionary = { }, }; -export async function start(ctx, log, warn, error, cb: (id: string) => any) { +export async function start(ctx, log, warn, error, cb: (id: string) => void) { const id = String.random(6); cb(id); for (const name in checks) { diff --git a/packages/hydrooj/src/interface.ts b/packages/hydrooj/src/interface.ts index a516c4a7c..df2efdd69 100644 --- a/packages/hydrooj/src/interface.ts +++ b/packages/hydrooj/src/interface.ts @@ -5,6 +5,10 @@ import { Dictionary, NumericDictionary } from 'lodash'; export type NumberKeys = { [K in keyof O]: number extends O[K] ? K : never +}[keyof O]; + +export type ArrayKeys = { + [K in keyof O]: P[] extends O[K] ? K : never }[keyof O] export interface System { @@ -157,7 +161,7 @@ export type ProblemConfig = LocalProblemConfig | RemoteProblemConfig; export interface Pdoc { _id: ObjectID, domainId: string, - docType: number, + docType: 10, docId: number, pid: string, owner: number, @@ -181,14 +185,21 @@ export interface Pdoc { export type Pdict = NumericDictionary; -export interface ProblemStatusDoc { +export interface StatusDoc { _id: ObjectID, - docId: number, + docId: any, docType: number, domainId: string, uid: number, +} + +export interface ProblemStatusDoc extends StatusDoc { + docId: number, + docType: 10, rid?: ObjectID, status?: number, + nSubmit?: number, + nAccept?: number, star?: boolean, } diff --git a/packages/hydrooj/src/model/blacklist.ts b/packages/hydrooj/src/model/blacklist.ts index 54e69a49e..c6e3f4b22 100644 --- a/packages/hydrooj/src/model/blacklist.ts +++ b/packages/hydrooj/src/model/blacklist.ts @@ -1,9 +1,7 @@ -import { Collection } from 'mongodb'; import * as db from '../service/db'; import * as bus from '../service/bus'; -import { Bdoc } from '../interface'; -const coll: Collection = db.collection('blacklist'); +const coll = db.collection('blacklist'); export async function add(ip: string) { /** diff --git a/packages/hydrooj/src/model/document.ts b/packages/hydrooj/src/model/document.ts index 97a8f5d70..d31aa8f54 100644 --- a/packages/hydrooj/src/model/document.ts +++ b/packages/hydrooj/src/model/document.ts @@ -1,9 +1,11 @@ import assert from 'assert'; -import { ObjectID, Cursor, FilterQuery } from 'mongodb'; +import { + ObjectID, Cursor, FilterQuery, UpdateQuery, +} from 'mongodb'; import * as db from '../service/db'; import * as bus from '../service/bus'; import { - Pdoc, Ddoc, Ufdoc, Drdoc, Tdoc, TrainingDoc, NumberKeys, + Pdoc, Ddoc, Ufdoc, Drdoc, Tdoc, TrainingDoc, NumberKeys, ArrayKeys, ProblemStatusDoc, } from '../interface'; type DocID = ObjectID | string | number; @@ -35,6 +37,11 @@ export interface DocType { [TYPE_HOMEWORK]: Tdoc, } +export interface DocStatusType { + [TYPE_PROBLEM]: ProblemStatusDoc, + [key: number]: any +} + export async function add( domainId: string, content: string, owner: number, docType: K, docId: DocType[K]['docId'], @@ -104,8 +111,8 @@ export function deleteMulti( return coll.deleteMany({ ...query, domainId, docType }); } -export function deleteMultiStatus( - domainId: string, docType: K, query?: FilterQuery, +export function deleteMultiStatus( + domainId: string, docType: K, query?: FilterQuery, ) { return collStatus.deleteMany({ ...query, domainId, docType }); } @@ -128,8 +135,6 @@ export async function inc( return res.value; } -type Q = NumberKeys - export async function incAndSet( domainId: string, docType: K, docId: DocType[K]['docId'], key: NumberKeys, value: number, args: Partial, @@ -169,7 +174,7 @@ export async function push( export async function pull( domainId: string, docType: K, docId: DocType[K]['docId'], - setKey: string, contents: string[], + setKey: ArrayKeys, contents: string[], ): Promise { const res = await coll.findOneAndUpdate( { domainId, docType, docId }, @@ -179,10 +184,10 @@ export async function pull( return res.value; } -export async function deleteSub( - domainId: string, docType: number, docId: DocID, - key: string, subId: ObjectID, -) { +export async function deleteSub( + domainId: string, docType: K, docId: DocType[K]['docId'], + key: ArrayKeys, subId: ObjectID, +): Promise { const res = await coll.findOneAndUpdate( { domainId, docType, docId }, { $pull: { [key]: { _id: subId } } }, @@ -191,10 +196,10 @@ export async function deleteSub( return res.value; } -export async function getSub( - domainId: string, docType: number, docId: DocID, - key: string, subId: ObjectID, -) { +export async function getSub>( + domainId: string, docType: T, docId: DocType[T]['docId'], + key: K, subId: ObjectID, +): Promise<[DocType[T], DocType[T][K]][0]> { const doc = await coll.findOne({ domainId, docType, @@ -208,10 +213,10 @@ export async function getSub( return [doc, null]; } -export async function setSub( - domainId: string, docType: number, docId: DocID, - key: string, subId: ObjectID, args: any, -) { +export async function setSub>( + domainId: string, docType: T, docId: DocType[T]['docId'], + key: K, subId: ObjectID, args: UpdateQuery['$set'], +): Promise { const $set = {}; for (const k in args) { $set[`${key}.$.${k}`] = args[k]; @@ -229,9 +234,9 @@ export async function setSub( return res.value; } -export async function addToSet( - domainId: string, docType: number, docId: DocID, - setKey: string, content: string, +export async function addToSet>( + domainId: string, docType: T, docId: DocType[T]['docId'], + setKey: K, content: string, ) { const res = await coll.findOneAndUpdate( { domainId, docType, docId }, @@ -241,23 +246,29 @@ export async function addToSet( return res.value; } -export function getStatus(domainId: string, docType: number, docId: DocID, uid: number) { +export function getStatus( + domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, +): Promise { return collStatus.findOne({ domainId, docType, docId, uid, }); } -export function getMultiStatus(domainId: string, docType: number, args: any) { +export function getMultiStatus( + domainId: string, docType: K, args: FilterQuery, +): Cursor { return collStatus.find({ ...args, docType, domainId }); } -export function getMultiStatusWithoutDomain(docType: number, args: any) { +export function getMultiStatusWithoutDomain( + docType: K, args: FilterQuery, +): Cursor { return collStatus.find({ ...args, docType }); } -export async function setStatus( - domainId: string, docType: number, docId: DocID, uid: number, args: any, -) { +export async function setStatus( + domainId: string, docType: K, docId: DocStatusType[K]['docId'], uid: number, args: UpdateQuery['$set'], +): Promise { const res = await collStatus.findOneAndUpdate( { domainId, docType, docId, uid, @@ -268,7 +279,9 @@ export async function setStatus( return res.value; } -export function setMultiStatus(domainId: string, docType: number, query: any, args: any) { +export function setMultiStatus( + domainId: string, docType: K, query: FilterQuery, args: UpdateQuery['$set'], +) { return collStatus.updateMany( { domainId, docType, ...query }, { $set: args }, diff --git a/tsconfig.json b/tsconfig.json index 1ee9215de..2b60c7dc7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,6 @@ { "path": "packages/hydrooj" }, { "path": "packages/hydrojudge" }, { "path": "packages/geoip" }, - { "path": "packages/html2md" }, { "path": "packages/better-search" }, { "path": "packages/fps-importer" }, { "path": "packages/login-with-github" },