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.

278 lines
8.0 KiB

let Problem = syzoj.model('problem');
8 years ago
let Article = syzoj.model('article');
let ArticleComment = syzoj.model('article-comment');
let User = syzoj.model('user');
app.get('/discussion/:type?', async (req, res) => {
8 years ago
try {
if (!['global', 'problems'].includes(req.params.type)) {
res.redirect(syzoj.utils.makeUrl(['discussion', 'global']));
}
const in_problems = req.params.type === 'problems';
let where;
if (in_problems) {
6 years ago
where = { problem_id: TypeORM.Not(null) };
} else {
6 years ago
where = { problem_id: null };
}
let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion);
6 years ago
let articles = await Article.queryPage(paginate, where, {
sort_time: 'DESC'
});
8 years ago
for (let article of articles) {
await article.loadRelationships();
if (in_problems) {
6 years ago
article.problem = await Problem.findById(article.problem_id);
}
}
8 years ago
res.render('discussion', {
articles: articles,
paginate: paginate,
problem: null,
in_problems: in_problems
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/discussion/problem/:pid', async (req, res) => {
try {
let pid = parseInt(req.params.pid);
6 years ago
let problem = await Problem.findById(pid);
if (!problem) throw new ErrorMessage('无此题目。');
if (!await problem.isAllowedUseBy(res.locals.user)) {
throw new ErrorMessage('您没有权限进行此操作。');
}
let where = { problem_id: pid };
let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion);
6 years ago
let articles = await Article.queryPage(paginate, where, {
sort_time: 'DESC'
});
for (let article of articles) await article.loadRelationships();
res.render('discussion', {
articles: articles,
paginate: paginate,
problem: problem,
in_problems: false
8 years ago
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/article/:id', async (req, res) => {
try {
let id = parseInt(req.params.id);
6 years ago
let article = await Article.findById(id);
if (!article) throw new ErrorMessage('无此帖子。');
8 years ago
await article.loadRelationships();
article.allowedEdit = await article.isAllowedEditBy(res.locals.user);
article.allowedComment = await article.isAllowedCommentBy(res.locals.user);
article.content = await syzoj.utils.markdown(article.content);
let where = { article_id: id };
let commentsCount = await ArticleComment.count(where);
let paginate = syzoj.utils.paginate(commentsCount, req.query.page, syzoj.config.page.article_comment);
8 years ago
6 years ago
let comments = await ArticleComment.queryPage(paginate, where, {
public_time: 'DESC'
});
8 years ago
8 years ago
for (let comment of comments) {
comment.content = await syzoj.utils.markdown(comment.content);
comment.allowedEdit = await comment.isAllowedEditBy(res.locals.user);
await comment.loadRelationships();
8 years ago
}
let problem = null;
if (article.problem_id) {
6 years ago
problem = await Problem.findById(article.problem_id);
if (!await problem.isAllowedUseBy(res.locals.user)) {
throw new ErrorMessage('您没有权限进行此操作。');
}
}
8 years ago
res.render('article', {
article: article,
comments: comments,
paginate: paginate,
problem: problem,
commentsCount: commentsCount
8 years ago
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.get('/article/:id/edit', async (req, res) => {
try {
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
8 years ago
let id = parseInt(req.params.id);
6 years ago
let article = await Article.findById(id);
8 years ago
if (!article) {
article = await Article.create();
article.id = 0;
article.allowedEdit = true;
} else {
article.allowedEdit = await article.isAllowedEditBy(res.locals.user);
}
res.render('article_edit', {
8 years ago
article: article
});
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.post('/article/:id/edit', async (req, res) => {
try {
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
8 years ago
let id = parseInt(req.params.id);
6 years ago
let article = await Article.findById(id);
8 years ago
let time = syzoj.utils.getCurrentDate();
8 years ago
if (!article) {
article = await Article.create();
article.user_id = res.locals.user.id;
article.public_time = article.sort_time = time;
if (req.query.problem_id) {
6 years ago
let problem = await Problem.findById(req.query.problem_id);
if (!problem) throw new ErrorMessage('无此题目。');
article.problem_id = problem.id;
} else {
article.problem_id = null;
}
8 years ago
} else {
if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
}
if (!req.body.title.trim()) throw new ErrorMessage('标题不能为空。');
8 years ago
article.title = req.body.title;
article.content = req.body.content;
article.update_time = time;
article.is_notice = (res.locals.user && res.locals.user.is_admin ? req.body.is_notice === 'on' : article.is_notice);
8 years ago
await article.save();
res.redirect(syzoj.utils.makeUrl(['article', article.id]));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.post('/article/:id/delete', async (req, res) => {
8 years ago
try {
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
8 years ago
let id = parseInt(req.params.id);
6 years ago
let article = await Article.findById(id);
8 years ago
if (!article) {
throw new ErrorMessage('无此帖子。');
8 years ago
} else {
if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
}
await article.destroy();
res.redirect(syzoj.utils.makeUrl(['discussion', 'global']));
8 years ago
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.post('/article/:id/comment', async (req, res) => {
try {
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
8 years ago
let id = parseInt(req.params.id);
6 years ago
let article = await Article.findById(id);
8 years ago
if (!article) {
throw new ErrorMessage('无此帖子。');
8 years ago
} else {
if (!await article.isAllowedCommentBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
}
let comment = await ArticleComment.create({
content: req.body.comment,
article_id: id,
user_id: res.locals.user.id,
public_time: syzoj.utils.getCurrentDate()
8 years ago
});
await comment.save();
article.sort_time = syzoj.utils.getCurrentDate();
article.comments_num += 1;
await article.save();
8 years ago
res.redirect(syzoj.utils.makeUrl(['article', article.id]));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});
app.post('/article/:article_id/comment/:id/delete', async (req, res) => {
8 years ago
try {
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
8 years ago
let id = parseInt(req.params.id);
6 years ago
let comment = await ArticleComment.findById(id);
8 years ago
if (!comment) {
throw new ErrorMessage('无此评论。');
8 years ago
} else {
if (!await comment.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
8 years ago
}
await comment.destroy();
let article = comment.article;
article.comments_num -= 1;
await article.save();
8 years ago
res.redirect(syzoj.utils.makeUrl(['article', comment.article_id]));
} catch (e) {
syzoj.log(e);
res.render('error', {
err: e
});
}
});