系统设置中自定义UI的支持

This commit is contained in:
undefined committed 2020-07-12 13:36:12 +08:00
1 parent 11368bc831
commit 1722fbfaca
21 files changed
+284 -140

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/.build/app.js",
"preLaunchTask": "npm: build",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}
+13
View File
@@ -0,0 +1,13 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"group": "build",
"problemMatcher": [],
"label": "npm: build",
"detail": "node build --development"
}
]
}
+1 -1
View File
@@ -200,7 +200,7 @@ class ContestEditHandler extends ContestHandler {
class ContestProblemHandler extends ContestHandler {
@param('tid', Types.ObjectID)
@param('pid', Types.UnsignedInt)
async prepare(domainId: string, tid: ObjectID, pid: number) {
async _prepare(domainId: string, tid: ObjectID, pid: number) {
[this.tdoc, this.pdoc] = await Promise.all([
contest.get(domainId, tid),
problem.get(domainId, pid, this.user._id),
+5 -2
View File
@@ -1,6 +1,7 @@
import {
VerifyPasswordError, UserAlreadyExistError, InvalidTokenError,
NotFoundError,
UserNotFoundError,
} from '../error';
import * as bus from '../service/bus';
import {
@@ -141,7 +142,7 @@ class HomeSecurityHandler extends Handler {
async postChangeMail({ domainId, currentPassword, mail }) {
this.limitRate('send_mail', 3600, 30);
this.user.checkPassword(currentPassword);
const udoc = await user.getByEmail(domainId, mail, true);
const udoc = await user.getByEmail(domainId, mail);
if (udoc) throw new UserAlreadyExistError(mail);
const [code] = await token.add(
token.TYPE_CHANGEMAIL,
@@ -212,7 +213,7 @@ class UserChangemailWithCodeHandler extends Handler {
if (!tdoc || tdoc.uid !== this.user._id) {
throw new InvalidTokenError(code);
}
const udoc = await user.getByEmail(domainId, tdoc.mail, true);
const udoc = await user.getByEmail(domainId, tdoc.mail);
if (udoc) throw new UserAlreadyExistError(tdoc.mail);
await Promise.all([
user.setEmail(this.user._id, tdoc.mail),
@@ -253,6 +254,7 @@ class HomeMessagesHandler extends Handler {
async postSend({ uid, content }) {
const udoc = await user.getById('system', uid);
if (!udoc) throw new UserNotFoundError(uid);
const mdoc = await message.send(this.user._id, uid, content);
// TODO(twd2): improve here: projection
if (this.user._id !== uid) {
@@ -262,6 +264,7 @@ class HomeMessagesHandler extends Handler {
}
async postDeleteMessage({ messageId }) {
// TODO limitations
await message.del(messageId);
this.back();
}
+12 -4
View File
@@ -7,6 +7,7 @@ import {
ValidationError, HomeworkNotLiveError, ProblemNotFoundError,
HomeworkNotAttendedError,
} from '../error';
import { PenaltyRules } from '../interface';
import {
Route, Handler, Types, param,
} from '../service/server';
@@ -21,6 +22,12 @@ import * as document from '../model/document';
import paginate from '../lib/paginate';
import { isTitle, isContent } from '../lib/validator';
const validatePenaltyRules = (input: string) => {
const s = yaml.safeLoad(input);
return s;
};
const convertPenaltyRules = validatePenaltyRules;
const HomeworkHandler = contest.ContestHandlerMixin(Handler);
class HomeworkMainHandler extends HomeworkHandler {
@@ -28,7 +35,7 @@ class HomeworkMainHandler extends HomeworkHandler {
const tdocs = await contest.getMulti(domainId, {}, document.TYPE_HOMEWORK).toArray();
const calendar = [];
for (const tdoc of tdocs) {
const cal = { ...tdoc, url: `/homework/${tdoc.docId}` };
const cal = { ...tdoc, url: this.url('homework_detail', { tid: tdoc.docId }) };
if (contest.isExtended(tdoc) || contest.isDone(tdoc)) {
cal.endAt = tdoc.endAt;
cal.penaltySince = tdoc.penaltySince;
@@ -211,7 +218,7 @@ class HomeworkCreateHandler extends HomeworkHandler {
@param('penaltySinceDate', Types.Date)
@param('penaltySinceTime', Types.Time)
@param('extensionDays', Types.Float)
@param('penaltyRules', Types.String)
@param('penaltyRules', Types.String, validatePenaltyRules, convertPenaltyRules)
@param('title', Types.String, isTitle)
@param('content', Types.String, isContent)
@param('pids', Types.String)
@@ -219,7 +226,7 @@ class HomeworkCreateHandler extends HomeworkHandler {
async post(
domainId: string, beginAtDate: string, beginAtTime: string,
penaltySinceDate: string, penaltySinceTime: string, extensionDays: number,
penaltyRules: string, title: string, content: string, _pids: string, rated = false,
penaltyRules: PenaltyRules, title: string, content: string, _pids: string, rated = false,
) {
const pids = _pids.split(',').map((i) => {
if (isSafeInteger(parseInt(i, 10))) return parseInt(i, 10);
@@ -278,7 +285,7 @@ class HomeworkEditHandler extends HomeworkHandler {
@param('penaltySinceDate', Types.Date)
@param('penaltySinceTime', Types.Time)
@param('extensionDays', Types.Float)
@param('penaltyRules', Types.String)
@param('penaltyRules', Types.String, validatePenaltyRules, convertPenaltyRules)
@param('title', Types.String, isTitle)
@param('content', Types.String, isContent)
@param('pids', Types.String)
@@ -330,6 +337,7 @@ class HomeworkEditHandler extends HomeworkHandler {
class HomeworkScoreboardHandler extends HomeworkHandler {
@param('tid', Types.ObjectID)
async get(domainId: string, tid: ObjectID) {
console.log(123);
const [tdoc, rows, udict] = await this.getScoreboard(
domainId, tid, false, document.TYPE_HOMEWORK,
);
+1 -1
View File
@@ -168,7 +168,7 @@ class JudgeConnectionHandler extends ConnectionHandler {
async cleanup() {
if (this.processing) {
await record.reset(this.processing.domainId, this.processing.rid);
await record.reset(this.processing.domainId, this.processing.rid, false);
task.add(this.processing);
}
}
-1
View File
@@ -169,7 +169,6 @@ class SystemSettingHandler extends SystemHandler {
for (const key in args) {
if (typeof args[key] === 'object') {
for (const subkey in args[key]) {
console.log(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey]));
if (typeof set(`${key}.${subkey}`, args[key][subkey]) !== 'undefined') {
tasks.push(system.set(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey])));
}
+14
View File
@@ -1,5 +1,6 @@
import { ObjectID } from 'mongodb';
import { PRIV } from '../model/builtin';
import * as system from '../model/system';
import * as file from '../model/file';
import * as user from '../model/user';
import * as markdown from '../lib/markdown';
@@ -77,6 +78,18 @@ class SockToken extends Handler {
}
}
class UiSettingsHandler extends Handler {
async get() {
const [
header, nav, headerBackground,
] = await system.getMany([
'ui.header', 'ui.nav', 'ui.headerBackground',
]);
this.response.body = await this.renderHTML('extra.css', { header, nav, headerBackground });
this.response.type = 'text/css';
}
}
export async function apply() {
Route('file_download', '/fs/:fileId/:secret', FileDownloadHandler);
Route('file_download_with_name', '/fs/:fileId/:name/:secret', FileDownloadHandler);
@@ -85,6 +98,7 @@ export async function apply() {
Route('switch_language', '/language/:lang', SwitchLanguageHandler);
Route('markdown', '/markdown', MarkdownHandler);
Route('token', '/token', SockToken);
Route('ui', '/extra.css', UiSettingsHandler);
}
apply.updateStatus = function updateStatus(args) {
+4 -3
View File
@@ -62,7 +62,7 @@ class UserRegisterHandler extends Handler {
@param('mail', Types.String, isEmail)
async post(domainId: string, mail: string) {
if (await user.getByEmail('system', mail, true)) throw new UserAlreadyExistError(mail);
if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail);
this.limitRate('send_mail', 3600, 30);
const t = await token.add(
token.TYPE_REGISTRATION,
@@ -158,7 +158,8 @@ class UserDetailHandler extends Handler {
@param('uid', Types.Int)
async get(domainId: string, uid: number) {
const isSelfProfile = this.user._id === uid;
const udoc = await user.getById(domainId, uid, true);
const udoc = await user.getById(domainId, uid);
if (!udoc) throw new UserNotFoundError(uid);
const sdoc = await token.getMostRecentSessionByUid(uid);
const rdocs = await record.getByUid(domainId, uid);
const pdict = await problem.getList(
@@ -310,7 +311,7 @@ class OauthCallbackHandler extends Handler {
if (args.type === 'github') r = await this.github(args);
else if (args.type === 'google') r = await this.google(args);
else throw new UserFacingError('Oauth type');
const udoc = await user.getByEmail('system', r.email, true);
const udoc = await user.getByEmail('system', r.email);
if (udoc) {
this.session.uid = udoc._id;
} else {
+92 -13
View File
@@ -13,21 +13,51 @@ export interface Setting {
flag: number,
}
export interface Udoc {
udoc: () => any,
dudoc: () => any,
_id: number,
mail: string,
uname: string,
salt: () => string,
hash: () => string,
hashType: string,
priv: number,
regat: Date,
loginat: Date,
perm: string,
role: string,
regip: () => string,
loginip: () => string,
hasPerm: (perm: string) => boolean,
hasPriv: (priv: number) => boolean,
checkPassword: (password: string) => void,
[key: string]: any,
}
export interface Udict {
[key: number]: Udoc,
}
export interface Pdoc {
_id: ObjectID
domainId: string
docId: number
pid: string
owner: number
title: string
content: string
nSubmit: number
nAccept: number
tag: string[]
_id: ObjectID,
domainId: string,
docId: number,
pid: string,
owner: number,
title: string,
content: string,
nSubmit: number,
nAccept: number,
tag: string[],
category: string[],
data: ObjectID | null
hidden: boolean
config: string
data?: ObjectID,
hidden: boolean,
config: string,
}
export interface Pdict {
[key: string]: Pdoc,
}
export interface TestCase {
@@ -63,6 +93,55 @@ export interface Rdoc {
ttype?: number,
}
export interface ScoreboardNode {
type: string,
value: string,
raw?: any,
}
export interface PenaltyRules {
[key: string]: number,
}
export interface Tdoc {
_id: ObjectID,
domainId: string,
docId: ObjectID,
docType: number,
beginAt: Date,
endAt: Date,
penaltySince?: Date,
penaltyRules?: PenaltyRules,
attend: number,
title: string,
content: string,
rule: string,
pids: number[],
}
interface ContestStat {
detail: any,
[key: string]: any,
}
export interface ContestRule {
TEXT: string,
check: (args: any) => any,
statusSort: any,
showScoreboard: (tdoc: Tdoc, now: Date) => boolean,
showRecord: (tdoc: Tdoc, now: Date) => boolean,
stat: (tdoc: Tdoc, journal: any[]) => ContestStat,
scoreboard: (
isExport: boolean, _: (s: string) => string,
tdoc: Tdoc, rankedTsdocs: any[] | Generator<any>, udict: Udict, pdict: Pdict
) => ScoreboardNode[][],
rank: (tsdocs: any[]) => any[] | Generator<any>,
}
export interface ContestRules {
[key: string]: ContestRule,
}
export type ProblemImporter = (url: string, handler: any) =>
Promise<[Pdoc, fs.ReadStream?]> | [Pdoc, fs.ReadStream?];
+79 -85
View File
@@ -7,34 +7,18 @@ import {
} from '../error';
import * as document from './document';
import { PERM } from './builtin';
import {
Tdoc, ContestRule, ContestRules,
Udict, ScoreboardNode,
} from '../interface';
import * as validator from '../lib/validator';
import * as misc from '../lib/misc';
import ranked from '../lib/rank';
interface Column {
type: string,
value: string,
raw?: any
}
export interface Tdoc {
_id: ObjectID,
domainId: string,
docId: ObjectID,
docType: number,
beginAt: Date,
endAt: Date,
penaltySince?: Date,
attend: number,
title: string,
content: string,
rule: string,
pids: number[]
}
const acm = {
const acm: ContestRule = {
TEXT: 'ACM/ICPC',
check: () => { },
statusSort: { accept: -1, time: 1 },
showScoreboard: () => true,
showRecord: (tdoc, now) => now > tdoc.endAt,
stat: (tdoc, journal: any[]) => {
@@ -47,17 +31,18 @@ const acm = {
if (tdoc.pids.includes(j.pid)
&& !(effective[j.pid] && effective[j.pid].accept)) {
effective[j.pid] = j;
if (!j.accept) naccept[j.pid] = (naccept[j.pid] || 0) + 1;
}
if (!j.accept) naccept[j.pid]++;
}
function _time(jdoc) {
const real = jdoc.rid.generationTime - Math.floor(tdoc.beginAt / 1000);
const real = jdoc.rid.generationTime - Math.floor(tdoc.beginAt.getTime() / 1000);
const penalty = 20 * 60 * naccept[jdoc.pid];
return real + penalty;
}
for (const j in effective) {
for (const key in effective) {
const j = effective[key];
detail.push({
...effective[j], naccept: naccept[effective[j].pid], time: _time(effective[j]),
...j, naccept: naccept[j.pid], time: _time(j),
});
}
for (const d of detail) {
@@ -66,8 +51,8 @@ const acm = {
}
return { accept, time, detail };
},
scoreboard(isExport: boolean, _: (s: string) => string, tdoc, rankedTsdocs, udict, pdict) {
const columns: Column[] = [
scoreboard(isExport, _, tdoc, rankedTsdocs, udict, pdict) {
const columns: ScoreboardNode[] = [
{ type: 'rank', value: _('Rank') },
{ type: 'user', value: _('User') },
{ type: 'solved_problems', value: _('Solved Problems') },
@@ -78,40 +63,41 @@ const acm = {
{ type: 'total_time_str', value: _('Total Time') },
);
}
for (const i in tdoc.pids) {
for (let i = 1; i <= tdoc.pids.length; i++) {
const pid = tdoc.pids[i - 1];
if (isExport) {
columns.push(
{
type: 'problem_flag',
value: '#{0} {1}'.format(i + 1, pdict[tdoc.pids[i]].title),
value: '#{0} {1}'.format(i, pdict[pid].title),
},
{
type: 'problem_time',
value: '#{0} {1}'.format(i + 1, _('Time (Seconds)')),
value: '#{0} {1}'.format(i, _('Time (Seconds)')),
},
{
type: 'problem_time_str',
value: '#{0} {1}'.format(i + 1, _('Time')),
value: '#{0} {1}'.format(i, _('Time')),
},
);
} else {
columns.push({
type: 'problem_detail',
value: '#{0}'.format(i + 1),
raw: pdict[tdoc.pids[i]],
value: '#{0}'.format(i),
raw: pid,
});
}
}
const rows = [columns];
for (const [rank, tsdoc] of rankedTsdocs) {
const tsddict = {};
if (tdoc.detail) {
for (const item of tsdoc.journal) tsddict[item.pid] = item;
if (tsdoc.detail) {
for (const item of tsdoc.detail) tsddict[item.pid] = item;
}
const row = [];
row.push(
{ type: 'string', value: rank },
{ type: 'user', value: udict[tsdoc.uid].uname, raw: udict[tsdoc.uid] },
{ type: 'user', value: udict[tsdoc.uid].uname, raw: tsdoc.uid },
{ type: 'string', value: tsdoc.accept || 0 },
);
if (isExport) {
@@ -121,17 +107,17 @@ const acm = {
);
}
for (const pid of tdoc.pids) {
let rdoc;
let rid;
let colAccepted;
let colTime;
let colTimeStr;
if ((tsddict[pid] || {}).accept) {
rdoc = tsddict[pid].rid;
if (tsddict[pid]?.accept) {
rid = tsddict[pid].rid;
colAccepted = _('Accepted');
colTime = tsddict[pid].time;
colTimeStr = colTime;
colTimeStr = misc.formatSeconds(colTime);
} else {
rdoc = null;
rid = null;
colAccepted = '-';
colTime = '-';
colTimeStr = '-';
@@ -144,20 +130,21 @@ const acm = {
row.push({
type: 'record',
value: '{0}\n{1}'.format(colAccepted, colTimeStr),
raw: rdoc,
raw: rid,
});
}
}
rows.push(row);
}
}
return rows;
},
rank: (tdocs) => ranked(tdocs, (a, b) => a.score === b.score),
rank: (tsdocs) => ranked(tsdocs, (a, b) => a.score === b.score),
};
const oi = {
const oi: ContestRule = {
TEXT: 'OI',
check: () => { },
statusSort: { score: -1 },
stat: (tdoc, journal) => {
const detail = {};
let score = 0;
@@ -176,50 +163,55 @@ const oi = {
return now > tdoc.endAt;
},
scoreboard(isExport, _, tdoc, rankedTsdocs, udict, pdict) {
const columns: Column[] = [
const columns: ScoreboardNode[] = [
{ type: 'rank', value: _('Rank') },
{ type: 'user', value: _('User') },
{ type: 'total_score', value: _('Total Score') },
];
for (const i in tdoc.pids) {
for (let i = 1; i <= tdoc.pids.length; i++) {
if (isExport) {
columns.push({
type: 'problem_score',
value: '#{0} {1}'.format(i + 1, pdict[tdoc.pids[i]].title),
value: '#{0} {1}'.format(i, pdict[tdoc.pids[i - 1]].title),
});
} else {
columns.push({
type: 'problem_detail',
value: '#{0}'.format(i + 1),
raw: pdict[tdoc.pids[i]],
value: '#{0}'.format(i),
raw: tdoc.pids[i - 1],
});
}
}
const rows = [columns];
for (const [rank, tsdoc] of rankedTsdocs) {
const tsddict = {};
if (tsdoc.journal) { for (const item of tsdoc.journal) tsddict[item.pid] = item; }
if (tsdoc.journal) {
for (const item of tsdoc.journal) tsddict[item.pid] = item;
}
const row = [];
row.push({ type: 'string', value: rank });
row.push({ type: 'user', value: udict[tsdoc.uid].uname, raw: udict[tsdoc.uid] });
row.push({ type: 'string', value: tsdoc.score || 0 });
row.push(
{ type: 'string', value: rank },
{ type: 'user', value: udict[tsdoc.uid].uname, raw: tsdoc.uid },
{ type: 'string', value: tsdoc.score || 0 },
);
for (const pid of tdoc.pids) {
row.push({
type: 'record',
value: (tsddict[pid] || {}).score || '-',
raw: (tsddict[pid] || {}).rid || null,
value: tsddict[pid]?.score || '-',
raw: tsddict[pid]?.rid || null,
});
}
rows.push(row);
}
return rows;
},
rank: (tdocs) => ranked(tdocs, (a, b) => a.score === b.score),
rank: (tsdocs) => ranked(tsdocs, (a, b) => a.score === b.score),
};
const homework = {
const homework: ContestRule = {
TEXT: 'Assignment',
check: () => { },
statusSort: { penaltyScore: -1, time: 1 },
stat: (tdoc, journal) => {
const effective = {};
for (const j of journal) {
@@ -233,6 +225,7 @@ const homework = {
const real = jdoc.rid.generationTime - tdoc.beginAt.getTime() / 1000;
return Math.floor(real);
}
function penaltyScore(jdoc) {
const { score } = jdoc;
const exceedSeconds = Math.floor(
@@ -266,7 +259,7 @@ const homework = {
showScoreboard: () => true,
showRecord: () => true,
scoreboard(isExport, _, tdoc, rankedTsdocs, udict, pdict) {
const columns: Column[] = [
const columns: ScoreboardNode[] = [
{ type: 'rank', value: _('Rank') },
{ type: 'user', value: _('User') },
{ type: 'total_score', value: _('Score') },
@@ -278,32 +271,32 @@ const homework = {
);
}
columns.push({ type: 'total_time_str', value: _('Total Time') });
for (const index in tdoc.pids) {
const pid = tdoc.pids[index];
for (let i = 1; i <= tdoc.pids.length; i++) {
const pid = tdoc.pids[i - 1];
if (isExport) {
columns.push(
{
type: 'problem_score',
value: '#{0} {1}'.format(index + 1, pdict[pid].title),
value: '#{0} {1}'.format(i, pdict[pid].title),
},
{
type: 'problem_original_score',
value: '#{0} {1}'.format(index + 1, _('Original Score')),
value: '#{0} {1}'.format(i, _('Original Score')),
},
{
type: 'problem_time',
value: '#{0} {1}'.format(index + 1, _('Time (Seconds)')),
value: '#{0} {1}'.format(i, _('Time (Seconds)')),
},
{
type: 'problem_time_str',
value: '#{0} {1}'.format(index + 1, _('Time')),
value: '#{0} {1}'.format(i, _('Time')),
},
);
} else {
columns.push({
type: 'problem_detail',
value: '#{0}'.format(index + 1),
raw: pdict[pid],
value: '#{0}'.format(i),
raw: pid,
});
}
}
@@ -318,7 +311,7 @@ const homework = {
{
type: 'user',
value: udict[tsdoc.uid].uname,
raw: udict[tsdoc.uid],
raw: tsdoc.uid,
},
{
type: 'string',
@@ -331,10 +324,10 @@ const homework = {
}
row.push({ type: 'string', value: misc.formatSeconds(tsdoc.time || 0) });
for (const pid of tdoc.pids) {
const rdoc = (tsddict[pid] || {}).rid;
const colScore = (tsddict[pid] || {}).penaltyScore || '-';
const colOriginalScore = (tsddict[pid] || {}).score || '-';
const colTime = (tsddict[pid] || {}).time || '-';
const rid = tsddict[pid]?.rid;
const colScore = tsddict[pid]?.penaltyScore || '-';
const colOriginalScore = tsddict[pid]?.score || '-';
const colTime = tsddict[pid]?.time || '-';
const colTimeStr = colTime !== '-' ? misc.formatSeconds(colTime || 0) : '-';
if (isExport) {
row.push(
@@ -347,7 +340,7 @@ const homework = {
row.push({
type: 'record',
value: '{0} / {1}\n{2}'.format(colScore, colOriginalScore, colTimeStr),
raw: rdoc,
raw: rid,
});
}
}
@@ -358,10 +351,14 @@ const homework = {
rank: (tsdocs) => ranked(tsdocs, (a, b) => a.score === b.score),
};
export const RULES = {
export const RULES: ContestRules = {
acm, oi, homework,
};
function _getStatusJournal(tsdoc) {
return tsdoc.journal.sort((a, b) => (a.rid.generationTime - b.rid.generationTime));
}
export function add(
domainId: string, title: string, content: string, owner: number,
rule: string, beginAt = new Date(), endAt = new Date(), pids = [],
@@ -407,11 +404,14 @@ export async function updateStatus(
domainId: string, tid: ObjectID, uid: number, rid: ObjectID, pid: number,
accept = false, score = 0, type = document.TYPE_CONTEST,
) {
await get(domainId, tid, type);
const tdoc = await get(domainId, tid, type);
const tsdoc = await document.revPushStatus(domainId, type, tid, uid, 'journal', {
rid, pid, accept, score,
});
if (!tsdoc.attend) throw new ContestNotAttendedError(tid, uid);
const journal = _getStatusJournal(tsdoc);
const stats = RULES[tdoc.rule].stat(tdoc, journal);
return await document.revSetStatus(domainId, type, tid, uid, tsdoc.rev, { journal, ...stats });
}
export function getStatus(
@@ -498,13 +498,11 @@ export const ContestHandlerMixin = (c) => class extends c {
async getScoreboard(
domainId: string, tid: ObjectID,
isExport = false, docType = document.TYPE_CONTEST,
) {
): Promise<[Tdoc, ScoreboardNode[][], Udict]> {
const tdoc = await get(domainId, tid, docType);
if (!this.canShowScoreboard(tdoc)) throw new ContestScoreboardHiddenError(tid);
const tsdocs = await getMultiStatus(domainId, { docId: tid }, docType)
.sort(RULES[tdoc.rule].statusSort).toArray();
const uids = [];
for (const tsdoc of tsdocs) uids.push(tsdoc.uid);
const tsdocs = await getMultiStatus(domainId, { docId: tid }, docType).toArray();
const uids = tsdocs.map((tsdoc) => tsdoc.uid);
const [udict, pdict] = await Promise.all([
user.getList(domainId, uids),
problem.getList(domainId, tdoc.pids, true),
@@ -535,10 +533,6 @@ export function getMulti(domainId: string, query = {}, type = document.TYPE_CONT
return document.getMulti(domainId, type, query);
}
function _getStatusJournal(tsdoc) {
return tsdoc.journal.sort((a, b) => (a.rid.generationTime - b.rid.generationTime));
}
export async function getAndListStatus(
domainId: string, tid: ObjectID, docType = document.TYPE_CONTEST,
) {
+2 -3
View File
@@ -307,11 +307,10 @@ export async function revSetStatus(
};
const update = { $set: args, $inc: { rev: 1 } };
if (returnDoc) {
const res = await coll.findOneAndUpdate(filter, update, { returnOriginal: false });
const res = await collStatus.findOneAndUpdate(filter, update, { returnOriginal: false });
return res.value;
}
const res = await coll.updateOne(filter, update);
return res;
return await collStatus.updateOne(filter, update);
}
export async function ensureIndexes() {
+2 -2
View File
@@ -3,7 +3,7 @@ import { STATUS } from './builtin';
import * as file from './file';
import * as document from './document';
import * as domain from './domain';
import { Pdoc } from '../interface';
import { Pdoc, Pdict } from '../interface';
import { ProblemNotFoundError } from '../error';
import readConfig from '../lib/readConfig';
@@ -103,7 +103,7 @@ export async function random<T>(domainId: string, query: FilterQuery<T>) {
export async function getList(
domainId: string, pids: Array<number | string>,
getHidden = false, doThrow = true,
) {
): Promise<Pdict> {
pids = Array.from(new Set(pids));
const r = {};
const q: any = { $or: [{ docId: { $in: pids } }, { pid: { $in: pids } }] };
+6 -5
View File
@@ -112,8 +112,8 @@ export async function update(
return res.value;
}
export function reset(domainId: string, rid: ObjectID) {
return update(domainId, rid, {
export function reset(domainId: string, rid: ObjectID, isRejudge: boolean) {
const upd: any = {
score: 0,
status: STATUS.STATUS_WAITING,
time: 0,
@@ -123,8 +123,9 @@ export function reset(domainId: string, rid: ObjectID) {
compilerTexts: [],
judgeAt: null,
judger: null,
rejudged: true,
});
};
if (isRejudge) upd.rejudged = true;
return update(domainId, rid, upd);
}
export function count(domainId: string, query: any) {
@@ -176,7 +177,7 @@ export async function judge(domainId: string, rid: ObjectID) {
}
export async function rejudge(domainId: string, rid: ObjectID) {
await reset(domainId, rid);
await reset(domainId, rid, true);
const rdoc = await get(domainId, rid);
const pdoc = await problem.get(domainId, rdoc.pid);
await task.add({
+3
View File
@@ -117,6 +117,9 @@ SystemSetting(
Setting('setting_server', 'server.url', null, null, 'text', 'Server BaseURL'),
Setting('setting_server', 'server.port', null, 8888, 'number', 'Server Port'),
Setting('setting_server', 'server.log', null, false, 'boolean', 'Disable Access Log'),
Setting('setting_ui', 'ui.header', null, null, 'text', 'Header Logo'),
Setting('setting_ui', 'ui.headerBackground', null, null, 'text', 'Header Background'),
Setting('setting_ui', 'ui.nav', null, null, 'text', 'Nav Logo'),
Setting('setting_oauth', 'oauth.githubappid', null, null, 'text', 'Github Oauth AppID'),
Setting('setting_oauth', 'oauth.githubsecret', null, null, 'text', 'Github Oauth Secret', null, FLAG_SECRET),
Setting('setting_oauth', 'oauth.googleappid', null, null, 'text', 'Google Oauth ClientID', null),
+12 -14
View File
@@ -6,6 +6,7 @@ import { BUILTIN_ROLES, BUILTIN_USERS, PRIV } from './builtin';
import { UserNotFoundError, UserAlreadyExistError, LoginError } from '../error';
import pwhash from '../lib/hash.hydro';
import * as db from '../service/db';
import { Udoc, Udict } from '../interface';
const coll = db.collection('user');
@@ -17,7 +18,7 @@ export function setPassword(uid: number, password: string) {
);
}
export class User {
export class User implements Udoc {
udoc: () => any;
dudoc: () => any;
@@ -85,7 +86,7 @@ export class User {
}
hasPriv(p: number) {
return this.priv & p;
return !!(this.priv & p);
}
checkPassword(password: string) {
@@ -109,19 +110,16 @@ export async function getInDomain(domainId: string, udoc: User) {
return dudoc;
}
export async function getById(domainId: string, _id: number, throwError = false) {
export async function getById(domainId: string, _id: number): Promise<Udoc | null> {
const udoc = _id === 0 || _id === 1
? BUILTIN_USERS[_id]
: await coll.findOne({ _id });
if (!udoc) {
if (throwError) throw new UserNotFoundError(_id);
else return null;
}
if (!udoc) return null;
const dudoc = await getInDomain(domainId, udoc);
return new User(udoc, dudoc);
}
export async function getList(domainId: string, uids: number[]) {
export async function getList(domainId: string, uids: number[]): Promise<Udict> {
const _uids = new Set(uids);
const r = {};
// eslint-disable-next-line no-await-in-loop
@@ -129,7 +127,9 @@ export async function getList(domainId: string, uids: number[]) {
return r;
}
export async function getByUname(domainId: string, uname: string, ignoreMissing = false) {
export async function getByUname(
domainId: string, uname: string, ignoreMissing = false,
): Promise<Udoc> {
const unameLower = uname.trim().toLowerCase();
const udoc = (unameLower === 'guest')
? BUILTIN_USERS[0]
@@ -144,17 +144,14 @@ export async function getByUname(domainId: string, uname: string, ignoreMissing
return new User(udoc, dudoc);
}
export async function getByEmail(domainId: string, mail: string, ignoreMissing = false) {
export async function getByEmail(domainId: string, mail: string): Promise<Udoc | null> {
const mailLower = mail.trim().toLowerCase();
const udoc = (mailLower === 'guest@hydro.local')
? BUILTIN_USERS[0]
: mailLower === 'hydro@hydro.local'
? BUILTIN_USERS[1]
: await coll.findOne({ mailLower });
if (!udoc) {
if (ignoreMissing) return null;
throw new UserNotFoundError(mail);
}
if (!udoc) return null;
const dudoc = await getInDomain(domainId, udoc);
return new User(udoc, dudoc);
}
@@ -172,6 +169,7 @@ export function setEmail(uid: number, mail: string) {
export async function changePassword(uid: number, currentPassword: string, newPassword: string) {
const udoc = await getById('system', uid);
if (!udoc) throw new UserNotFoundError(uid);
udoc.checkPassword(currentPassword);
const salt = String.random();
return await coll.findOneAndUpdate(
+1 -1
View File
@@ -44,7 +44,7 @@ export async function run({ username = '', password = '' } = {}) {
await Promise.all(tasks);
await domain.add('system', 0, 'Hydro', true);
if (username && password) {
const udoc = await user.getById('system', -1, false);
const udoc = await user.getById('system', -1);
if (!udoc) {
await user.create({
uid: -1,
+1 -1
View File
@@ -376,7 +376,7 @@ export const Handler = HandlerMixin(class {
this.getBdoc(),
]);
[this.user] = await Promise.all([
user.getById(domainId, this.session.uid, true),
user.getById(domainId, this.session.uid),
this.getCsrfToken(),
]);
}
+4 -4
View File
@@ -14,15 +14,15 @@
<table class="data-table">
<colgroup>
{%- for column in rows[0] -%}
<col class="col--{{ column['type'] }}">
<col class="col--{{ column.type }}">
{%- endfor -%}
</colgroup>
<thead>
<tr>
{%- for column in rows[0] -%}
<th class="col--{{ column['type'] }}">
<th class="col--{{ column.type }}">
{% if column.type == 'problem_detail' %}
<a href="{{ url('contest_detail_problem', tid=tdoc.docId, pid=column.raw._id) }}" data-tooltip="{{ column['raw']['title'] }}">{{ column['value'] }}</a>
<a href="{{ url('contest_detail_problem', tid=tdoc.docId, pid=column.raw) }}" data-tooltip="{{ pdict[column.raw].title }}">{{ column.value }}</a>
{% else %}
{{ column.value }}
{% endif %}
@@ -36,7 +36,7 @@
{%- for column in row -%}
<td class="col--{{ rows[0][loop.index0]['type'] }}">
{% if column.type == 'user' %}
{{ user.render_inline(column.raw, badge=false) }}
{{ user.render_inline(udict[column.raw], badge=false) }}
{% elif column.type == 'record' and column.raw %}
<a href="{{ url('record_detail', rid=column.raw) }}">{{ column.value }}</a>
{% else %}
+10
View File
@@ -0,0 +1,10 @@
/* {% if header %} */
.header__logo {
background: url({{ header }}) !important
}
/* {% endif %} */
/* {% if headerBackground %} */
.header {
background: url({{ headerBackground }}) !important
}
/* {% endif %} */
+1
View File
@@ -23,6 +23,7 @@
<meta name="theme-color" content="#56758f">
<link rel="stylesheet" media="all" href="{{ static_url('vj4.css') }}">
<link rel="stylesheet" media="all" href="{{ static_url('extra.css') }}">
<title>{{ handler.renderTitle(page_name) }}</title>
{% if not isIE(handler.request.headers['user-agent']) %}
<script>