You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

8 years ago
let User = syzoj.model('user');
let Article = syzoj.model('article');
let Contest = syzoj.model('contest');
let Problem = syzoj.model('problem');
let Divine = require('syzoj-divine');
let TimeAgo = require('javascript-time-ago');
let zh = require('../libs/timeago');
TimeAgo.locale(zh);
const timeAgo = new TimeAgo('zh-CN');
8 years ago
app.get('/', async (req, res) => {
try {
6 years ago
let ranklist = await User.queryRange([1, syzoj.config.page.ranklist_index], { is_show: true }, {
[syzoj.config.sorting.ranklist.field]: syzoj.config.sorting.ranklist.order
});
await ranklist.forEachAsync(async x => x.renderInformation());
8 years ago
6 years ago
let notices = (await Article.find({
where: { is_notice: true },
6 years ago
order: { public_time: 'DESC' }
})).map(article => ({
title: article.title,
url: syzoj.utils.makeUrl(['article', article.id]),
date: syzoj.utils.formatDate(article.public_time, 'L')
}));
8 years ago
let fortune = null;
if (res.locals.user) {
fortune = Divine(res.locals.user.username, res.locals.user.sex);
}
6 years ago
let contests = await Contest.queryRange([1, 5], { is_public: true }, {
start_time: 'DESC'
});
6 years ago
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)),
}));
// 增加匿名用户名后用户的第一次登陆
if (res.locals.user){
const userQuery = await User.createQueryBuilder();
const userId = res.locals.user.id;
const userInfo = await userQuery.where("id = :id", { id:userId }).getOne();
if (!userInfo.anonymous_name || userInfo.anonymous_name.length === 0){
await userQuery.update(User).set({anonymous_name: require('randomstring').generate(6)}).where("id = :id", { id:userId }).execute();
}
}
8 years ago
res.render('index', {
ranklist: ranklist,
notices: notices,
fortune: fortune,
8 years ago
contests: contests,
problems: problems,
8 years ago
links: syzoj.config.links
8 years ago
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
8 years ago
app.get('/help', async (req, res) => {
try {
res.render('help');
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});