|
|
|
let Problem = syzoj.model('problem');
|
|
|
|
let Article = syzoj.model('article');
|
|
|
|
let ArticleComment = syzoj.model('article-comment');
|
|
|
|
let User = syzoj.model('user');
|
|
|
|
let Reply = syzoj.model('reply');
|
|
|
|
let NewReply = syzoj.model('new_reply');
|
|
|
|
|
|
|
|
app.get('/discussion/:type?', async (req, res) => {
|
|
|
|
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) {
|
|
|
|
where = { problem_id: TypeORM.Not(TypeORM.IsNull()) };
|
|
|
|
} else {
|
|
|
|
where = { problem_id: null };
|
|
|
|
}
|
|
|
|
let paginate = syzoj.utils.paginate(await Article.countForPagination(where), req.query.page, syzoj.config.page.discussion);
|
|
|
|
let articles = await Article.queryPage(paginate, where, {
|
|
|
|
sort_time: 'DESC'
|
|
|
|
});
|
|
|
|
let newReplyQuery = await NewReply.createQueryBuilder();
|
|
|
|
for (let article of articles) {
|
|
|
|
await article.loadRelationships();
|
|
|
|
if (in_problems) {
|
|
|
|
article.problem = await Problem.findById(article.problem_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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.countForPagination(where), req.query.page, syzoj.config.page.discussion);
|
|
|
|
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
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
syzoj.log(e);
|
|
|
|
res.render('error', {
|
|
|
|
err: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/article/:id', async (req, res) => {
|
|
|
|
try {
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
if (!article) throw new ErrorMessage('无此帖子。');
|
|
|
|
|
|
|
|
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 newReplyQuery = await NewReply.createQueryBuilder();
|
|
|
|
|
|
|
|
let where = { article_id: id };
|
|
|
|
let commentsCount = await NewReply.countForPagination(where);
|
|
|
|
let paginate = syzoj.utils.paginate(commentsCount, req.query.page, syzoj.config.page.article_comment);
|
|
|
|
|
|
|
|
let comments = await newReplyQuery.where('article_id=:id', {id}).getMany();
|
|
|
|
|
|
|
|
function arrayToTree(data){
|
|
|
|
for(let i=0;i<data.length;i++){
|
|
|
|
if (data[i].parent_id){
|
|
|
|
var info = data.find(item=>item.id===data[i].parent_id);
|
|
|
|
if (info) {
|
|
|
|
if (!info.children) {
|
|
|
|
info.children=[data[i]];
|
|
|
|
} else {
|
|
|
|
info.children.push(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data.filter(item=>!item.parent_id && item.is_show)
|
|
|
|
}
|
|
|
|
const originData = comments.filter(item => item.is_show);
|
|
|
|
const datas = arrayToTree(comments.filter(item => item.is_show));
|
|
|
|
const rootIdArray = datas.map(item=>item.id);
|
|
|
|
const authorId = article.user_id;
|
|
|
|
let count = 0;
|
|
|
|
async function operate(data){
|
|
|
|
for(let i=0;i<data.length;i++){
|
|
|
|
count++;
|
|
|
|
data[i].user = await User.findById(data[i].from_user_id);
|
|
|
|
data[i].avatarUrl = syzoj.utils.gravatar(data[i].user.email, 120);
|
|
|
|
data[i].date = syzoj.utils.formatDate(data[i].public_time)
|
|
|
|
data[i].toUser = await User.findById(data[i].to_user_id)
|
|
|
|
data[i].allowedEdit = await data[i].isAllowedEditBy(res.locals.user);
|
|
|
|
if (rootIdArray.includes(data[i].parent_id)){
|
|
|
|
data[i].firstComment = true;
|
|
|
|
}
|
|
|
|
if (data[i].children) {
|
|
|
|
await operate(data[i].children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await operate(datas);
|
|
|
|
let articleQuery = await Article.createQueryBuilder();
|
|
|
|
await articleQuery.update(Article).set({comments_num: count}).where("id = :id", { id }).execute();
|
|
|
|
datas.sort((a,b) => {
|
|
|
|
return b.public_time - a.public_time;
|
|
|
|
})
|
|
|
|
|
|
|
|
let problem = null;
|
|
|
|
if (article.problem_id) {
|
|
|
|
problem = await Problem.findById(article.problem_id);
|
|
|
|
if (!await problem.isAllowedUseBy(res.locals.user)) {
|
|
|
|
throw new ErrorMessage('您没有权限进行此操作。');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('article', {
|
|
|
|
user: res.locals.user,
|
|
|
|
article: article,
|
|
|
|
comments: datas,
|
|
|
|
paginate: paginate,
|
|
|
|
problem: problem,
|
|
|
|
commentsCount: count,
|
|
|
|
authorId
|
|
|
|
});
|
|
|
|
} 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 }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
|
|
|
|
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', {
|
|
|
|
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 }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
|
|
|
|
let time = syzoj.utils.getCurrentDate();
|
|
|
|
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) {
|
|
|
|
let problem = await Problem.findById(req.query.problem_id);
|
|
|
|
if (!problem) throw new ErrorMessage('无此题目。');
|
|
|
|
article.problem_id = problem.id;
|
|
|
|
} else {
|
|
|
|
article.problem_id = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.body.title.trim()) throw new ErrorMessage('标题不能为空。');
|
|
|
|
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);
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
try {
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
|
|
|
|
if (!article) {
|
|
|
|
throw new ErrorMessage('无此帖子。');
|
|
|
|
} else {
|
|
|
|
if (!await article.isAllowedEditBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
|
|
|
|
}
|
|
|
|
let newReplyQuery = await NewReply.createQueryBuilder();
|
|
|
|
|
|
|
|
await newReplyQuery.delete().from(NewReply).where("article_id = :id", { id }).execute();
|
|
|
|
|
|
|
|
await article.destroy();
|
|
|
|
|
|
|
|
res.redirect(syzoj.utils.makeUrl(['discussion', 'global']));
|
|
|
|
} 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 }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
let to_id = article.user_id;
|
|
|
|
if (!article) {
|
|
|
|
throw new ErrorMessage('无此帖子。');
|
|
|
|
} else {
|
|
|
|
if (!await article.isAllowedCommentBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
|
|
|
|
}
|
|
|
|
|
|
|
|
let comment = await NewReply.create({
|
|
|
|
content: req.body.comment,
|
|
|
|
article_id: id,
|
|
|
|
from_user_id: res.locals.user.id,
|
|
|
|
to_user_id: to_id,
|
|
|
|
public_time: syzoj.utils.getCurrentDate(),
|
|
|
|
update_time: syzoj.utils.getCurrentDate(),
|
|
|
|
is_show: true
|
|
|
|
});
|
|
|
|
|
|
|
|
await comment.save();
|
|
|
|
|
|
|
|
await article.resetReplyCountAndTime();
|
|
|
|
|
|
|
|
res.redirect(syzoj.utils.makeUrl(['article', article.id]));
|
|
|
|
} catch (e) {
|
|
|
|
syzoj.log(e);
|
|
|
|
res.render('error', {
|
|
|
|
err: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/article/:id/comment/:commentId', async (req, res) => {
|
|
|
|
try {
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let commentId = parseInt(req.params.commentId);
|
|
|
|
let userId = parseInt(res.locals.user.id);
|
|
|
|
let article = await Article.findById(id);
|
|
|
|
if (!article) {
|
|
|
|
throw new ErrorMessage('无此帖子。');
|
|
|
|
} else {
|
|
|
|
if (!await article.isAllowedCommentBy(res.locals.user)) throw new ErrorMessage('您没有权限进行此操作。');
|
|
|
|
}
|
|
|
|
// let articleQuery = await Article.createQueryBuilder();
|
|
|
|
// await articleQuery.update(Article).set({comments_num: article.comments_num + 1}).where("id = :id", { id }).execute();
|
|
|
|
const toInfo = await NewReply.findById(commentId);
|
|
|
|
|
|
|
|
let newReply = await NewReply.create({
|
|
|
|
content: req.body.comment,
|
|
|
|
article_id: id,
|
|
|
|
from_user_id: res.locals.user.id,
|
|
|
|
to_user_id: toInfo.from_user_id,
|
|
|
|
parent_id: commentId,
|
|
|
|
public_time: syzoj.utils.getCurrentDate(),
|
|
|
|
update_time: syzoj.utils.getCurrentDate(),
|
|
|
|
is_show: true
|
|
|
|
});
|
|
|
|
|
|
|
|
await newReply.save();
|
|
|
|
|
|
|
|
await article.resetReplyCountAndTime();
|
|
|
|
res.redirect(syzoj.utils.makeUrl(['article', article.id]));
|
|
|
|
} catch (e) {
|
|
|
|
syzoj.log(e);
|
|
|
|
res.render('error', {
|
|
|
|
err: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/article/:articleId/comment/:id/delete', async (req, res) => {
|
|
|
|
try {
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let articleId = parseInt(req.params.articleId);
|
|
|
|
let replyQuery = await NewReply.createQueryBuilder();
|
|
|
|
await replyQuery.update(NewReply).set({is_show: 0}).where("id = :id", { id }).execute();
|
|
|
|
|
|
|
|
|
|
|
|
res.redirect(syzoj.utils.makeUrl(['article', articleId]));
|
|
|
|
} catch (e) {
|
|
|
|
syzoj.log(e);
|
|
|
|
res.render('error', {
|
|
|
|
err: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/article/:articleId/reply/:id/delete', async (req, res) => {
|
|
|
|
try {
|
|
|
|
if (!res.locals.user) throw new ErrorMessage('请登录后继续。', { '登录': syzoj.utils.makeUrl(['login'], { 'url': req.originalUrl }) });
|
|
|
|
|
|
|
|
let id = parseInt(req.params.id);
|
|
|
|
let reply = await Reply.findById(id);
|
|
|
|
let articleId = parseInt(req.params.articleId);
|
|
|
|
let replyQuery = await Reply.createQueryBuilder();
|
|
|
|
if (!reply) {
|
|
|
|
throw new ErrorMessage('无此回复。');
|
|
|
|
}
|
|
|
|
|
|
|
|
await reply.destroy();
|
|
|
|
|
|
|
|
res.redirect(`/article/${articleId}`);
|
|
|
|
} catch (e) {
|
|
|
|
syzoj.log(e);
|
|
|
|
res.render('error', {
|
|
|
|
err: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|