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/hydro/handler/record.ts

184 lines
6.3 KiB
TypeScript

import { ObjectID } from 'mongodb';
import { PermissionError } from '../error';
import { PERM } from '../model/builtin';
import * as problem from '../model/problem';
import * as record from '../model/record';
import * as contest from '../model/contest';
import * as system from '../model/system';
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';
const RecordHandler = contest.ContestHandlerMixin(Handler);
class RecordListHandler extends RecordHandler {
@param('page', Types.UnsignedInt, 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';
if (tid) tid = new ObjectID(tid);
const q: any = { tid, hidden: false };
if (uidOrName) {
4 years ago
q.$or = [
{ unameLower: uidOrName.toLowerCase() },
{ _id: parseInt(uidOrName, 10) },
4 years ago
];
}
4 years ago
if (pid) q.pid = pid;
const [rdocs] = await paginate(
record.getMulti(domainId, q).sort('_id', -1),
page,
await system.get('RECORD_PER_PAGE'),
);
const [udict, pdict] = await Promise.all([
user.getList(domainId, rdocs.map((rdoc) => rdoc.uid)),
4 years ago
problem.getList(domainId, rdocs.map((rdoc) => rdoc.pid), false, false),
]);
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,
fliterPid: pid,
fliterTid: tid,
fliterUidOrName: 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);
if (rdoc.tid) {
const tdoc = await contest.get(domainId, rdoc.tid, rdoc.ttype);
if (!this.canShowRecord(tdoc, true)) throw new PermissionError(rid);
}
if (rdoc.uid !== this.user.uid && !this.user.hasPerm(PERM.PERM_READ_RECORD_CODE)) {
rdoc.code = null;
}
4 years ago
if (rdoc.type !== 'run') rdoc.stdout = rdoc.stderr = '';
const [pdoc, udoc] = await Promise.all([
problem.get(domainId, rdoc.pid, null, false),
user.getById(domainId, rdoc.uid),
]);
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
// TODO protect
showStatus: true,
};
}
}
5 years ago
class RecordRejudgeHandler extends Handler {
@param('rid', Types.ObjectID)
async post(domainId: string, rid: ObjectID) {
this.checkPerm(PERM.PERM_REJUDGE);
const rdoc = await record.get(domainId, rid);
if (rdoc) await record.rejudge(domainId, rid);
this.back();
}
}
5 years ago
4 years ago
const RecordConnectionHandler = contest.ContestHandlerMixin(ConnectionHandler);
class RecordMainConnectionHandler extends RecordConnectionHandler {
@param('tid', Types.ObjectID)
async prepare(domainId: string, tid: ObjectID) {
this.domainId = domainId;
4 years ago
if (tid) {
const tdoc = await contest.get(domainId, new ObjectID(tid), -1);
4 years ago
if (this.canShowRecord(tdoc)) this.tid = tid;
else {
this.close();
return;
}
}
bus.subscribe(['record_change'], this, 'onRecordChange');
}
5 years ago
async message(msg) {
if (msg.rids instanceof Array) {
const rdocs = await record.getMulti(
this.domainId, { _id: { $in: msg.rids.map((id) => new ObjectID(id)) } },
).toArray();
for (const rdoc of rdocs) {
this.onRecordChange({ value: rdoc });
}
}
}
5 years ago
async cleanup() {
bus.unsubscribe(['record_change'], this, 'onRecordChange');
}
5 years ago
async onRecordChange(data) {
5 years ago
const rdoc = data.value;
if (rdoc.tid && rdoc.tid.toString() !== this.tid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
4 years ago
problem.get(this.domainId, rdoc.pid, null, false),
]);
if (pdoc && pdoc.hidden && !this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) 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) {
@param('rid', Types.ObjectID)
async prepare(domainId: string, rid: ObjectID) {
const rdoc = await record.get(domainId, rid);
5 years ago
if (rdoc.tid) {
4 years ago
const tdoc = await contest.get(domainId, rdoc.tid, -1);
5 years ago
if (!this.canShowRecord(tdoc)) {
this.close();
return;
}
5 years ago
}
4 years ago
this.rid = rid.toString();
bus.subscribe(['record_change'], this, 'onRecordChange');
this.onRecordChange({ value: rdoc });
}
5 years ago
async onRecordChange(data) {
5 years ago
const rdoc = data.value;
4 years ago
if (rdoc._id.toString() !== this.rid) return;
this.send({
status_html: await this.renderHTML('record_detail_status.html', { rdoc }),
5 years ago
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc }),
});
}
5 years ago
async cleanup() {
bus.unsubscribe(['record_change'], this, 'onRecordChange');
}
}
export async function apply() {
Route('record_main', '/record', RecordListHandler);
Route('record_detail', '/record/:rid', RecordDetailHandler);
Route('record_rejudge', '/record/:rid/rejudge', RecordRejudgeHandler);
Connection('record_conn', '/record-conn', RecordMainConnectionHandler);
Connection('record_detail_conn', '/record-detail-conn', RecordDetailConnectionHandler);
5 years ago
}
global.Hydro.handler.record = apply;