算法评测平台前端。
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.

216 lines
6.4 KiB

8 years ago
let User = syzoj.model('user');
const RatingCalculation = syzoj.model('rating_calculation');
const RatingHistory = syzoj.model('rating_history');
const Contest = syzoj.model('contest');
const ContestPlayer = syzoj.model('contest_player');
8 years ago
// Ranklist
app.get('/ranklist', async (req, res) => {
try {
const sort = req.query.sort || syzoj.config.sorting.ranklist.field;
const order = req.query.order || syzoj.config.sorting.ranklist.order;
if (!['ac_num', 'rating', 'id', 'username'].includes(sort) || !['asc', 'desc'].includes(order)) {
throw new ErrorMessage('错误的排序参数。');
}
let paginate = syzoj.utils.paginate(await User.countForPagination({ is_show: true }), req.query.page, syzoj.config.page.ranklist);
6 years ago
let ranklist = await User.queryPage(paginate, { is_show: true }, { [sort]: order.toUpperCase() });
await ranklist.forEachAsync(async x => x.renderInformation());
8 years ago
res.render('ranklist', {
ranklist: ranklist,
paginate: paginate,
curSort: sort,
curOrder: order === 'asc'
8 years ago
});
} catch (e) {
syzoj.log(e);
8 years ago
res.render('error', {
err: e
});
}
});
app.get('/find_user', async (req, res) => {
try {
let user = await User.fromName(req.query.nickname);
if (!user) throw new ErrorMessage('无此用户。');
8 years ago
res.redirect(syzoj.utils.makeUrl(['user', user.id]));
} catch (e) {
syzoj.log(e);
8 years ago
res.render('error', {
err: e
});
}
});
// Login
app.get('/login', async (req, res) => {
if (res.locals.user) {
res.render('error', {
err: new ErrorMessage('您已经登录了,请先注销。', { '注销': syzoj.utils.makeUrl(['logout'], { 'url': req.originalUrl }) })
8 years ago
});
} else {
res.render('login');
}
});
// Sign up
app.get('/sign_up', async (req, res) => {
if (res.locals.user) {
res.render('error', {
err: new ErrorMessage('您已经登录了,请先注销。', { '注销': syzoj.utils.makeUrl(['logout'], { 'url': req.originalUrl }) })
8 years ago
});
} else {
res.render('sign_up');
}
});
// Logout
app.post('/logout', async (req, res) => {
8 years ago
req.session.user_id = null;
res.clearCookie('login');
res.redirect(req.query.url || '/');
8 years ago
});
// User page
app.get('/user/:id', async (req, res) => {
try {
let id = parseInt(req.params.id);
6 years ago
let user = await User.findById(id);
if (!user) throw new ErrorMessage('无此用户。');
8 years ago
user.ac_problems = await user.getACProblems();
user.articles = await user.getArticles();
8 years ago
user.allowedEdit = await user.isAllowedEditBy(res.locals.user);
8 years ago
let statistics = await user.getStatistics();
await user.renderInformation();
user.emailVisible = user.public_email || user.allowedEdit;
6 years ago
const ratingHistoryValues = await RatingHistory.find({
where: { user_id: user.id },
order: { rating_calculation_id: 'ASC' }
});
const ratingHistories = [{
7 years ago
contestName: "初始积分",
value: syzoj.config.default.user.rating,
delta: null,
rank: null
}];
for (const history of ratingHistoryValues) {
6 years ago
const contest = await Contest.findById((await RatingCalculation.findById(history.rating_calculation_id)).contest_id);
ratingHistories.push({
contestName: contest.title,
value: history.rating_after,
delta: history.rating_after - ratingHistories[ratingHistories.length - 1].value,
rank: history.rank,
participants: await ContestPlayer.count({ contest_id: contest.id })
});
}
7 years ago
ratingHistories.reverse();
8 years ago
res.render('user', {
show_user: user,
statistics: statistics,
ratingHistories: ratingHistories
8 years ago
});
} catch (e) {
syzoj.log(e);
8 years ago
res.render('error', {
err: e
});
}
});
app.get('/user/:id/edit', async (req, res) => {
try {
let id = parseInt(req.params.id);
6 years ago
let user = await User.findById(id);
if (!user) throw new ErrorMessage('无此用户。');
8 years ago
let allowedEdit = await user.isAllowedEditBy(res.locals.user);
if (!allowedEdit) {
throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
}
user.privileges = await user.getPrivileges();
res.locals.user.allowedManage = await res.locals.user.hasPrivilege('manage_user');
res.render('user_edit', {
8 years ago
edited_user: user,
error_info: null
});
} catch (e) {
syzoj.log(e);
8 years ago
res.render('error', {
err: e
});
}
});
app.get('/forget', async (req, res) => {
res.render('forget');
});
8 years ago
app.post('/user/:id/edit', async (req, res) => {
let user;
try {
let id = parseInt(req.params.id);
6 years ago
user = await User.findById(id);
if (!user) throw new ErrorMessage('无此用户。');
8 years ago
let allowedEdit = await user.isAllowedEditBy(res.locals.user);
if (!allowedEdit) throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
if (req.body.old_password && req.body.new_password) {
if (user.password !== req.body.old_password && !await res.locals.user.hasPrivilege('manage_user')) throw new ErrorMessage('旧密码错误。');
8 years ago
user.password = req.body.new_password;
}
if (res.locals.user && await res.locals.user.hasPrivilege('manage_user')) {
if (!syzoj.utils.isValidUsername(req.body.username)) throw new ErrorMessage('无效的用户名。');
user.username = req.body.username;
user.email = req.body.email;
}
if (res.locals.user && res.locals.user.is_admin) {
if (!req.body.privileges) {
req.body.privileges = [];
} else if (!Array.isArray(req.body.privileges)) {
req.body.privileges = [req.body.privileges];
}
let privileges = req.body.privileges;
await user.setPrivileges(privileges);
}
8 years ago
user.information = req.body.information;
user.sex = req.body.sex;
user.public_email = (req.body.public_email === 'on');
6 years ago
user.prefer_formatted_code = (req.body.prefer_formatted_code === 'on');
8 years ago
await user.save();
if (user.id === res.locals.user.id) res.locals.user = user;
user.privileges = await user.getPrivileges();
res.locals.user.allowedManage = await res.locals.user.hasPrivilege('manage_user');
res.render('user_edit', {
8 years ago
edited_user: user,
error_info: ''
8 years ago
});
} catch (e) {
user.privileges = await user.getPrivileges();
res.locals.user.allowedManage = await res.locals.user.hasPrivilege('manage_user');
res.render('user_edit', {
8 years ago
edited_user: user,
error_info: e.message
8 years ago
});
}
});