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

229 lines
8.1 KiB
TypeScript

import { FilterQuery, ObjectID } from 'mongodb';
4 years ago
import { PermissionError, RecordNotFoundError } from '../error';
import { PERM, CONSTANT, STATUS } from '../model/builtin';
import * as problem from '../model/problem';
import * as record from '../model/record';
import * as contest from '../model/contest';
import * as user from '../model/user';
import paginate from '../lib/paginate';
import * as bus from '../service/bus';
import {
Route, Handler, Connection, ConnectionHandler, Types, param,
} from '../service/server';
import { Rdoc } from '../interface';
const RecordHandler = contest.ContestHandlerMixin(Handler);
class RecordListHandler extends RecordHandler {
@param('page', Types.PositiveInt, true)
@param('pid', Types.String, true)
@param('tid', Types.ObjectID, true)
@param('uidOrName', Types.String, true)
async get(domainId: string, page = 1, pid?: string, tid?: ObjectID, uidOrName?: string) {
this.response.template = 'record_main.html';
const q: FilterQuery<Rdoc> = { 'contest.tid': tid, hidden: false };
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
}
const [rdocs] = await paginate(
record.getMulti(domainId, q).sort('_id', -1),
page,
CONSTANT.RECORD_PER_PAGE,
);
const canViewProblem = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM);
const canViewProblemHidden = this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
const [udict, pdict] = await Promise.all([
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
canViewProblem
? problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid), canViewProblemHidden, false)
: Object.fromEntries([rdocs.map((rdoc) => [rdoc.pid, problem.Pdoc.create(rdoc.pid, rdoc.pid)])]),
]);
5 years ago
const path = [
4 years ago
['Hydro', 'homepage'],
5 years ago
['record_main', null],
];
this.response.body = {
4 years ago
path,
page,
rdocs,
pdict,
udict,
filterPid: pid,
filterTid: tid,
filterUidOrName: uidOrName,
};
}
}
5 years ago
class RecordDetailHandler extends RecordHandler {
@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 (!this.canShowRecord(tdoc, true)) throw new PermissionError(rid);
}
if (rdoc.uid !== this.user._id && !this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) rdoc.code = null;
// eslint-disable-next-line prefer-const
let [pdoc, udoc] = await Promise.all([
4 years ago
problem.get(domainId, rdoc.pid),
user.getById(domainId, rdoc.uid),
]);
if (!(pdoc && this.user.hasPerm(PERM.PERM_VIEW_PROBLEM))) {
pdoc = problem.Pdoc.create(pdoc?.docId || 0, pdoc?.pid || '*');
}
if (!rdoc.contest && pdoc.hidden && pdoc.owner !== this.user._id) {
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) {
pdoc = problem.Pdoc.create(pdoc.docId, pdoc.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,
};
}
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',
}],
};
await Promise.all([
record.update(domainId, rid, $set),
bus.emit('record/change', rdoc, $set),
]);
}
this.back();
}
4 years ago
}
4 years ago
const RecordConnectionHandler = contest.ContestHandlerMixin(ConnectionHandler);
class RecordMainConnectionHandler extends RecordConnectionHandler {
dispose: bus.Disposable;
@param('tid', Types.ObjectID, true)
async prepare(domainId: string, tid?: ObjectID) {
this.domainId = domainId;
4 years ago
if (tid) {
const tdoc = await contest.get(domainId, tid, -1);
if (this.canShowRecord(tdoc)) this.tid = tid.toHexString();
4 years ago
else {
this.close();
return;
}
}
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: Rdoc) {
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
problem.get(this.domainId, rdoc.pid, null),
]);
if (pdoc) {
if (pdoc.hidden && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)) pdoc = null;
}
this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
}
}
5 years ago
5 years ago
class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(ConnectionHandler) {
dispose: bus.Disposable;
@param('rid', Types.ObjectID)
async prepare(domainId: string, rid: ObjectID) {
const rdoc = await record.get(domainId, rid);
if (rdoc.contest) {
const tdoc = await contest.get(domainId, rdoc.contest.tid, -1);
5 years ago
if (!this.canShowRecord(tdoc)) {
this.close();
return;
}
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
async onRecordChange(rdoc: Rdoc, $set?: any, $push?: any) {
4 years ago
if (rdoc._id.toString() !== this.rid) return;
if ($set) this.send({ $set, $push });
else {
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;