From adcda98f30b253a2f3563455ecfd5e4c0702e071 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Tue, 29 Mar 2016 22:23:11 +0900 Subject: [PATCH] Prevent humanTime helper to generate future times Fixes #592. --- framework/core/js/admin/dist/app.js | 8 ++++++++ framework/core/js/forum/dist/app.js | 8 ++++++++ framework/core/js/lib/utils/humanTime.js | 10 +++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/framework/core/js/admin/dist/app.js b/framework/core/js/admin/dist/app.js index 54dc5b052..0bef0c2f7 100644 --- a/framework/core/js/admin/dist/app.js +++ b/framework/core/js/admin/dist/app.js @@ -22447,6 +22447,14 @@ System.register('flarum/utils/formatNumber', [], function (_export, _context) { System.register('flarum/utils/humanTime', [], function (_export, _context) { function humanTime(time) { var m = moment(time); + var now = moment(); + + // To prevent showing things like "in a few seconds" due to small offsets + // between client and server time, we always reset future dates to the + // current time. This will result in "just now" being shown instead. + if (m.isAfter(now)) { + m = now; + } var day = 864e5; var diff = m.diff(moment()); diff --git a/framework/core/js/forum/dist/app.js b/framework/core/js/forum/dist/app.js index a9546bb3a..4bc4b4549 100644 --- a/framework/core/js/forum/dist/app.js +++ b/framework/core/js/forum/dist/app.js @@ -30555,6 +30555,14 @@ System.register('flarum/utils/History', [], function (_export, _context) { System.register('flarum/utils/humanTime', [], function (_export, _context) { function humanTime(time) { var m = moment(time); + var now = moment(); + + // To prevent showing things like "in a few seconds" due to small offsets + // between client and server time, we always reset future dates to the + // current time. This will result in "just now" being shown instead. + if (m.isAfter(now)) { + m = now; + } var day = 864e5; var diff = m.diff(moment()); diff --git a/framework/core/js/lib/utils/humanTime.js b/framework/core/js/lib/utils/humanTime.js index 279453594..f77d7d3b2 100644 --- a/framework/core/js/lib/utils/humanTime.js +++ b/framework/core/js/lib/utils/humanTime.js @@ -6,7 +6,15 @@ * @return {String} */ export default function humanTime(time) { - const m = moment(time); + let m = moment(time); + const now = moment(); + + // To prevent showing things like "in a few seconds" due to small offsets + // between client and server time, we always reset future dates to the + // current time. This will result in "just now" being shown instead. + if (m.isAfter(now)) { + m = now; + } const day = 864e5; const diff = m.diff(moment());