core: use record-conn handler for pretest-conn

This commit is contained in:
undefined committed 2021-05-22 18:12:51 +08:00
1 parent 9b9243a87c
commit c6d3ce1e79
10 files changed
+166 -141

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hydrooj",
"version": "2.26.25",
"version": "2.26.26",
"bin": "bin/hydrooj.js",
"main": "dist/loader.js",
"typings": "dist/loader.d.ts",
-32
View File
@@ -308,37 +308,6 @@ export class ProblemPretestHandler extends ProblemDetailHandler {
}
}
export class ProblemPretestConnectionHandler extends ConnectionHandler {
pid: string;
domainId: string;
dispose: bus.Disposable;
@param('pid', Types.Name)
async prepare(domainId: string, pid: string) {
const pdoc = await problem.get(domainId, pid);
if (!pdoc) throw new ProblemNotFoundError(domainId, pid);
if (pdoc.hidden) this.checkPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN);
this.pid = pdoc.docId.toString();
this.domainId = domainId;
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
}
async onRecordChange(rdoc: RecordDoc) {
if (
rdoc.uid !== this.user._id
|| rdoc.pid.toString() !== this.pid
|| rdoc.domainId !== this.domainId
) return;
// TODO handle update
if (rdoc.contest) return;
this.send({ rdoc });
}
async cleanup() {
if (this.dispose) this.dispose();
}
}
export class ProblemManageHandler extends ProblemDetailHandler {
async prepare() {
if (!this.user.own(this.pdoc, PERM.PERM_EDIT_PROBLEM_SELF)) this.checkPerm(PERM.PERM_EDIT_PROBLEM);
@@ -651,7 +620,6 @@ export async function apply() {
Route('problem_solution_reply_raw', '/p/:pid/solution/:psid/:psrid/raw', ProblemSolutionRawHandler, PERM.PERM_VIEW_PROBLEM);
Route('problem_create', '/problem/create', ProblemCreateHandler, PERM.PERM_CREATE_PROBLEM);
Route('problem_prefix_list', '/problem/list', ProblemPrefixListHandler, PERM.PERM_VIEW_PROBLEM);
Connection('problem_pretest_conn', '/conn/pretest', ProblemPretestConnectionHandler, PERM.PERM_SUBMIT_PROBLEM);
}
global.Hydro.handler.problem = apply;
+41 -5
View File
@@ -1,5 +1,8 @@
import { FilterQuery, ObjectID } from 'mongodb';
import { ContestNotFoundError, PermissionError, RecordNotFoundError } from '../error';
import {
ContestNotFoundError, PermissionError, ProblemNotFoundError,
RecordNotFoundError, UserNotFoundError,
} from '../error';
import { RecordDoc } from '../interface';
import { PERM, STATUS, PRIV } from '../model/builtin';
import * as system from '../model/system';
@@ -158,14 +161,44 @@ class RecordDetailHandler extends Handler {
class RecordMainConnectionHandler extends ConnectionHandler {
dispose: bus.Disposable;
tid: string;
uid: number;
pid: number;
status: number;
pretest: boolean;
@param('tid', Types.ObjectID, true)
async prepare(domainId: string, tid?: ObjectID) {
@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 (contest.canShowRecord.call(this, tdoc)) this.tid = tid.toHexString();
else throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
}
if (tid) {
const tdoc = await contest.get(domainId, tid, -1);
if (!tdoc) throw new ContestNotFoundError(domainId, tid);
if (!contest.canShowScoreboard.call(this, tdoc, true)) throw new PermissionError(PERM.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
this.tid = tid.toHexString();
}
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 pdoc = await problem.get(domainId, pid);
if (pdoc) 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));
}
@@ -182,9 +215,11 @@ class RecordMainConnectionHandler extends ConnectionHandler {
}
async onRecordChange(rdoc: RecordDoc) {
if (rdoc.input) return;
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
if (rdoc.domainId !== this.domainId) return;
if (!this.pretest && rdoc.input) return;
if (rdoc.contest && rdoc.contest.tid.toString() !== this.tid) return;
if (this.uid && rdoc.uid !== this.uid) return;
if (this.pid && rdoc.pid !== this.pid) return;
// eslint-disable-next-line prefer-const
let [udoc, pdoc] = await Promise.all([
user.getById(this.domainId, rdoc.uid),
@@ -194,7 +229,8 @@ class RecordMainConnectionHandler extends ConnectionHandler {
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 }) });
if (this.pretest) this.send({ rdoc });
else this.send({ html: await this.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
}
}