mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: add some null-safe check
This commit is contained in:
1 parent
619fd639e4
commit
9a4930f5cd
4 files changed
+34
-33
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "hydrooj",
|
"name": "hydrooj",
|
||||||
"version": "2.32.4",
|
"version": "2.32.5",
|
||||||
"bin": "bin/hydrooj.js",
|
"bin": "bin/hydrooj.js",
|
||||||
"main": "src/loader",
|
"main": "src/loader",
|
||||||
"module": "src/loader",
|
"module": "src/loader",
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ class HomeDomainHandler extends Handler {
|
|||||||
const canManage = {};
|
const canManage = {};
|
||||||
for (const ddoc of ddocs) {
|
for (const ddoc of ddocs) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
const udoc = await user.getById(ddoc._id, this.user._id);
|
const udoc = (await user.getById(ddoc._id, this.user._id))!;
|
||||||
canManage[ddoc._id] = udoc.hasPerm(PERM.PERM_EDIT_DOMAIN)
|
canManage[ddoc._id] = udoc.hasPerm(PERM.PERM_EDIT_DOMAIN)
|
||||||
|| udoc.hasPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
|
|| udoc.hasPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
|
||||||
}
|
}
|
||||||
@@ -415,7 +415,7 @@ class HomeMessagesConnectionHandler extends ConnectionHandler {
|
|||||||
|
|
||||||
async onMessageReceived(uid: number, mdoc: MessageDoc) {
|
async onMessageReceived(uid: number, mdoc: MessageDoc) {
|
||||||
if (uid !== this.user._id) return;
|
if (uid !== this.user._id) return;
|
||||||
const udoc = await user.getById(this.domainId, mdoc.from);
|
const udoc = (await user.getById(this.domainId, mdoc.from))!;
|
||||||
udoc.avatarUrl = avatar(udoc.avatar, 64);
|
udoc.avatarUrl = avatar(udoc.avatar, 64);
|
||||||
this.send({ udoc, mdoc });
|
this.send({ udoc, mdoc });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { FilterQuery, ObjectID } from 'mongodb';
|
import { FilterQuery, ObjectID } from 'mongodb';
|
||||||
|
import { pick } from 'lodash';
|
||||||
import { postJudge } from './judge';
|
import { postJudge } from './judge';
|
||||||
import {
|
import {
|
||||||
ContestNotFoundError, PermissionError, ProblemNotFoundError,
|
ContestNotFoundError, PermissionError, ProblemNotFoundError,
|
||||||
@@ -32,6 +33,7 @@ class RecordListHandler extends Handler {
|
|||||||
uidOrName?: string, status?: number, full = false, all = false,
|
uidOrName?: string, status?: number, full = false, all = false,
|
||||||
) {
|
) {
|
||||||
let tdoc = null;
|
let tdoc = null;
|
||||||
|
let invalid = false;
|
||||||
this.response.template = 'record_main.html';
|
this.response.template = 'record_main.html';
|
||||||
const q: FilterQuery<RecordDoc> = { 'contest.tid': tid, hidden: false };
|
const q: FilterQuery<RecordDoc> = { 'contest.tid': tid, hidden: false };
|
||||||
if (full) uidOrName = this.user._id.toString();
|
if (full) uidOrName = this.user._id.toString();
|
||||||
@@ -41,23 +43,19 @@ class RecordListHandler extends Handler {
|
|||||||
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||||
}
|
}
|
||||||
if (uidOrName) {
|
if (uidOrName) {
|
||||||
let udoc = await user.getById(domainId, +uidOrName);
|
const udoc = await user.getById(domainId, +uidOrName) ?? await user.getByUname(domainId, uidOrName);
|
||||||
if (udoc) q.uid = udoc._id;
|
if (udoc) q.uid = udoc._id;
|
||||||
else {
|
else invalid = true;
|
||||||
udoc = await user.getByUname(domainId, uidOrName);
|
|
||||||
if (udoc) q.uid = udoc._id;
|
|
||||||
else q.uid = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (pid) {
|
if (pid) {
|
||||||
const pdoc = await problem.get(domainId, pid);
|
const pdoc = await problem.get(domainId, pid);
|
||||||
if (pdoc) q.pid = pdoc.docId;
|
if (pdoc) q.pid = pdoc.docId;
|
||||||
else q.pid = null;
|
else invalid = true;
|
||||||
}
|
}
|
||||||
if (status) q.status = status;
|
if (status) q.status = status;
|
||||||
let cursor = record.getMulti(domainId, q).sort('_id', -1);
|
let cursor = record.getMulti(domainId, q).sort('_id', -1);
|
||||||
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
|
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
|
||||||
const [rdocs] = await paginate(cursor, page, full ? 10 : system.get('pagination.record'));
|
const [rdocs] = invalid ? [[]] : await paginate(cursor, page, full ? 10 : system.get('pagination.record'));
|
||||||
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
||||||
const canViewProblemHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
const canViewProblemHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
||||||
const [udict, pdict] = full ? [{}, {}]
|
const [udict, pdict] = full ? [{}, {}]
|
||||||
@@ -104,7 +102,7 @@ class RecordDetailHandler extends Handler {
|
|||||||
}
|
}
|
||||||
if (rdoc.uid !== this.user._id && !this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) {
|
if (rdoc.uid !== this.user._id && !this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) {
|
||||||
if (!this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) {
|
if (!this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) {
|
||||||
rdoc.code = null;
|
rdoc.code = '';
|
||||||
rdoc.compilerTexts = [];
|
rdoc.compilerTexts = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +116,8 @@ class RecordDetailHandler extends Handler {
|
|||||||
|| !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)
|
|| !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)
|
||||||
|| (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))
|
|| (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))
|
||||||
) {
|
) {
|
||||||
throw new PermissionError(PERM.PERM_READ_RECORD_CODE);
|
if (this.user._id !== rdoc.uid) throw new PermissionError(PERM.PERM_READ_RECORD_CODE);
|
||||||
|
else pdoc = { ...problem.default, ...pdoc ? pick(pdoc, ['domainId', 'docId', 'pid']) : {} };
|
||||||
}
|
}
|
||||||
this.response.body = { udoc, rdoc, pdoc };
|
this.response.body = { udoc, rdoc, pdoc };
|
||||||
if (rdoc.contest) {
|
if (rdoc.contest) {
|
||||||
@@ -154,14 +153,14 @@ class RecordDetailHandler extends Handler {
|
|||||||
record.update(domainId, rid, $set),
|
record.update(domainId, rid, $set),
|
||||||
TaskModel.deleteMany({ rid: rdoc._id }),
|
TaskModel.deleteMany({ rid: rdoc._id }),
|
||||||
]);
|
]);
|
||||||
await postJudge(latest);
|
if (latest) await postJudge(latest);
|
||||||
}
|
}
|
||||||
this.back();
|
this.back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RecordMainConnectionHandler extends ConnectionHandler {
|
class RecordMainConnectionHandler extends ConnectionHandler {
|
||||||
cleanup: bus.Disposable;
|
cleanup: bus.Disposable = () => { };
|
||||||
tid: string;
|
tid: string;
|
||||||
uid: number;
|
uid: number;
|
||||||
pdomain: string;
|
pdomain: string;
|
||||||
@@ -218,7 +217,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
|
|||||||
if (rdoc.domainId !== this.domainId) return;
|
if (rdoc.domainId !== this.domainId) return;
|
||||||
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
|
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
|
||||||
if (this.uid && rdoc.uid !== this.uid) return;
|
if (this.uid && rdoc.uid !== this.uid) return;
|
||||||
if (this.pid && (rdoc.pid !== this.pid || rdoc.pdomain !== this.pdomain)) return;
|
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let [udoc, pdoc] = await Promise.all([
|
let [udoc, pdoc] = await Promise.all([
|
||||||
user.getById(this.domainId, rdoc.uid),
|
user.getById(this.domainId, rdoc.uid),
|
||||||
@@ -241,12 +240,13 @@ class RecordMainConnectionHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RecordDetailConnectionHandler extends ConnectionHandler {
|
class RecordDetailConnectionHandler extends ConnectionHandler {
|
||||||
cleanup: bus.Disposable;
|
cleanup: bus.Disposable = () => { };
|
||||||
rid: string;
|
rid: string = '';
|
||||||
|
|
||||||
@param('rid', Types.ObjectID)
|
@param('rid', Types.ObjectID)
|
||||||
async prepare(domainId: string, rid: ObjectID) {
|
async prepare(domainId: string, rid: ObjectID) {
|
||||||
const rdoc = await record.get(domainId, rid);
|
const rdoc = await record.get(domainId, rid);
|
||||||
|
if (!rdoc) return;
|
||||||
if (rdoc.contest && rdoc.input === undefined) {
|
if (rdoc.contest && rdoc.input === undefined) {
|
||||||
const tdoc = await contest.get(domainId, rdoc.contest.tid, -1);
|
const tdoc = await contest.get(domainId, rdoc.contest.tid, -1);
|
||||||
let canView = this.user.own(tdoc);
|
let canView = this.user.own(tdoc);
|
||||||
@@ -256,13 +256,13 @@ class RecordDetailConnectionHandler extends ConnectionHandler {
|
|||||||
}
|
}
|
||||||
if (rdoc.uid !== this.user._id && !this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) {
|
if (rdoc.uid !== this.user._id && !this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) {
|
||||||
if (!this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) {
|
if (!this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) {
|
||||||
rdoc.code = null;
|
rdoc.code = '';
|
||||||
rdoc.compilerTexts = [];
|
rdoc.compilerTexts = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const pdoc = await problem.get(rdoc.pdomain, rdoc.pid);
|
const pdoc = await problem.get(rdoc.pdomain, rdoc.pid);
|
||||||
let canView = this.user.own(pdoc);
|
let canView = pdoc && this.user.own(pdoc);
|
||||||
canView ||= !pdoc.hidden && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
canView ||= !pdoc?.hidden && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
|
||||||
canView ||= this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
canView ||= this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
|
||||||
canView ||= !!rdoc.contest || this.user._id === rdoc.uid;
|
canView ||= !!rdoc.contest || this.user._id === rdoc.uid;
|
||||||
if (!canView) throw new PermissionError(PERM.PERM_READ_RECORD_CODE);
|
if (!canView) throw new PermissionError(PERM.PERM_READ_RECORD_CODE);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import domain from './domain';
|
|||||||
import storage from './storage';
|
import storage from './storage';
|
||||||
import { buildProjection } from '../utils';
|
import { buildProjection } from '../utils';
|
||||||
import type {
|
import type {
|
||||||
ProblemStatusDoc, ProblemDict, Document, ProblemId,
|
ProblemStatusDoc, ProblemDict, Document, ProblemId, DomainDoc,
|
||||||
} from '../interface';
|
} from '../interface';
|
||||||
import {
|
import {
|
||||||
ArrayKeys, MaybeArray, NumberKeys, Projection,
|
ArrayKeys, MaybeArray, NumberKeys, Projection,
|
||||||
@@ -77,7 +77,7 @@ export class ProblemModel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static async add(
|
static async add(
|
||||||
domainId: string, pid: string = null, title: string, content: string, owner: number,
|
domainId: string, pid: string = '', title: string, content: string, owner: number,
|
||||||
tag: string[] = [], hidden = false,
|
tag: string[] = [], hidden = false,
|
||||||
) {
|
) {
|
||||||
const [doc] = await ProblemModel.getMulti(domainId, {})
|
const [doc] = await ProblemModel.getMulti(domainId, {})
|
||||||
@@ -88,7 +88,7 @@ export class ProblemModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async addWithId(
|
static async addWithId(
|
||||||
domainId: string, docId: number, pid: string = null, title: string,
|
domainId: string, docId: number, pid: string = '', title: string,
|
||||||
content: string, owner: number, tag: string[] = [], hidden = false,
|
content: string, owner: number, tag: string[] = [], hidden = false,
|
||||||
) {
|
) {
|
||||||
const args: Partial<ProblemDoc> = {
|
const args: Partial<ProblemDoc> = {
|
||||||
@@ -107,7 +107,7 @@ export class ProblemModel {
|
|||||||
static async get(
|
static async get(
|
||||||
domainId: string, pid: string | number,
|
domainId: string, pid: string | number,
|
||||||
projection: Projection<ProblemDoc> = ProblemModel.PROJECTION_PUBLIC,
|
projection: Projection<ProblemDoc> = ProblemModel.PROJECTION_PUBLIC,
|
||||||
): Promise<ProblemDoc> {
|
): Promise<ProblemDoc | null> {
|
||||||
if (typeof pid !== 'number') {
|
if (typeof pid !== 'number') {
|
||||||
if (Number.isSafeInteger(parseInt(pid, 10))) pid = parseInt(pid, 10);
|
if (Number.isSafeInteger(parseInt(pid, 10))) pid = parseInt(pid, 10);
|
||||||
}
|
}
|
||||||
@@ -192,6 +192,7 @@ export class ProblemModel {
|
|||||||
const meta = await storage.getMeta(`problem/${domainId}/${pid}/testdata/${name}`);
|
const meta = await storage.getMeta(`problem/${domainId}/${pid}/testdata/${name}`);
|
||||||
if (!meta) throw new Error('Upload failed');
|
if (!meta) throw new Error('Upload failed');
|
||||||
const payload = { name, ...pick(meta, ['size', 'lastModified', 'etag']) };
|
const payload = { name, ...pick(meta, ['size', 'lastModified', 'etag']) };
|
||||||
|
payload.lastModified ||= new Date();
|
||||||
if (!fileinfo) await ProblemModel.push(domainId, pid, 'data', { _id: name, ...payload });
|
if (!fileinfo) await ProblemModel.push(domainId, pid, 'data', { _id: name, ...payload });
|
||||||
else await document.setSub(domainId, document.TYPE_PROBLEM, pid, 'data', name, payload);
|
else await document.setSub(domainId, document.TYPE_PROBLEM, pid, 'data', name, payload);
|
||||||
await bus.emit('problem/addTestdata', domainId, pid, name, payload);
|
await bus.emit('problem/addTestdata', domainId, pid, name, payload);
|
||||||
@@ -243,20 +244,20 @@ export class ProblemModel {
|
|||||||
})),
|
})),
|
||||||
'domainId',
|
'domainId',
|
||||||
);
|
);
|
||||||
const r = {};
|
const r: Record<ProblemId, ProblemDoc> = {};
|
||||||
const l = {};
|
const l: Record<string, ProblemDoc> = {};
|
||||||
const ddocs = await Promise.all(Object.keys(parsed).map((i) => domain.get(i)));
|
const ddocs = await Promise.all(Object.keys(parsed).map((i) => domain.get(i)));
|
||||||
const f = ddocs.filter((i) => !(
|
const f = ddocs.filter((i) => !(
|
||||||
i?._id === domainId
|
!i || i._id === domainId
|
||||||
|| i?.share === '*'
|
|| i.share === '*'
|
||||||
|| (`,${(i?.share || '').replace(/,/g, ',').split(',').map((q) => q.trim()).join(',')},`).includes(`,${domainId},`)
|
|| (`,${(i.share || '').replace(/,/g, ',').split(',').map((q) => q.trim()).join(',')},`).includes(`,${domainId},`)
|
||||||
));
|
)) as DomainDoc[];
|
||||||
if (f.length) {
|
if (f.length) {
|
||||||
if (doThrow) throw new ProblemNotFoundError(f[0]._id, parsed[f[0]._id][0].pid);
|
if (doThrow) throw new ProblemNotFoundError(f[0]._id, parsed[f[0]._id][0].pid);
|
||||||
else {
|
else {
|
||||||
for (const sf of f) {
|
for (const sf of f) {
|
||||||
for (const pinfo of parsed[sf._id]) {
|
for (const pinfo of parsed[sf._id]) {
|
||||||
r[pinfo.pid] = { ...ProblemModel.default, domainId: sf._id, pid: pinfo.pid };
|
r[pinfo.pid] = { ...ProblemModel.default, domainId: sf._id, pid: pinfo.pid.toString() };
|
||||||
}
|
}
|
||||||
delete parsed[sf._id];
|
delete parsed[sf._id];
|
||||||
}
|
}
|
||||||
@@ -290,7 +291,7 @@ export class ProblemModel {
|
|||||||
for (const pid of pids) {
|
for (const pid of pids) {
|
||||||
if (!(r[pid] || l[pid])) {
|
if (!(r[pid] || l[pid])) {
|
||||||
if (doThrow) throw new ProblemNotFoundError(domainId, pid);
|
if (doThrow) throw new ProblemNotFoundError(domainId, pid);
|
||||||
else r[pid] = { ...ProblemModel.default, domainId, pid };
|
else r[pid] = { ...ProblemModel.default, domainId, pid: pid.toString() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user