From 4d68579806cdd06f262767ed7ecf7855dedf73b8 Mon Sep 17 00:00:00 2001 From: hewenyang Date: Sat, 14 Apr 2018 16:11:38 +0000 Subject: [PATCH 1/4] Show latest updates in main page --- libs/timeago/index.js | 7 ++++++ libs/timeago/long.json | 53 ++++++++++++++++++++++++++++++++++++++++ libs/timeago/narrow.json | 53 ++++++++++++++++++++++++++++++++++++++++ libs/timeago/short.json | 53 ++++++++++++++++++++++++++++++++++++++++ models/problem.js | 15 ++++++++++-- modules/index.js | 17 ++++++++++--- package.json | 1 + views/index.ejs | 21 ++++++++++++++++ 8 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 libs/timeago/index.js create mode 100644 libs/timeago/long.json create mode 100644 libs/timeago/narrow.json create mode 100644 libs/timeago/short.json diff --git a/libs/timeago/index.js b/libs/timeago/index.js new file mode 100644 index 0000000..ccb08e6 --- /dev/null +++ b/libs/timeago/index.js @@ -0,0 +1,7 @@ +module.exports = +{ + locale: 'zh', + long: require('./long.json'), + short: require('./short.json'), + narrow: require('./narrow.json') +} \ No newline at end of file diff --git a/libs/timeago/long.json b/libs/timeago/long.json new file mode 100644 index 0000000..685227c --- /dev/null +++ b/libs/timeago/long.json @@ -0,0 +1,53 @@ +{ + "year": { + "previous": "去年", + "current": "今年", + "next": "明年", + "past": "{0} 年前", + "future": "{0} 年后" + }, + "quarter": { + "previous": "上季度", + "current": "本季度", + "next": "下季度", + "past": "{0} 个季度前", + "future": "{0} 个季度后" + }, + "month": { + "previous": "上个月", + "current": "本月", + "next": "下个月", + "past": "{0} 个月前", + "future": "{0} 个月后" + }, + "week": { + "previous": "上周", + "current": "本周", + "next": "下周", + "past": "{0} 周前", + "future": "{0} 周后" + }, + "day": { + "previous": "昨天", + "current": "今天", + "next": "明天", + "past": "{0} 天前", + "future": "{0} 天后" + }, + "hour": { + "current": "这一时间 / 此时", + "past": "{0} 小时前", + "future": "{0} 小时后" + }, + "minute": { + "current": "此刻", + "past": "{0} 分钟前", + "future": "{0} 分钟后" + }, + "second": { + "current": "现在", + "past": "{0} 秒钟前", + "future": "{0} 秒钟后" + }, + "now": "现在" +} diff --git a/libs/timeago/narrow.json b/libs/timeago/narrow.json new file mode 100644 index 0000000..c0e45e6 --- /dev/null +++ b/libs/timeago/narrow.json @@ -0,0 +1,53 @@ +{ + "year": { + "previous": "去年", + "current": "今年", + "next": "明年", + "past": "{0} 年前", + "future": "{0} 年后" + }, + "quarter": { + "previous": "上季度", + "current": "本季度", + "next": "下季度", + "past": "{0} 个季度前", + "future": "{0} 个季度后" + }, + "month": { + "previous": "上个月", + "current": "本月", + "next": "下个月", + "past": "{0} 个月前", + "future": "{0} 个月后" + }, + "week": { + "previous": "上周", + "current": "本周", + "next": "下周", + "past": "{0} 周前", + "future": "{0} 周后" + }, + "day": { + "previous": "昨天", + "current": "今天", + "next": "明天", + "past": "{0} 天前", + "future": "{0} 天后" + }, + "hour": { + "current": "这一时间 / 此时", + "past": "{0} 小时前", + "future": "{0} 小时后" + }, + "minute": { + "current": "此刻", + "past": "{0} 分钟前", + "future": "{0} 分钟后" + }, + "second": { + "current": "现在", + "past": "{0} 秒前", + "future": "{0} 秒后" + }, + "now": "现在" +} diff --git a/libs/timeago/short.json b/libs/timeago/short.json new file mode 100644 index 0000000..c0e45e6 --- /dev/null +++ b/libs/timeago/short.json @@ -0,0 +1,53 @@ +{ + "year": { + "previous": "去年", + "current": "今年", + "next": "明年", + "past": "{0} 年前", + "future": "{0} 年后" + }, + "quarter": { + "previous": "上季度", + "current": "本季度", + "next": "下季度", + "past": "{0} 个季度前", + "future": "{0} 个季度后" + }, + "month": { + "previous": "上个月", + "current": "本月", + "next": "下个月", + "past": "{0} 个月前", + "future": "{0} 个月后" + }, + "week": { + "previous": "上周", + "current": "本周", + "next": "下周", + "past": "{0} 周前", + "future": "{0} 周后" + }, + "day": { + "previous": "昨天", + "current": "今天", + "next": "明天", + "past": "{0} 天前", + "future": "{0} 天后" + }, + "hour": { + "current": "这一时间 / 此时", + "past": "{0} 小时前", + "future": "{0} 小时后" + }, + "minute": { + "current": "此刻", + "past": "{0} 分钟前", + "future": "{0} 分钟后" + }, + "second": { + "current": "现在", + "past": "{0} 秒前", + "future": "{0} 秒后" + }, + "now": "现在" +} diff --git a/models/problem.js b/models/problem.js index 1e6a32f..e89eccb 100644 --- a/models/problem.js +++ b/models/problem.js @@ -244,12 +244,17 @@ let model = db.define('problem', { file_io_input_name: { type: Sequelize.TEXT }, file_io_output_name: { type: Sequelize.TEXT }, + create_time: { type: Sequelize.DATE }, + last_update: { type: Sequelize.DATE }, + type: { type: Sequelize.ENUM, values: ['traditional', 'submit-answer', 'interaction'] } }, { - timestamps: false, + timestamps: true, + createdAt: 'create_time', + updatedAt: 'last_update', tableName: 'problem', indexes: [ { @@ -257,7 +262,13 @@ let model = db.define('problem', { }, { fields: ['user_id'], - } + }, + { + fields: ['create_time'], + }, + { + fields: ['last_update'], + }, ] }); diff --git a/modules/index.js b/modules/index.js index 7d1fb37..023686e 100644 --- a/modules/index.js +++ b/modules/index.js @@ -22,7 +22,12 @@ let User = syzoj.model('user'); let Article = syzoj.model('article'); let Contest = syzoj.model('contest'); +let Problem = syzoj.model('problem'); let Divine = require('syzoj-divine'); +let TimeAgo = require('javascript-time-ago'); +let zh = require('../libs/timeago'); +TimeAgo.locale(zh); +const timeAgo = new TimeAgo('zh-CN'); app.get('/', async (req, res) => { try { @@ -40,16 +45,20 @@ app.get('/', async (req, res) => { fortune = Divine(res.locals.user.username, res.locals.user.sex); } - let where; - if (res.locals.user && await res.locals.user.is_admin) where = {} - else where = { is_public: true }; - let contests = await Contest.query([1, 5], where, [['start_time', 'desc']]); + let contests = await Contest.query([1, 5], { is_public: true }, [['start_time', 'desc']]); + + let problems = (await Problem.query([1, 5], { is_public: true }, [['last_update', 'desc']])).map(problem => ({ + id: problem.id, + title: problem.title, + time: timeAgo.format(new Date(problem.last_update)), + })); res.render('index', { ranklist: ranklist, notices: notices, fortune: fortune, contests: contests, + problems: problems, links: syzoj.config.links }); } catch (e) { diff --git a/package.json b/package.json index e843190..428b448 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "file-size": "^1.0.0", "fs-extra": "^4.0.1", "gravatar": "^1.5.2", + "javascript-time-ago": "^1.0.30", "js-yaml": "^3.9.0", "jsonwebtoken": "^7.4.3", "moemark-renderer": "^1.2.6", diff --git a/views/index.ejs b/views/index.ejs index 0c6994f..e80c8cd 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -92,6 +92,27 @@ +

最新更新

+
+ + + + + + + + + <% + for (let problem of problems) { + %> + + + + + <% } %> + +
题目更新时间
<%= problem.title %><%= problem.time %>
+
<% if (fortune) { let color; From 16726a08b1c41ff708205a21a9c841f4f791f271 Mon Sep 17 00:00:00 2001 From: hewenyang Date: Sat, 14 Apr 2018 17:04:50 +0000 Subject: [PATCH 2/4] Use publicize time instead --- models/problem.js | 12 +++--------- modules/index.js | 4 ++-- modules/problem.js | 1 + 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/models/problem.js b/models/problem.js index e89eccb..9014e25 100644 --- a/models/problem.js +++ b/models/problem.js @@ -244,17 +244,14 @@ let model = db.define('problem', { file_io_input_name: { type: Sequelize.TEXT }, file_io_output_name: { type: Sequelize.TEXT }, - create_time: { type: Sequelize.DATE }, - last_update: { type: Sequelize.DATE }, + publicize_time: { type: Sequelize.DATE }, type: { type: Sequelize.ENUM, values: ['traditional', 'submit-answer', 'interaction'] } }, { - timestamps: true, - createdAt: 'create_time', - updatedAt: 'last_update', + timestamps: false, tableName: 'problem', indexes: [ { @@ -264,10 +261,7 @@ let model = db.define('problem', { fields: ['user_id'], }, { - fields: ['create_time'], - }, - { - fields: ['last_update'], + fields: ['publicize_time'], }, ] }); diff --git a/modules/index.js b/modules/index.js index 023686e..73741a4 100644 --- a/modules/index.js +++ b/modules/index.js @@ -47,10 +47,10 @@ app.get('/', async (req, res) => { let contests = await Contest.query([1, 5], { is_public: true }, [['start_time', 'desc']]); - let problems = (await Problem.query([1, 5], { is_public: true }, [['last_update', 'desc']])).map(problem => ({ + let problems = (await Problem.query([1, 5], { is_public: true }, [['publicize_time', 'desc']])).map(problem => ({ id: problem.id, title: problem.title, - time: timeAgo.format(new Date(problem.last_update)), + time: timeAgo.format(new Date(problem.publicize_time)), })); res.render('index', { diff --git a/modules/problem.js b/modules/problem.js index f30da2d..dcc990d 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -581,6 +581,7 @@ async function setPublic(req, res, is_public) { problem.is_public = is_public; problem.publicizer_id = res.locals.user.id; + problem.publicize_time = new Date(); await problem.save(); JudgeState.model.update( From 3db1af18be8f26a71132ebfb618b05e973bf5d61 Mon Sep 17 00:00:00 2001 From: hewenyang Date: Sat, 14 Apr 2018 17:10:34 +0000 Subject: [PATCH 3/4] Minor word change --- views/index.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/index.ejs b/views/index.ejs index e80c8cd..33c1a7d 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -92,7 +92,7 @@ -

最新更新

+

最近更新

From 2dee15a357dd4cd237501bc45d63ffd1e036e578 Mon Sep 17 00:00:00 2001 From: hewenyang Date: Sun, 15 Apr 2018 03:54:49 +0000 Subject: [PATCH 4/4] Allow sort by publicize time --- modules/problem.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/problem.js b/modules/problem.js index dcc990d..4d2dc3c 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -35,7 +35,7 @@ app.get('/problems', async (req, res) => { try { const sort = req.query.sort || syzoj.config.sorting.problem.field; const order = req.query.order || syzoj.config.sorting.problem.order; - if (!['id', 'title', 'rating', 'ac_num', 'submit_num', 'ac_rate'].includes(sort) || !['asc', 'desc'].includes(order)) { + if (!['id', 'title', 'rating', 'ac_num', 'submit_num', 'ac_rate', 'publicize_time'].includes(sort) || !['asc', 'desc'].includes(order)) { throw new ErrorMessage('错误的排序参数。'); }