diff --git a/models/article.js b/models/article.js index 0e2017b..41153d7 100644 --- a/models/article.js +++ b/models/article.js @@ -23,6 +23,7 @@ let Sequelize = require('sequelize'); let db = syzoj.db; let User = syzoj.model('user'); +const Problem = syzoj.model('problem'); let model = db.define('article', { id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, diff --git a/modules/discussion.js b/modules/discussion.js index b61f430..9ec6014 100644 --- a/modules/discussion.js +++ b/modules/discussion.js @@ -24,18 +24,34 @@ let Article = syzoj.model('article'); let ArticleComment = syzoj.model('article-comment'); let User = syzoj.model('user'); -app.get('/discussion', async (req, res) => { +app.get('/discussion/:type?', async (req, res) => { try { - let where = { problem_id: null }; + 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: { $not: null } }; + } else { + where = { problem_id: { $eq: null } }; + } let paginate = syzoj.utils.paginate(await Article.count(where), req.query.page, syzoj.config.page.discussion); let articles = await Article.query(paginate, where, [['public_time', 'desc']]); - for (let article of articles) await article.loadRelationships(); + for (let article of articles) { + await article.loadRelationships(); + if (in_problems) { + article.problem = await Problem.fromID(article.problem_id); + } + } res.render('discussion', { articles: articles, paginate: paginate, - problem: null + problem: null, + in_problems: in_problems }); } catch (e) { syzoj.log(e); @@ -45,7 +61,7 @@ app.get('/discussion', async (req, res) => { } }); -app.get('/problem/:pid/discussion', async (req, res) => { +app.get('/discussion/problem/:pid', async (req, res) => { try { let pid = parseInt(req.params.pid); let problem = await Problem.fromID(pid); @@ -63,7 +79,8 @@ app.get('/problem/:pid/discussion', async (req, res) => { res.render('discussion', { articles: articles, paginate: paginate, - problem: problem + problem: problem, + in_problems: false }); } catch (e) { syzoj.log(e); @@ -201,7 +218,7 @@ app.post('/article/:id/delete', async (req, res) => { await article.destroy(); - res.redirect(syzoj.utils.makeUrl(['discussion'])); + res.redirect(syzoj.utils.makeUrl(['discussion', 'global'])); } catch (e) { syzoj.log(e); res.render('error', { diff --git a/views/article.ejs b/views/article.ejs index 1d7a2ee..b40b6f4 100644 --- a/views/article.ejs +++ b/views/article.ejs @@ -10,11 +10,11 @@
标题 | -作者 | +标题 | + <% if (in_problems) { %> +所属题目 | + <% } %> +作者 | 发表时间 |
---|---|---|---|---|---|
<%= article.title %> | + <% if (in_problems) { %> +<%= article.problem.title %> | + <% } %><%= article.user.username %><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %> | <%= syzoj.utils.formatDate(article.public_time) %> |