mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
多时区支持
This commit is contained in:
1 parent
d14b63883e
commit
8c20c34d8b
9 files changed
+163
-104
No files matched your search
@@ -1,3 +1,4 @@
|
||||
const moment = require('moment-timezone');
|
||||
const {
|
||||
ContestNotLiveError, ValidationError, ProblemNotFoundError,
|
||||
ContestNotAttendedError,
|
||||
@@ -150,12 +151,13 @@ class ContestEditHandler extends ContestDetailHandler {
|
||||
}) {
|
||||
let beginAt;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.uset.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
const endAt = new Date(beginAt + duration * 3600 * 1000);
|
||||
if (beginAt >= endAt) throw new ValidationError('duration');
|
||||
const endAt = beginAt.add(duration, 'hours').toDate();
|
||||
if (beginAt.isSameOrAfter(endAt)) throw new ValidationError('duration');
|
||||
beginAt = beginAt.toDate();
|
||||
pids = await this.verifyProblems(domainId, pids);
|
||||
await contest.edit(domainId, this.tdoc.docId, title, content, rule, beginAt, endAt, pids);
|
||||
if (this.tdoc.beginAt !== beginAt || this.tdoc.endAt !== endAt
|
||||
@@ -286,11 +288,11 @@ class ContestCreateHandler extends ContestHandler {
|
||||
}) {
|
||||
let beginAt;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
const endAt = beginAt.delta({ hour: duration });
|
||||
const endAt = beginAt.add(duration, 'hours');
|
||||
pids = await this.verifyProblems(domainId, pids);
|
||||
const tid = await contest.add(
|
||||
domainId, title, content,
|
||||
|
||||
@@ -160,7 +160,7 @@ class HomeSettingsHandler extends Handler {
|
||||
async get({ category }) {
|
||||
const path = [
|
||||
['Hydro', '/'],
|
||||
['home_settings', null],
|
||||
[`home_${category}`, null],
|
||||
];
|
||||
this.response.template = 'home_settings.html';
|
||||
this.response.body = {
|
||||
@@ -179,11 +179,11 @@ class HomeSettingsHandler extends Handler {
|
||||
async post(args) {
|
||||
const $set = {};
|
||||
for (const key in args) {
|
||||
if (setting.SETTINGS_BY_KEY[args]) {
|
||||
if (setting.SETTINGS_BY_KEY[key]) {
|
||||
$set[key] = args[key];
|
||||
}
|
||||
}
|
||||
await user.setById(args.domainId, this.user._id, $set);
|
||||
await user.setById(this.user._id, $set);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
+26
-18
@@ -1,4 +1,5 @@
|
||||
const yaml = require('js-yaml');
|
||||
const moment = require('moment-timezone');
|
||||
const {
|
||||
ValidationError, HomeworkNotLiveError, ProblemNotFoundError,
|
||||
HomeworkNotAttendedError,
|
||||
@@ -174,8 +175,8 @@ class HomeworkDetailProblemSubmitHandler extends HomeworkDetailProblemHandler {
|
||||
|
||||
class HomeworkCreateHandler extends HomeworkHandler {
|
||||
async get() {
|
||||
const beginAt = new Date().delta({ day: 1 });
|
||||
const penaltySince = beginAt.delta({ day: 7 });
|
||||
const beginAt = moment().add(1, 'day');
|
||||
const penaltySince = beginAt.add(7, 'days');
|
||||
const path = [
|
||||
['Hydro', '/'],
|
||||
['homework_main', '/homework'],
|
||||
@@ -184,9 +185,9 @@ class HomeworkCreateHandler extends HomeworkHandler {
|
||||
this.response.template = 'homework_edit.html';
|
||||
this.response.body = {
|
||||
path,
|
||||
dateBeginText: beginAt.format('%Y-%m-%d'),
|
||||
dateBeginText: beginAt.tz(this.user.timeZone).format('YYYY-M-D'),
|
||||
timeBeginText: '00:00',
|
||||
datePenaltyText: penaltySince.format('%Y-%m-%d'),
|
||||
datePenaltyText: penaltySince.tz(this.user.timeZone).format('YYYY-M-D'),
|
||||
timePenaltyText: '23:59',
|
||||
pids: '1000, 1001',
|
||||
extensionDays: 1,
|
||||
@@ -202,21 +203,23 @@ class HomeworkCreateHandler extends HomeworkHandler {
|
||||
let beginAt;
|
||||
let penaltySince;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
try {
|
||||
penaltySince = new Date(Date.parse(`${penaltySinceDate} ${penaltySinceTime.replace('-', ':')}`));
|
||||
penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('endAtDate', 'endAtTime');
|
||||
}
|
||||
const endAt = penaltySince.delta({ days: extensionDays });
|
||||
if (beginAt >= penaltySince) { throw new ValidationError('endAtDate', 'endAtTime'); }
|
||||
if (penaltySince > endAt) throw new ValidationError('extensionDays');
|
||||
const endAt = penaltySince.add(extensionDays, 'days');
|
||||
if (beginAt.isSameOrAfter(penaltySince)) throw new ValidationError('endAtDate', 'endAtTime');
|
||||
if (penaltySince.isAfter(endAt)) throw new ValidationError('extensionDays');
|
||||
penaltySince = penaltySince.toDate();
|
||||
await this.verifyProblems(domainId, pids);
|
||||
const tid = await contest.add(domainId, title, content, this.user._id,
|
||||
'homework', beginAt, endAt, pids, rated, { penaltySince, penaltyRules }, document.TYPE_HOMEWORK);
|
||||
'homework', beginAt.toDate(), endAt.toDate(), pids, rated,
|
||||
{ penaltySince, penaltyRules }, document.TYPE_HOMEWORK);
|
||||
this.response.body = { tid };
|
||||
this.response.redirect = `/homework/${tid}`;
|
||||
}
|
||||
@@ -235,13 +238,15 @@ class HomeworkEditHandler extends HomeworkHandler {
|
||||
[tdoc.title, `/homework/${tid}`, true],
|
||||
['homework_edit', null],
|
||||
];
|
||||
const beginAt = moment(tdoc.beginAt).tz(this.user.timeZone);
|
||||
const penaltySince = moment(tdoc.penaltySince).tz(this.user.timeZone);
|
||||
this.response.template = 'homework_edit.html';
|
||||
this.response.body = {
|
||||
tdoc,
|
||||
dateBeginText: tdoc.beginAt.format('%Y-%m-%d'),
|
||||
timeBeginText: tdoc.beginAt.format('%H:%M'),
|
||||
datePenaltyText: tdoc.penaltySince.format('%Y-%m-%d'),
|
||||
timePenaltyText: tdoc.penaltySince.format('%H:%M'),
|
||||
dateBeginText: beginAt.format('YYYY-M-D'),
|
||||
timeBeginText: beginAt.format('hh:mm'),
|
||||
datePenaltyText: penaltySince.format('YYYY-M-D'),
|
||||
timePenaltyText: penaltySince.format('hh:mm'),
|
||||
extensionDays,
|
||||
penaltyRules: yaml.safeDump(tdoc.penaltyRules),
|
||||
pids: tdoc.pids.join(','),
|
||||
@@ -259,18 +264,21 @@ class HomeworkEditHandler extends HomeworkHandler {
|
||||
let beginAt;
|
||||
let penaltySince;
|
||||
try {
|
||||
beginAt = new Date(Date.parse(`${beginAtDate} ${beginAtTime.replace('-', ':')}`));
|
||||
beginAt = moment.tz(`${beginAtDate} ${beginAtTime}`, this.user.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('beginAtDate', 'beginAtTime');
|
||||
}
|
||||
try {
|
||||
penaltySince = new Date(Date.parse(`${penaltySinceDate} ${penaltySinceTime.replace('-', ':')}`));
|
||||
penaltySince = moment.tz(`${penaltySinceDate} ${penaltySinceTime}`, this.user.timeZone);
|
||||
} catch (e) {
|
||||
throw new ValidationError('endAtDate', 'endAtTime');
|
||||
}
|
||||
const endAt = penaltySince.delta({ days: extensionDays });
|
||||
if (beginAt >= penaltySince) throw new ValidationError('end_at_date', 'end_at_time');
|
||||
let endAt = penaltySince.add(extensionDays, 'days');
|
||||
if (beginAt.isSameOrAfter(penaltySince)) throw new ValidationError('endAtDate', 'endAtTime');
|
||||
await this.verifyProblems(domainId, pids);
|
||||
beginAt = beginAt.toDate();
|
||||
endAt = endAt.toDate();
|
||||
penaltySince = penaltySince.toDate();
|
||||
await contest.edit(domainId, tid, {
|
||||
title, content, beginAt, endAt, pids, penaltySince, penaltyRules,
|
||||
}, document.TYPE_HOMEWORK);
|
||||
|
||||
@@ -227,7 +227,7 @@ function add(domainId, title, content, owner, rule,
|
||||
* @param {ObjectID} tid
|
||||
* @param {object} $set
|
||||
* @param {number} type
|
||||
* @returns {Tdoc} tdoc after modification
|
||||
* @returns {Promise<Tdoc>} tdoc after modification
|
||||
*/
|
||||
async function edit(domainId, tid, $set, type = document.TYPE_CONTEST) {
|
||||
if ($set.rule) {
|
||||
|
||||
+11
-2
@@ -1,7 +1,16 @@
|
||||
const moment = require('moment-timezone');
|
||||
const builtin = require('./builtin');
|
||||
const options = require('../options');
|
||||
const i18n = require('../lib/i18n');
|
||||
|
||||
const countries = moment.tz.countries();
|
||||
const tzs = new Set();
|
||||
for (const country of countries) {
|
||||
const tz = moment.tz.zonesForCountry(country);
|
||||
for (const t of tz) tzs.add(t);
|
||||
}
|
||||
const timezones = Array.from(tzs).sort().map((tz) => [tz, tz]);
|
||||
|
||||
const Setting = (
|
||||
family, key, range = null,
|
||||
value = null, ui = 'text', name = '',
|
||||
@@ -11,9 +20,9 @@ const Setting = (
|
||||
});
|
||||
|
||||
const PREFERENCE_SETTINGS = [
|
||||
Setting('setting_display', 'viewLang', i18n,
|
||||
Setting('setting_display', 'viewLang', builtin.VIEW_LANGS.map((i) => [i.code, i.name]),
|
||||
options.default_locale, 'select', 'UI Language'),
|
||||
Setting('setting_display', 'timezone', [], // TODO(masnn) timezone
|
||||
Setting('setting_display', 'timezone', timezones,
|
||||
'Asia/Shanghai', 'select', 'Timezone'),
|
||||
Setting('setting_usage', 'codeLang', builtin.LANG_TEXTS,
|
||||
null, 'select', 'Default Code Language'),
|
||||
|
||||
@@ -19,6 +19,7 @@ class USER {
|
||||
this.salt = () => udoc.salt;
|
||||
this.hash = () => udoc.hash;
|
||||
this.priv = udoc.priv;
|
||||
this.timeZone = udoc.timeZone || 'Asia/Shanghai';
|
||||
this.viewLang = udoc.viewLang || 'zh_CN';
|
||||
this.codeLang = udoc.codeLang || 'c';
|
||||
this.codeTemplate = udoc.codeTemplate || '';
|
||||
|
||||
@@ -306,8 +306,12 @@ function Route(route, RouteHandler, permission = null) {
|
||||
domainId: 'system', ...ctx.params, ...ctx.query, ...ctx.request.body,
|
||||
};
|
||||
if (h.request.host !== await system.get('server.host')) {
|
||||
args.domainId = (h.request.url.split('//')[1] || '')
|
||||
.split(`.${await system.get('server.host')}`)[0] || 'system';
|
||||
try {
|
||||
args.domainId = (h.request.url.split('//')[1] || '')
|
||||
.split(`.${await system.get('server.host')}`)[0] || 'system';
|
||||
} catch (e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (h.___prepare) await h.___prepare(args);
|
||||
|
||||
Reference in new issue
Block a user