From 4d68579806cdd06f262767ed7ecf7855dedf73b8 Mon Sep 17 00:00:00 2001 From: hewenyang Date: Sat, 14 Apr 2018 16:11:38 +0000 Subject: [PATCH] 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;