Browse Source

Add problem discussion list.

pull/6/head
t123yh 7 years ago
parent
commit
9917b293ea
  1. 1
      models/article.js
  2. 31
      modules/discussion.js
  3. 6
      views/article.ejs
  4. 42
      views/discussion.ejs
  5. 2
      views/header.ejs
  6. 2
      views/problem.ejs

1
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 },

31
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', {

6
views/article.ejs

@ -10,11 +10,11 @@
<div class="section">讨论</div>
<i class="right angle icon divider"></i>
<% if (problem) { %>
<div class="section">题目</div>
<div class="section"><a href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>">题目</a></div>
<i class="right angle icon divider"></i>
<a href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" class="active section"><%= problem.title %></a>
<a href="<%= syzoj.utils.makeUrl(['discussion', 'problem', problem.id]) %>" class="active section"><%= problem.title %></a>
<% } else { %>
<a href="<%= syzoj.utils.makeUrl(['discussion']) %>" class="section">全局板块</a>
<a href="<%= syzoj.utils.makeUrl(['discussion', 'global']) %>" class="section">全局板块</a>
<% } %>
</div>
<h1><%= article.title %></h1>

42
views/discussion.ejs

@ -8,33 +8,50 @@
<div class="section">讨论</div>
<i class="right angle icon divider"></i>
<% if (problem) { %>
<div class="section">题目</div>
<div class="section"><a href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>">题目</a></div>
<i class="right angle icon divider"></i>
<div class="active section"><%= problem.title %></div>
<% } else if (in_problems) { %>
<div class="section">题目</div>
<% } else { %>
<div class="section">全局板块</div>
<% } %>
</div>
</div>
<div class="six wide right aligned column">
<% if (problem) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>" class="ui labeled icon mini blue button">
<i class="arrow left icon"></i>
返回题目
<% if(in_problems) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['discussion', 'global']) %>" class="ui labeled icon mini blue button">
<i class="world icon"></i>
全局板块
</a>
<% } else { %>
<% if (problem) { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['problem', problem.id]) %>" class="ui labeled icon mini blue button">
<i class="arrow left icon"></i>
返回题目
</a>
<% } else { %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['discussion', 'problems']) %>" class="ui labeled icon mini blue button">
<i class="file outline icon"></i>
题目板块
</a>
<% } %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['article', 0, 'edit'], problem ? { problem_id: problem.id } : null) %>" class="ui labeled icon mini button">
<i class="write icon"></i>
发帖
</a>
<% } %>
<a style="margin-left: 10px; " href="<%= syzoj.utils.makeUrl(['article', 0, 'edit'], problem ? { problem_id: problem.id } : null) %>" class="ui labeled icon mini button">
<i class="write icon"></i>
发帖
</a>
</div>
</div>
</div>
<table class="ui very basic center aligned table">
<thead>
<tr>
<th class="left aligned" style="width: 65%; ">标题</th>
<th style="width: 15%; ">作者</th>
<th class="left aligned" style="width: 50%; ">标题</th>
<% if (in_problems) { %>
<th style="width: 20%; ">所属题目</th>
<% } %>
<th style="width: 10%; ">作者</th>
<th style="width: 20%; ">发表时间</th>
</tr>
</thead>
@ -42,6 +59,9 @@
<% for (let article of articles) { %>
<tr>
<td class="left aligned"><a href="<%= syzoj.utils.makeUrl(['article', article.id]) %>"><%= article.title %></a></td>
<% if (in_problems) { %>
<td><a href="<%= syzoj.utils.makeUrl(['discussion', 'problem', article.problem_id]) %>"><%= article.problem.title %></a></td>
<% } %>
<td><a href="<%= syzoj.utils.makeUrl(['user', article.user_id]) %>"><%= article.user.username %></a><% if (article.user.nameplate) { %><%- article.user.nameplate %><% } %></td>
<td><%= syzoj.utils.formatDate(article.public_time) %></td>
</tr>

2
views/header.ejs

@ -22,7 +22,7 @@
<a class="item<% if (active.startsWith('contest')) { %> active<% } %>" href="/contests"><i class="calendar icon"></i> 比赛</a>
<a class="item<% if (active.startsWith('submission')) { %> active<% } %>" href="/submissions"><i class="tasks icon"></i> 评测</a>
<a class="item<% if (active.startsWith('ranklist')) { %> active<% } %>" href="/ranklist"><i class="signal icon"></i> 排名</a>
<a class="item<% if (active.startsWith('discussion') || active.startsWith('article')) { %> active<% } %>" href="/discussion"><i class="comments icon"></i> 讨论</a>
<a class="item<% if (active.startsWith('discussion') || active.startsWith('article')) { %> active<% } %>" href="/discussion/global"><i class="comments icon"></i> 讨论</a>
<a class="item<% if (active.startsWith('help')) { %> active<% } %>" href="/help"><i class="help circle icon"></i> 帮助</a>
<% if (typeof contest !== 'undefined' && contest && contest.id) { %>
<a id="back_to_contest" class="item" href="<%= syzoj.utils.makeUrl(['contest', contest.id]) %>"><i class="arrow left icon"></i> 返回比赛</a>

2
views/problem.ejs

@ -96,7 +96,7 @@ div[class*=ace_br] {
<% } %>
<a class="small ui positive button" href="<%= syzoj.utils.makeUrl(['submissions'], { problem_id: problem.id }) %>">提交记录</a>
<a class="small ui orange button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'statistics', problem.type === 'submit-answer' ? 'min' : 'fastest']) %>">统计</a>
<a class="small ui brown button" href="<%= syzoj.utils.makeUrl(['problem', problem.id, 'discussion']) %>" style="position: relative; ">
<a class="small ui brown button" href="<%= syzoj.utils.makeUrl(['discussion', 'problem', problem.id]) %>" style="position: relative; ">
讨论
<% if (discussionCount) { %>
<div class="floating ui red tiny circular label"><%= discussionCount %></div>

Loading…
Cancel
Save