From 643416373fde8e39b65ba8ee76e130d78b7a255c Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 7 Nov 2025 21:01:30 +0800 Subject: [PATCH] core: fix #1078 --- framework/utils/lib/utils.ts | 36 ------------------------- packages/hydrooj/src/handler/problem.ts | 2 ++ packages/hydrooj/src/model/setting.ts | 4 +-- 3 files changed, 4 insertions(+), 38 deletions(-) diff --git a/framework/utils/lib/utils.ts b/framework/utils/lib/utils.ts index e8f338fce..5505855cd 100644 --- a/framework/utils/lib/utils.ts +++ b/framework/utils/lib/utils.ts @@ -253,42 +253,6 @@ export function findFileSync(pathname: string, doThrow: boolean | Error = true) return null; } -export async function retry(func: (...args: Arg) => Ret, ...args: Arg): Promise; -export async function retry(times: number, func: (...args: Arg) => Ret, ...args: Arg): Promise; -// eslint-disable-next-line consistent-return -export async function retry(arg0: number | ((...args: any[]) => any), func: any, ...args: any[]): Promise { - let res; - if (typeof arg0 !== 'number') { - args = [func, ...args]; - func = arg0; - arg0 = 3; - } - for (let i = 1; i <= arg0; i++) { - try { - // eslint-disable-next-line no-await-in-loop - res = await func(...args); - } catch (e) { - if (i === arg0) throw e; - continue; - } - return res; - } -} - -export function CallableInstance(property = '__call__') { - let func; - if (typeof property === 'function') func = property; - else func = this.constructor.prototype[property]; - const apply = function __call__(...args) { return func.apply(apply, ...args); }; - Object.setPrototypeOf(apply, this.constructor.prototype); - for (const p of Object.getOwnPropertyNames(func)) { - Object.defineProperty(apply, p, Object.getOwnPropertyDescriptor(func, p)); - } - return apply; -} - -CallableInstance.prototype = Object.create(Function.prototype); - export const htmlEncode = (str: string) => str.replace(/[&<>'"]/g, (tag: string) => ({ '&': '&', diff --git a/packages/hydrooj/src/handler/problem.ts b/packages/hydrooj/src/handler/problem.ts index ffe9e273c..ac9d1271a 100644 --- a/packages/hydrooj/src/handler/problem.ts +++ b/packages/hydrooj/src/handler/problem.ts @@ -254,6 +254,7 @@ export class ProblemMainHandler extends Handler { for (const pid of pids) { // eslint-disable-next-line no-await-in-loop const pdoc = await problem.get(domainId, pid); + if (!pdoc) throw new ProblemNotFoundError(domainId, pid); if (!this.user.own(pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM); // eslint-disable-next-line no-await-in-loop await problem.edit(domainId, pid, { hidden: true }); @@ -266,6 +267,7 @@ export class ProblemMainHandler extends Handler { for (const pid of pids) { // eslint-disable-next-line no-await-in-loop const pdoc = await problem.get(domainId, pid); + if (!pdoc) throw new ProblemNotFoundError(domainId, pid); if (!this.user.own(pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM); // eslint-disable-next-line no-await-in-loop await problem.edit(domainId, pid, { hidden: false }); diff --git a/packages/hydrooj/src/model/setting.ts b/packages/hydrooj/src/model/setting.ts index 6d5a875f0..7fea88885 100644 --- a/packages/hydrooj/src/model/setting.ts +++ b/packages/hydrooj/src/model/setting.ts @@ -7,7 +7,7 @@ import { Dictionary } from 'lodash'; import moment from 'moment-timezone'; import Schema from 'schemastery'; import { LangConfig, parseLang } from '@hydrooj/common'; -import { findFileSync, randomstring, retry } from '@hydrooj/utils'; +import { findFileSync, randomstring } from '@hydrooj/utils'; import { Context } from '../context'; import { Setting as _Setting } from '../interface'; import { Logger } from '../logger'; @@ -386,7 +386,7 @@ export async function apply(ctx: Context) { if (!setting.value) continue; const current = await ctx.db.collection('system').findOne({ _id: setting.key }); if (!current || current.value == null || current.value === '') { - await retry(system.set, setting.key, setting.value); + await system.set(setting.key, setting.value); } } try {