forked from syzoj/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
86 lines (78 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
let User = syzoj.model('user');
let Article = syzoj.model('article');
let Contest = syzoj.model('contest');
let Problem = syzoj.model('problem');
let Divine = syzoj.lib('divine');
let TimeAgo = require('javascript-time-ago');
let zh = require('../libs/timeago');
TimeAgo.locale(zh);
const timeAgo = new TimeAgo('zh-CN');
app.get('/', async (req, res) => {
try {
let ranklist_where = { is_show: true };
if (syzoj.config.ranklist_rated_only) ranklist_where.is_rated = true;
if (syzoj.config.ranklist_verified_only) ranklist_where.is_verified = true;
let ranklist = await User.queryRange([1, syzoj.config.page.ranklist_index], ranklist_where, {
[syzoj.config.sorting.ranklist.field]: syzoj.config.sorting.ranklist.order
});
await ranklist.forEachAsync(async x => x.renderInformation());
let notices = (await Article.find({
where: { is_notice: true },
order: { update_time: 'DESC' }
})).map(article => ({
title: article.title,
url: syzoj.utils.makeUrl(['article', article.id]),
date: syzoj.utils.formatDate(article.update_time, 'L')
}));
let fortune = null;
if (res.locals.user && syzoj.config.divine) {
fortune = Divine(res.locals.user.username, res.locals.user.sex);
}
let contests = await Contest.queryRange([1, 5], { is_public: true }, {
start_time: 'DESC'
});
let problems = (await Problem.queryRange([1, 5], { is_public: true }, {
publicize_time: 'DESC'
})).map(problem => ({
id: problem.id,
title: problem.title,
time: timeAgo.format(new Date(problem.publicize_time)),
}));
let todoList = null;
if (res.locals.user) {
let problem_ids = await res.locals.user.getTodoList();
todoList = await problem_ids.mapAsync(async (problem_id) => {
let problem = await Problem.findOne({
select: ["id", "title"],
where: { id: problem_id }
});
problem.judge_state = await problem.getJudgeState(res.locals.user, true);
return problem;
});
}
res.render('index', {
ranklist: ranklist,
notices: notices,
fortune: fortune,
contests: contests,
problems: problems,
todoList: todoList,
links: syzoj.config.links
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/help', async (req, res) => {
try {
res.render('help');
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});