core: 优化钩子逻辑

This commit is contained in:
undefined committed 2020-09-06 11:31:49 +08:00
1 parent 03dc8903e2
commit f2f0212947
27 files changed
+205 -124

No files matched your search

+1 -1
View File
@@ -310,7 +310,7 @@ class ContestProblemSubmitHandler extends ContestProblemHandler {
record.get(domainId, rid),
contest.updateStatus(domainId, this.tdoc.docId, this.user._id, rid, this.pdoc.docId),
]);
bus.publish('record_change', rdoc);
bus.boardcast('record/change', rdoc);
if (!this.canShowRecord(this.tdoc)) {
this.response.body = { tid };
this.response.redirect = this.url('contest_detail', { tid });
+7 -5
View File
@@ -25,6 +25,7 @@ import { PERM, PRIV, CONSTANT } from '../model/builtin';
import {
isContent, isPassword, isEmail, isTitle,
} from '../lib/validator';
import { Mdoc } from '../interface';
const { geoip, useragent } = global.Hydro.lib;
@@ -358,18 +359,19 @@ class HomeMessagesHandler extends Handler {
}
class HomeMessagesConnectionHandler extends ConnectionHandler {
id: string;
dispose: bus.Disposable;
async prepare() {
bus.subscribe([`user_message-${this.user._id}`], this.onMessageReceived.bind(this));
this.dispose = bus.on('user/message', this.onMessageReceived.bind(this));
}
async onMessageReceived(e: any) {
this.send(e.value);
async onMessageReceived(uid: number, e: any) {
if (uid !== this.user._id) return;
this.send(e);
}
async cleanup() {
bus.unsubscribe([`user_message-${this.user._id}`], this.id);
if (this.dispose) this.dispose();
}
}
+1 -1
View File
@@ -189,7 +189,7 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
contest.updateStatus(domainId, tid, this.user._id,
rid, pid, false, 0, document.TYPE_HOMEWORK),
]);
bus.publish('record_change', rdoc);
bus.boardcast('record/change', rdoc);
this.response.body = { tid, rid };
if (this.canShowRecord(this.tdoc)) this.response.redirect = this.url('record_detail', { rid });
else this.response.redirect = this.url('homework_detail', { tid });
+2 -2
View File
@@ -62,7 +62,7 @@ export async function next(body: JudgeResultBody) {
if (body.memory) $set.memory = body.memory;
if (body.progress) $set.progress = body.progress;
rdoc = await record.update(body.domainId, body.rid, $set, $push);
bus.publish('record_change', { rdoc, $set, $push });
bus.boardcast('record/change', rdoc, $set, $push);
}
export async function end(body: JudgeResultBody) {
@@ -87,7 +87,7 @@ export async function end(body: JudgeResultBody) {
$set.judger = body.judger;
rdoc = await record.update(body.domainId, body.rid, $set, $push, $unset);
await _postJudge(rdoc);
bus.publish('record_change', { rdoc }); // trigger a full update
bus.boardcast('record/change', rdoc); // trigger a full update
}
class JudgeHandler extends Handler {
+9 -12
View File
@@ -1,6 +1,6 @@
import { isSafeInteger, flatten } from 'lodash';
import yaml from 'js-yaml';
import { ObjectID } from 'mongodb';
import { FilterQuery, ObjectID } from 'mongodb';
import {
NoProblemError, ProblemDataNotFoundError, BadRequestError,
SolutionNotFoundError, ProblemNotFoundError, ValidationError,
@@ -61,16 +61,14 @@ class ProblemMainHandler extends ProblemHandler {
@param('q', Types.String, true)
async get(domainId: string, page = 1, q = '') {
this.response.template = 'problem_main.html';
const query: any = {};
const query: FilterQuery<Pdoc> = {};
let psdict = {};
const path: PathComponent[] = [
['Hydro', 'homepage'],
['problem_main', null],
];
if (q) {
q = q.toLowerCase();
const $regex = new RegExp(`\\A\\Q${q}\\E`, 'gmi');
query.title = { $regex };
query.$text = { $search: q };
path.push([q, null, null, true]);
}
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM_HIDDEN)) query.hidden = false;
@@ -252,7 +250,7 @@ class ProblemSubmitHandler extends ProblemDetailHandler {
record.get(domainId, rid),
domain.incUserInDomain(domainId, this.user._id, 'nSubmit'),
]);
bus.publish('record_change', { rdoc });
bus.boardcast('record/change', rdoc);
this.response.body = { rid };
this.response.redirect = this.url('record_detail', { rid });
}
@@ -275,7 +273,7 @@ class ProblemPretestHandler extends ProblemDetailHandler {
},
);
const rdoc = await record.get(domainId, rid);
bus.publish('record_change', { rdoc });
bus.boardcast('record/change', rdoc);
this.response.body = { rid };
}
}
@@ -285,7 +283,7 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
domainId: string;
id: string;
dispose: bus.Disposable;
@param('pid', Types.String)
async prepare(domainId: string, pid: string) {
@@ -294,11 +292,10 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
if (!pdoc) throw new ProblemNotFoundError(domainId, pid);
this.pid = pdoc.docId.toString();
this.domainId = domainId;
this.id = bus.subscribe(['record_change'], this.onRecordChange.bind(this));
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
}
async onRecordChange(data) {
const rdoc: Rdoc = data.value.rdoc;
async onRecordChange(rdoc: Rdoc) {
if (
rdoc.uid !== this.user._id
|| rdoc.pid.toString() !== this.pid
@@ -316,7 +313,7 @@ class ProblemPretestConnectionHandler extends ConnectionHandler {
}
async cleanup() {
bus.unsubscribe(['record_change'], this.id);
if (this.dispose) this.dispose();
}
}
+12 -11
View File
@@ -102,6 +102,8 @@ class RecordRejudgeHandler extends Handler {
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;
@@ -113,7 +115,7 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
return;
}
}
this.id = bus.subscribe(['record_change'], this.onRecordChange.bind(this));
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
}
async message(msg) {
@@ -123,17 +125,16 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
this.domainId, { _id: { $in: rids } },
).toArray();
for (const rdoc of rdocs) {
this.onRecordChange({ value: { rdoc } });
this.onRecordChange(rdoc);
}
}
}
async cleanup() {
bus.unsubscribe(['record_change'], this.id);
if (this.dispose) this.dispose();
}
async onRecordChange(data) {
const rdoc: Rdoc = data.value.rdoc;
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([
@@ -146,6 +147,8 @@ class RecordMainConnectionHandler extends RecordConnectionHandler {
}
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);
@@ -157,13 +160,11 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
}
}
this.rid = rid.toString();
this.id = bus.subscribe(['record_change'], this.onRecordChange.bind(this));
this.onRecordChange({ value: { rdoc } });
this.dispose = bus.on('record/change', this.onRecordChange.bind(this));
this.onRecordChange(rdoc);
}
async onRecordChange(data) {
const rdoc: Rdoc = data.value.rdoc;
const { $set, $push } = data.value;
async onRecordChange(rdoc: Rdoc, $set?: any, $push?: any) {
if (rdoc._id.toString() !== this.rid) return;
if ($set) this.send({ $set, $push });
else {
@@ -175,7 +176,7 @@ class RecordDetailConnectionHandler extends contest.ContestHandlerMixin(Connecti
}
async cleanup() {
bus.unsubscribe(['record_change'], this.id);
if (this.dispose) this.dispose();
}
}