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

+20 -4
View File
@@ -1,3 +1,4 @@
/* eslint-disable consistent-return */
/* eslint-disable no-undef */
/// <reference types="./jssh" />
@@ -93,7 +94,13 @@ const steps = [
operations: [
() => {
const res = exec1('bash -c "source /root/.nvm/nvm.sh && nvm install 14"', { NVM_NODEJS_ORG_MIRROR });
const ver = res.output.split('Now using node v')[1].split(' ')[0];
let ver;
try {
ver = res.output.split('Now using node v')[1].split(' ')[0];
} catch (e) {
log.error('Cannot parse version. Installation fail.');
return 'retry';
}
setenv('PATH', `/root/.nvm/versions/node/v${ver}/bin:${__env.PATH}`);
const rc_path = `/root/.${__env['SHELL'].endsWith('zsh') ? 'zsh' : 'bash'}rc`;
if (!fs.exist(rc_path)) fs.writefile(rc_path, source_nvm);
@@ -152,12 +159,17 @@ const steps = [
Arch ? 'pacman --needed --quiet --noconfirm -S gcc fpc' : 'apt-get install -y g++ fp-compiler >/dev/null',
],
},
{
init: '正在安装沙箱 / Installing sandbox',
operations: [
`curl -fSL ${SANDBOX_DOWNLOAD} -o /usr/bin/sandbox`,
'chmod +x /usr/bin/sandbox',
],
},
{
init: '正在安装 HydroOJ / Installing HydroOJ',
operations: [
'yarn global add hydrooj @hydrooj/ui-default @hydrooj/hydrojudge',
() => http.download(SANDBOX_DOWNLOAD, '/usr/bin/sandbox'),
'chmod +x /usr/bin/sandbox',
() => fs.writefile('/root/.hydro/addon.json', '["@hydrooj/ui-default","@hydrooj/hydrojudge"]'),
],
},
@@ -199,7 +211,11 @@ for (let i = 0; i < steps.length; i++) {
if (!(step.skip && step.skip())) {
for (const op of step.operations) {
if (typeof op === 'string') {
exec(op);
let res = exec(op);
while (res === 'retry') {
log.warn('Retry...');
res = exec(op);
}
if (__code !== 0) log.fatal('Error when running %s', op);
} else op();
}
+1 -1
View File
@@ -201,7 +201,7 @@ async function postInit() {
this.clean = [];
fs.ensureDirSync(this.tmpdir);
tmpfs.mount(this.tmpdir, getConfig('tmpfs_size'));
logger.info(`Submission: ${this.rid}`, { pid: this.pid });
logger.info(`Submission: ${this.domainId}/${this.rid}`, { pid: this.pid });
if (typeof this.input === 'string') await this.run();
else await this.submission();
} catch (e) {
+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 }) });
}
}
@@ -157,16 +157,18 @@ export default connect(mapStateToProps, mapDispatchToProps)(class ScratchpadTool
{' '}
{i18n('Pretest')}
</ToolbarButton>
<ToolbarButton
activated={this.props.recordsVisible}
onClick={() => this.props.togglePanel('records')}
data-global-hotkey="alt+r"
data-tooltip={`${i18n('Toggle Records Panel')} (Alt+R)`}
>
<Icon name="flag" />
{' '}
{i18n('Records')}
</ToolbarButton>
{UiContext.canViewRecord && (
<ToolbarButton
activated={this.props.recordsVisible}
onClick={() => this.props.togglePanel('records')}
data-global-hotkey="alt+r"
data-tooltip={`${i18n('Toggle Records Panel')} (Alt+R)`}
>
<Icon name="flag" />
{' '}
{i18n('Records')}
</ToolbarButton>
)}
</Toolbar>
);
}
@@ -14,9 +14,7 @@ function buildNestedPane([a, ...panes]) {
a,
...panes.filter((p) => p.props.visible),
];
if (elements.length === 1) {
return a;
}
if (elements.length === 1) return a;
return elements
.reduce((prev, curr) => (
<SplitPane
@@ -1,4 +1,5 @@
import Notification from 'vj/components/notification';
import i18n from 'vj/utils/i18n';
export default function reducer(state = {
main: {
@@ -9,94 +10,95 @@ export default function reducer(state = {
size: 200,
},
records: {
visible: true,
visible: UiContext.canViewRecord,
size: 200,
isLoading: false,
},
isPosting: false,
}, action) {
switch (action.type) {
case 'SCRATCHPAD_UI_CHANGE_SIZE': {
const { uiElement, size } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
size,
},
};
}
case 'SCRATCHPAD_UI_SET_VISIBILITY': {
const { uiElement, visibility } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
visible: visibility,
},
};
}
case 'SCRATCHPAD_UI_TOGGLE_VISIBILITY': {
const { uiElement } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
visible: !state[uiElement].visible,
},
};
}
case 'SCRATCHPAD_POST_PRETEST_PENDING':
case 'SCRATCHPAD_POST_SUBMIT_PENDING': {
return {
...state,
isPosting: true,
};
}
case 'SCRATCHPAD_POST_PRETEST_FULFILLED':
case 'SCRATCHPAD_POST_SUBMIT_FULFILLED': {
return {
...state,
isPosting: false,
};
}
case 'SCRATCHPAD_POST_PRETEST_REJECTED':
case 'SCRATCHPAD_POST_SUBMIT_REJECTED': {
Notification.error(action.payload.message);
return {
...state,
isPosting: false,
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_PENDING': {
return {
...state,
records: {
...state.records,
isLoading: true,
},
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_REJECTED': {
Notification.error(action.payload.message);
return {
...state,
records: {
...state.records,
isLoading: false,
},
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_FULFILLED': {
return {
...state,
records: {
...state.records,
isLoading: false,
},
};
}
default:
return state;
case 'SCRATCHPAD_UI_CHANGE_SIZE': {
const { uiElement, size } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
size,
},
};
}
case 'SCRATCHPAD_UI_SET_VISIBILITY': {
const { uiElement, visibility } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
visible: visibility,
},
};
}
case 'SCRATCHPAD_UI_TOGGLE_VISIBILITY': {
const { uiElement } = action.payload;
return {
...state,
[uiElement]: {
...state[uiElement],
visible: !state[uiElement].visible,
},
};
}
case 'SCRATCHPAD_POST_PRETEST_PENDING':
case 'SCRATCHPAD_POST_SUBMIT_PENDING': {
return {
...state,
isPosting: true,
};
}
case 'SCRATCHPAD_POST_PRETEST_FULFILLED':
case 'SCRATCHPAD_POST_SUBMIT_FULFILLED': {
Notification.success(i18n('Submitted.'));
return {
...state,
isPosting: false,
};
}
case 'SCRATCHPAD_POST_PRETEST_REJECTED':
case 'SCRATCHPAD_POST_SUBMIT_REJECTED': {
Notification.error(action.payload.message);
return {
...state,
isPosting: false,
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_PENDING': {
return {
...state,
records: {
...state.records,
isLoading: true,
},
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_REJECTED': {
Notification.error(action.payload.message);
return {
...state,
records: {
...state.records,
isLoading: false,
},
};
}
case 'SCRATCHPAD_RECORDS_LOAD_SUBMISSIONS_FULFILLED': {
return {
...state,
records: {
...state.records,
isLoading: false,
},
};
}
default:
return state;
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@hydrooj/ui-default",
"version": "4.8.39",
"version": "4.8.40",
"author": "undefined <i@undefined.moe>",
"license": "AGPL-3.0",
"main": "hydro.js",
@@ -7,19 +7,22 @@
problemNumId: pdoc.docId,
postCopyUrl: url('problem_send'),
postPretestUrl: url('problem_pretest', pid=pdoc.docId),
pretestConnUrl: "/conn/pretest?pid=" + pdoc.pid|default(pdoc.docId) + "&domainId=" + handler.domainId,
codeLang: handler.user.codeLang,
codeTemplate: handler.user.codeTemplate,
pdoc: pdoc
}) }}
{% if tdoc %}
{{ set(UiContext, 'canViewRecord', model.contest.canShowRecord.call(handler, tdoc)) }}
{{ set(UiContext, 'postSubmitUrl', url('contest_detail_problem_submit' if tdoc.docType == model.document.TYPE_CONTEST else 'homework_detail_problem_submit', pid=pdoc.docId, tid=tdoc.docId)) }}
{{ set(UiContext, 'getSubmissionsUrl', url('contest_detail_problem_submit' if tdoc.docType == model.document.TYPE_CONTEST else 'homework_detail_problem_submit', pid=pdoc.docId, tid=tdoc.docId)) }}
{{ set(UiContext, 'getRecordDetailUrl', url('record_detail', rid='{rid}', query={tid:tdoc.docId})) }}
{{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.pid|default(pdoc.docId) + "&domainId=" + handler.domainId + "&tid=" + tdoc.docId) }}
{% else %}
{{ set(UiContext, 'canViewRecord', true) }}
{{ set(UiContext, 'postSubmitUrl', url('problem_submit', pid=pdoc.docId)) }}
{{ set(UiContext, 'getSubmissionsUrl', url('problem_submit', pid=pdoc.docId)) }}
{{ set(UiContext, 'getRecordDetailUrl', url('record_detail', rid='{rid}')) }}
{{ set(UiContext, 'pretestConnUrl', "/record-conn?pretest=1&uid=" + handler.user._id + "&pid=" + pdoc.pid|default(pdoc.docId) + "&domainId=" + handler.domainId) }}
{% endif %}
</script>
<div class="row">