core: add noTemplate option to RecordConnectionHandler (#924)

This commit is contained in:
Baoshuo authored and GitHub committed 2025-01-02 21:54:21 +08:00
1 parent 1123e02f5e
commit 8eed68e78f
1 file changed
+22 -9
+22 -9
View File
@@ -257,6 +257,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
pretest = false; pretest = false;
tdoc: Tdoc; tdoc: Tdoc;
applyProjection = false; applyProjection = false;
noTemplate = false;
queue: Map<string, () => Promise<any>> = new Map(); queue: Map<string, () => Promise<any>> = new Map();
throttleQueueClear: () => void; throttleQueueClear: () => void;
@@ -267,9 +268,10 @@ class RecordMainConnectionHandler extends ConnectionHandler {
@param('pretest', Types.Boolean) @param('pretest', Types.Boolean)
@param('all', Types.Boolean) @param('all', Types.Boolean)
@param('allDomain', Types.Boolean) @param('allDomain', Types.Boolean)
@param('noTemplate', Types.Boolean, true)
async prepare( async prepare(
domainId: string, tid?: ObjectId, pid?: string | number, uidOrName?: string, domainId: string, tid?: ObjectId, pid?: string | number, uidOrName?: string,
status?: number, pretest = false, all = false, allDomain = false, status?: number, pretest = false, all = false, allDomain = false, noTemplate = false,
) { ) {
if (tid) { if (tid) {
this.tdoc = await contest.get(domainId, tid); this.tdoc = await contest.get(domainId, tid);
@@ -308,6 +310,7 @@ class RecordMainConnectionHandler extends ConnectionHandler {
this.checkPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN); this.checkPriv(PRIV.PRIV_MANAGE_ALL_DOMAIN);
this.allDomain = true; this.allDomain = true;
} }
this.noTemplate = noTemplate;
this.throttleQueueClear = throttle(this.queueClear, 100, { trailing: true }); this.throttleQueueClear = throttle(this.queueClear, 100, { trailing: true });
} }
@@ -346,8 +349,11 @@ class RecordMainConnectionHandler extends ConnectionHandler {
if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)) pdoc = null; if (!this.user.hasPerm(PERM.PERM_VIEW_PROBLEM)) pdoc = null;
} }
if (this.applyProjection && typeof rdoc.input !== 'string') rdoc = contest.applyProjection(tdoc, rdoc, this.user); if (this.applyProjection && typeof rdoc.input !== 'string') rdoc = contest.applyProjection(tdoc, rdoc, this.user);
if (this.pretest) this.queueSend(rdoc._id.toHexString(), async () => ({ rdoc: omit(rdoc, ['code', 'input']) })); if (this.pretest) {
else { this.queueSend(rdoc._id.toHexString(), async () => ({ rdoc: omit(rdoc, ['code', 'input']) }));
} else if (this.noTemplate) {
this.queueSend(rdoc._id.toHexString(), async () => ({ rdoc }));
} else {
this.queueSend(rdoc._id.toHexString(), async () => ({ this.queueSend(rdoc._id.toHexString(), async () => ({
html: await this.renderHTML('record_main_tr.html', { html: await this.renderHTML('record_main_tr.html', {
rdoc, udoc, pdoc, tdoc, allDomain: this.allDomain, rdoc, udoc, pdoc, tdoc, allDomain: this.allDomain,
@@ -374,9 +380,11 @@ class RecordDetailConnectionHandler extends ConnectionHandler {
disconnectTimeout: NodeJS.Timeout; disconnectTimeout: NodeJS.Timeout;
throttleSend: any; throttleSend: any;
applyProjection = false; applyProjection = false;
noTemplate = false;
@param('rid', Types.ObjectId) @param('rid', Types.ObjectId)
async prepare(domainId: string, rid: ObjectId) { @param('noTemplate', Types.Boolean, true)
async prepare(domainId: string, rid: ObjectId, noTemplate = false) {
const rdoc = await record.get(domainId, rid); const rdoc = await record.get(domainId, rid);
if (!rdoc) return; if (!rdoc) return;
if (rdoc.contest && ![record.RECORD_GENERATE, record.RECORD_PRETEST].some((i) => i.toHexString() === rdoc.contest.toHexString())) { if (rdoc.contest && ![record.RECORD_GENERATE, record.RECORD_PRETEST].some((i) => i.toHexString() === rdoc.contest.toHexString())) {
@@ -408,17 +416,22 @@ class RecordDetailConnectionHandler extends ConnectionHandler {
} }
this.pdoc = pdoc; this.pdoc = pdoc;
this.noTemplate = noTemplate;
this.throttleSend = throttle(this.sendUpdate, 1000, { trailing: true }); this.throttleSend = throttle(this.sendUpdate, 1000, { trailing: true });
this.rid = rid.toString(); this.rid = rid.toString();
this.onRecordChange(rdoc); this.onRecordChange(rdoc);
} }
async sendUpdate(rdoc: RecordDoc) { async sendUpdate(rdoc: RecordDoc) {
this.send({ if (this.noTemplate) {
status: rdoc.status, this.send({ rdoc });
status_html: await this.renderHTML('record_detail_status.html', { rdoc, pdoc: this.pdoc }), } else {
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc, pdoc: this.pdoc }), this.send({
}); status: rdoc.status,
status_html: await this.renderHTML('record_detail_status.html', { rdoc, pdoc: this.pdoc }),
summary_html: await this.renderHTML('record_detail_summary.html', { rdoc, pdoc: this.pdoc }),
});
}
} }
@subscribe('record/change') @subscribe('record/change')