mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
fix rejudge, add cache
This commit is contained in:
1 parent
c68953c59f
commit
4c53fc8146
7 files changed
+103
-103
No files matched your search
@@ -44,6 +44,10 @@ MIDDLEWARE(async (ctx, next) => {
|
||||
}
|
||||
}
|
||||
};
|
||||
ctx.back = () => {
|
||||
console.log(122143);
|
||||
ctx.redirect(ctx.request.headers.referer || '/');
|
||||
};
|
||||
ctx.csrf_token = await token.add(token.TYPE_CSRF_TOKEN, 600, ctx.path);
|
||||
await next();
|
||||
if (ctx.session.sid)
|
||||
@@ -58,7 +62,7 @@ MIDDLEWARE(async (ctx, next) => {
|
||||
update_ip: ctx.request.ip,
|
||||
update_ua: ctx.request.headers['user-agent'] || ''
|
||||
}, ctx.session));
|
||||
let cookie = { secure: options.session.secure, httponly: true };
|
||||
let cookie = { secure: options.session.secure };
|
||||
if (save) {
|
||||
cookie.expires = ctx.session.expireAt, cookie.maxAge = expireSeconds;
|
||||
ctx.cookies.set('save', 'true', cookie);
|
||||
|
||||
+36
-41
@@ -23,26 +23,19 @@ GET('/r', async ctx => {
|
||||
}
|
||||
ctx.body = { page, rdocs, pdict, udict };
|
||||
});
|
||||
SOCKET('/record-conn', class RecordConnHandler {
|
||||
constructor(conn) {
|
||||
this.tid = null;
|
||||
this.h = async data => {
|
||||
let rdoc = data.value;
|
||||
if (rdoc.tid && rdoc.tid != this.tid.toString()) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.get({ pid: rdoc.pid })]);
|
||||
if (pdoc.hidden && !conn.state.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
|
||||
conn.send({ html: await conn.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
|
||||
};
|
||||
}
|
||||
onOpen() {
|
||||
bus.subscribe(['record_change'], this.h);
|
||||
}
|
||||
onMessage(data) {
|
||||
if (data.tid) this.tid = data.tid;
|
||||
}
|
||||
onClose() {
|
||||
bus.unsubscribe(['record_change'], this.h);
|
||||
SOCKET('/record-conn', [], conn => {
|
||||
let tid = conn.params.tid;
|
||||
async function onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc.tid && rdoc.tid.toString() != tid) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.get({ pid: rdoc.pid })]);
|
||||
if (pdoc.hidden && !conn.state.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
|
||||
conn.send({ html: await conn.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
|
||||
}
|
||||
bus.subscribe(['record_change'], onRecordChange);
|
||||
conn.on('close', () => {
|
||||
bus.unsubscribe(['record_change'], onRecordChange);
|
||||
});
|
||||
});
|
||||
GET('/r/:rid', async ctx => {
|
||||
ctx.templateName = 'record_detail.html';
|
||||
@@ -52,35 +45,37 @@ GET('/r/:rid', async ctx => {
|
||||
if (rdoc.uid != uid && !ctx.state.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
ctx.body = { rdoc, show_status: true };
|
||||
});
|
||||
SOCKET('/record-detail-conn', class RecordDetailConnHandler {
|
||||
constructor(conn) {
|
||||
this.rid = conn.params.rid;
|
||||
this.h = async data => {
|
||||
let rdoc = data.value;
|
||||
if (rdoc.rid.toString() != this.rid) return;
|
||||
let [udoc, pdoc] = await Promise.all([user.getById(rdoc.uid), problem.get({ pid: rdoc.pid })]);
|
||||
if (pdoc.hidden && !conn.state.user.hasPerm(PERM_VIEW_PROBLEM_HIDDEN)) pdoc = null;
|
||||
conn.send({ html: await conn.renderHTML('record_main_tr.html', { rdoc, udoc, pdoc }) });
|
||||
};
|
||||
}
|
||||
async onOpen() {
|
||||
bus.subscribe(['record_change'], this.h);
|
||||
}
|
||||
onMessage(data) {
|
||||
if (data.rid) this.rid = data.rid;
|
||||
}
|
||||
onClose() {
|
||||
bus.unsubscribe(['record_change'], this.h);
|
||||
SOCKET('/record-detail-conn', [], async conn => {
|
||||
let rdoc = await record.get(conn.params.rid);
|
||||
if (rdoc.tid)
|
||||
if (!await conn.rdoc_contest_visible(rdoc)) {
|
||||
conn.close();
|
||||
return;
|
||||
}
|
||||
async function onRecordChange(data) {
|
||||
let rdoc = data.value;
|
||||
if (rdoc._id.toString() != conn.params.rid) return;
|
||||
conn.send({
|
||||
status_html: await conn.renderHTML('record_detail_status.html', { rdoc }),
|
||||
summary_html: await conn.renderHTML('record_detail_summary.html', { rdoc })
|
||||
});
|
||||
}
|
||||
bus.subscribe(['record_change'], onRecordChange);
|
||||
onRecordChange({ value: rdoc });
|
||||
conn.on('close', () => {
|
||||
bus.unsubscribe(['record_change'], onRecordChange);
|
||||
});
|
||||
});
|
||||
POST('/r/:rid/rejudge', requirePerm(PERM_REJUDGE), async ctx => {
|
||||
ctx.templateName = 'record_detail.html';
|
||||
let uid = ctx.state.user._id, rid = new bson.ObjectID(ctx.params.rid);
|
||||
let rdoc = await record.get(rid);
|
||||
if (rdoc.hidden) ctx.checkPerm(PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD);
|
||||
if (rdoc.uid != uid && !ctx.state.user.hasPerm(PERM_READ_RECORD_CODE)) rdoc.code = null;
|
||||
if (rdoc) await queue.push('judge', rid);
|
||||
ctx.body = { rdoc, show_status: true };
|
||||
if (rdoc) {
|
||||
await record.reset(rid);
|
||||
await queue.push('judge', rid);
|
||||
}
|
||||
ctx.back();
|
||||
});
|
||||
|
||||
/*
|
||||
|
||||
+4
-11
@@ -1,6 +1,6 @@
|
||||
const
|
||||
path = require('path'),
|
||||
send = require('koa-send'),
|
||||
staticCache = require('koa-static-cache'),
|
||||
nunjucks = require('nunjucks'),
|
||||
hljs = require('highlight.js'),
|
||||
MarkdownIt = require('markdown-it'),
|
||||
@@ -77,16 +77,9 @@ class Nunjucks extends nunjucks.Environment {
|
||||
}
|
||||
}
|
||||
let env = new Nunjucks();
|
||||
MIDDLEWARE(async (ctx, next) => {
|
||||
let done = false;
|
||||
if (ctx.method === 'HEAD' || ctx.method === 'GET')
|
||||
try {
|
||||
done = await send(ctx, ctx.path, { root: path.resolve(process.cwd(), '.uibuild'), index: 'index.html' });
|
||||
} catch (err) {
|
||||
if (err.status !== 404) throw err;
|
||||
}
|
||||
if (!done) await next();
|
||||
});
|
||||
MIDDLEWARE(staticCache(path.join(process.cwd(), '.uibuild'), {
|
||||
maxAge: 365 * 24 * 60 * 60
|
||||
}));
|
||||
MIDDLEWARE(async (ctx, next) => {
|
||||
ctx.render_title = str => str;
|
||||
ctx.UIContext = {
|
||||
|
||||
+15
-1
@@ -45,6 +45,19 @@ async function update(rid, $set) {
|
||||
if (!rdoc) throw new RecordNotFoundError(rid);
|
||||
return rdoc;
|
||||
}
|
||||
async function reset(rid) {
|
||||
return await update(rid, {
|
||||
score: 0,
|
||||
status: STATUS_WAITING,
|
||||
time: 0,
|
||||
memory: 0,
|
||||
cases: [],
|
||||
judgeTexts: [],
|
||||
compilerTexts: [],
|
||||
judgeAt: null,
|
||||
judger: null
|
||||
});
|
||||
}
|
||||
async function count(query) {
|
||||
return await coll.find(query).count();
|
||||
}
|
||||
@@ -54,5 +67,6 @@ module.exports = {
|
||||
get,
|
||||
getMany,
|
||||
update,
|
||||
count
|
||||
count,
|
||||
reset
|
||||
};
|
||||
+9
-12
@@ -41,19 +41,16 @@ function GET(route, ...handler) {
|
||||
function POST(route, ...handler) {
|
||||
router.post(route, ...handler);
|
||||
}
|
||||
/**
|
||||
* @callback SocketIOHandler
|
||||
* @param {import('socket.io').Socket} socket
|
||||
*/
|
||||
|
||||
/**
|
||||
* @callback SockJSHandler
|
||||
* @param {import('sockjs').Connection} conn
|
||||
*/
|
||||
/**
|
||||
* @param {string} prefix
|
||||
* @param {SocketIOHandler} handler
|
||||
* @param {SockJSHandler} handler
|
||||
*/
|
||||
function SOCKET(prefix, handler) {
|
||||
function SOCKET(prefix, middlewares, handler) {
|
||||
const sock = sockjs.createServer({ prefix });
|
||||
sock.on('connection', async conn => {
|
||||
conn.cookies = {
|
||||
@@ -80,18 +77,18 @@ function SOCKET(prefix, handler) {
|
||||
});
|
||||
setTimeout(reject, 5000);
|
||||
});
|
||||
for (let i of m) {
|
||||
for (let i of m)
|
||||
await new Promise((resolve, reject) => {
|
||||
i(conn, resolve).catch(reject);
|
||||
});
|
||||
for (let i of middlewares)
|
||||
await new Promise((resolve, reject) => {
|
||||
i(conn, resolve).catch(reject);
|
||||
});
|
||||
}
|
||||
conn.send = data => {
|
||||
conn.write(JSON.stringify(data));
|
||||
};
|
||||
let h = new handler(conn);
|
||||
if (h.onOpen) conn.on('open', h.onOpen);
|
||||
if (h.onClose) conn.on('close', h.onClose);
|
||||
if (h.onMessage) conn.on('data', h.onMessage);
|
||||
handler(conn);
|
||||
});
|
||||
sock.installHandlers(server);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user