You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/hydrooj/src/handler/record.ts

293 lines
11 KiB
TypeScript

import { FilterQuery, ObjectID } from 'mongodb';
import { postJudge } from './judge';
import {
ContestNotFoundError, PermissionError, ProblemNotFoundError,
RecordNotFoundError, UserNotFoundError,
} from '../error';
import { buildProjection } from '../utils';
import { RecordDoc } from '../interface';
import { PERM, STATUS, PRIV } from '../model/builtin';
import * as system from '../model/system';
import problem from '../model/problem';
import record from '../model/record';
import * as contest from '../model/contest';
import user from '../model/user';
import TaskModel from '../model/task';
import paginate from '../lib/paginate';
import * as bus from '../service/bus';
import {
Route, Handler, Connection, ConnectionHandler, Types, param,
} from '../service/server';
class RecordListHandler extends Handler {
@param('page', Types.PositiveInt, true)
@param('pid', Types.Name, true)
@param('tid', Types.ObjectID, true)
@param('uidOrName', Types.Name, true)
@param('status', Types.Int, true)
3 years ago
@param('fullStatus', Types.Boolean)
@param('allDomain', Types.Boolean, true)
3 years ago
async get(
domainId: string, page = 1, pid?: string, tid?: ObjectID,
uidOrName?: string, status?: number, full = false, all = false,
) {
let tdoc = null;
this.response.template = 'record_main.html';
const q: FilterQuery<RecordDoc> = { 'contest.tid': tid, hidden: false };
3 years ago
if (full) uidOrName = this.user._id.toString();
if (tid) {
tdoc = await contest.get(domainId, tid, -1);
if (!tdoc) throw new ContestNotFoundError(domainId, pid);
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
}
if (uidOrName) {
let udoc = await user.getById(domainId, +uidOrName);
if (udoc) q.uid = udoc._id;
else {
udoc = await user.getByUname(domainId, uidOrName);
if (udoc) q.uid = udoc._id;
else q.uid = null;
}
}
if (pid) {
const pdoc = await problem.get(domainId, pid);
if (pdoc) q.pid = pdoc.docId;
else q.pid = null;
4 years ago
}
if (status) q.status = status;
3 years ago
let cursor = record.getMulti(domainId, q).sort('_id', -1);
if (!full) cursor = cursor.project(buildProjection(record.PROJECTION_LIST));
const [rdocs] = await paginate(cursor, page, full ? 10 : system.get('pagination.record'));
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
const canViewProblemHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
3 years ago
const [udict, pdict] = full ? [{}, {}]
: await Promise.all([
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
canViewProblem
? problem.getList(domainId, rdocs.map(
(rdoc) => (rdoc.domainId === domainId ? rdoc.pid : `${rdoc.pdomain}:${rdoc.pid}`),
), (!!tid) || canViewProblemHidden || this.user._id, false)
3 years ago
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, { ...problem.default, pid: rdoc.pid }])]),
]);
this.response.body = {
4 years ago
page,
rdocs,
tdoc,
4 years ago
pdict,
udict,
filterPid: pid,
filterTid: tid,
filterUidOrName: uidOrName,
filterStatus: status,
};
3 years ago
if (this.user.hasPriv(PRIV.PRIV_VIEW_JUDGE_STATISTICS) && !full) {
this.response.body.statistics = {
...await record.stat(all ? undefined : domainId),
delay: await TaskModel.getDelay({ type: 'judge' }),
};
}
}
}
5 years ago
class RecordDetailHandler extends Handler {
@param('rid', Types.ObjectID)
async get(domainId: string, rid: ObjectID) {
this.response.template = 'record_detail.html';
const rdoc = await record.get(domainId, rid);
4 years ago
if (!rdoc) throw new RecordNotFoundError(rid);
if (rdoc.contest) {
const tdoc = await contest.get(domainId, rdoc.contest.tid, rdoc.contest.type);
if (
(rdoc.uid !== this.user._id && !contest.canShowRecord.call(this, tdoc, true))
|| (rdoc.uid === this.user._id && !contest.canShowSelfRecord.call(this, tdoc, true))
) {
throw new PermissionError(rid);
}
}
if (rdoc.uid !== this.user._id && !this.user.hasPriv(PRIV.PRIV_READ_RECORD_CODE)) {
if (!this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) rdoc.code = null;
}
// eslint-disable-next-line prefer-const
let [pdoc, udoc] = await Promise.all([
problem.get(rdoc.pdomain, rdoc.pid),
user.getById(domainId, rdoc.uid),
]);
if (
!pdoc
|| !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)
|| (!rdoc.contest && pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN))
) {
pdoc = { ...problem.default, docId: pdoc?.docId || 0, pid: '*' };
}
this.response.body = {
path: [
4 years ago
['Hydro', 'homepage'],
5 years ago
['record_detail', null],
],
udoc,
5 years ago
rdoc,
5 years ago
pdoc,
};
if (rdoc.contest) {
this.response.body.tdoc = await contest.get(domainId, rdoc.contest.tid, rdoc.contest.type);
}
}
4 years ago
@param('rid', Types.ObjectID)
async postRejudge(domainId: string, rid: ObjectID) {
this.checkPerm(PERM.PERM_REJUDGE);
const rdoc = await record.get(domainId, rid);
if (rdoc) {
await record.reset(domainId, rid, true);
await record.judge(domainId, rid, 0);
4 years ago
}
this.back();
}
@param('rid', Types.ObjectID)
async postCancel(domainId: string, rid: ObjectID) {
const rdoc = await record.get(domainId, rid);
if (rdoc) {
const $set = {
status: STATUS.STATUS_CANCELED,
score: 0,
time: 0,
memory: 0,
testCases: [{
status: 9, score: 0, time: 0, memory: 0, message: 'score canceled',
}],
};
const [latest] = await Promise.all([
record.update(domainId, rid, $set),
bus.emit('record/change', rdoc, $set),
]);
await postJudge(latest);
}
this.back();
}
4 years ago
}
class RecordMainConnectionHandler extends ConnectionHandler {
dispose: bus.Disposable;
tid: string;
uid: number;
pdomain: string;
pid: number;
status: number;
pretest: boolean;
@param('tid', Types.ObjectID, true)
@param('pid', Types.Name, true)
@param('uidOrName', Types.Name, true)
@param('status', Types.Int, true)
@param('pretest', Types.Boolean)
async prepare(domainId: string, tid?: ObjectID, pid?: string, uidOrName?: string, status?: number, pretest = false) {
if (tid) {
const tdoc = await contest.get(domainId, tid, -1);
if (!tdoc) throw new ContestNotFoundError(domainId, tid);
if (pretest || contest.canShowScoreboard.call(this, tdoc, true)) this.tid = tid.toHexString();
else throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
}
if (uidOrName) {
let udoc = await user.getById(domainId, +uidOrName);
if (udoc) this.uid = udoc._id;
else {
udoc = await user.getByUname(domainId, uidOrName);
if (udoc) this.uid = udoc._id;
else throw new UserNotFoundError(uidOrName);
}
}
if (pid) {
const pdomain = pid.includes(':') ? pid.split(':')[0] : domainId;
const ppid = pid.includes(':') ? +pid.split(':')[1] : pid;
const pdoc = await problem.get(pdomain, ppid);
if (pdoc) {
this.pdomain = pdoc.domainId;
this.pid = pdoc.docId;
} else throw new ProblemNotFoundError(domainId, pid);
}
if (status) this.status = status;
if (pretest) this.pretest = true;
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
}
5 years ago
async message(msg) {
if (msg.rids instanceof Array) {
const rids = msg.rids.map((id: string) => new ObjectID(id));
const rdocs = await record.getMulti(this.domainId, { _id: { $in: rids } }).toArray();
for (const rdoc of rdocs) this.onRecordChange(rdoc);
}
}
5 years ago
async cleanup() {
if (this.dispose) this.dispose();
}
5 years ago
async onRecordChange(rdoc: RecordDoc) {
if (rdoc.domainId !== this.domainId) return;
if (!this.pretest && rdoc.input) return;
if (!rdoc.input && rdoc.contest && rdoc.contest.tid.toString() !== this.tid) 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
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
3 years ago
problem.get(this.domainId, rdoc.pid),
]);
const tdoc = this.tid ? contest.get(this.domainId, new ObjectID(this.tid)) : null;
if (pdoc && !rdoc.contest) {
if (pdoc.hidden && !this.user.own(pdoc) && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)) pdoc = null;
}
if (rdoc.input) this.send({ rdoc });
else {
this.send({
html: await this.renderHTML('record_main_tr.html', {
rdoc, udoc, pdoc, tdoc,
}),
});
}
}
}
5 years ago
class RecordDetailConnectionHandler extends ConnectionHandler {
dispose: bus.Disposable;
rid: string;
@param('rid', Types.ObjectID)
async prepare(domainId: string, rid: ObjectID) {
const rdoc = await record.get(domainId, rid);
if (rdoc.contest && rdoc.input === undefined) {
const tdoc = await contest.get(domainId, rdoc.contest.tid, -1);
if (!contest.canShowRecord.call(this, tdoc)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
5 years ago
}
4 years ago
this.rid = rid.toString();
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
this.onRecordChange(rdoc);
}
5 years ago
4 years ago
// eslint-disable-next-line
async onRecordChange(rdoc: RecordDoc, $set?: any, $push?: any) {
4 years ago
if (rdoc._id.toString() !== this.rid) return;
4 years ago
// TODO: frontend doesn't support incremental update
// if ($set) this.send({ $set, $push });
this.send({
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
});
}
5 years ago
async cleanup() {
if (this.dispose) this.dispose();
}
}
export async function apply() {
Route('record_main', '/record', RecordListHandler);
Route('record_detail', '/record/:rid', RecordDetailHandler);
Connection('record_conn', '/record-conn', RecordMainConnectionHandler);
Connection('record_detail_conn', '/record-detail-conn', RecordDetailConnectionHandler);
5 years ago
}
global.Hydro.handler.record = apply;