From e876d8f6ed362b93a12e806bc22c62e3f24eab91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sun, 20 Dec 2015 13:34:35 +0100 Subject: [PATCH] Display relative timestamps in conversation list This mimicks Signal-Android's relative timestamps. Previously, only the date was displayed. Fixes #284 --- background.html | 2 +- js/views/conversation_list_item_view.js | 5 +- js/views/message_view.js | 2 +- js/views/timestamp_view.js | 78 ++++++++++++++++++++----- test/index.html | 1 + test/views/timestamp_view_test.js | 72 +++++++++++++++++++++++ 6 files changed, 142 insertions(+), 18 deletions(-) create mode 100644 test/views/timestamp_view_test.js diff --git a/background.html b/background.html index bc86e1ad..2eddd549 100644 --- a/background.html +++ b/background.html @@ -143,7 +143,7 @@ + diff --git a/test/views/timestamp_view_test.js b/test/views/timestamp_view_test.js new file mode 100644 index 00000000..6d471208 --- /dev/null +++ b/test/views/timestamp_view_test.js @@ -0,0 +1,72 @@ +/* + * vim: ts=4:sw=4:expandtab + */ +'use strict'; + +describe('TimestampView', function() { + it('formats long-ago timestamps correctly', function() { + var timestamp = Date.now(); + var brief_view = new Whisper.BriefTimestampView().render(), + ext_view = new Whisper.ExtendedTimestampView().render(); + + // Helper functions to check absolute and relative timestamps + + // Helper to check an absolute TS for an exact match + var check = function(view, ts, expected) { + var result = view.getRelativeTimeSpanString(ts); + assert.strictEqual(result, expected); + }; + + // Helper to check relative times for an exact match against both views + var checkDiff = function(sec_ago, expected_brief, expected_ext) { + check(brief_view, timestamp - sec_ago * 1000, expected_brief); + check(ext_view, timestamp - sec_ago * 1000, expected_ext); + }; + + // Helper to check an absolute TS for an exact match against both views + var checkAbs = function(ts, expected_brief, expected_ext) { + if (!expected_ext) expected_ext = expected_brief; + check(brief_view, ts, expected_brief); + check(ext_view, ts, expected_ext); + }; + + // Helper to check an absolute TS for a match at the beginning against + var checkStartsWith = function(view, ts, expected) { + var result = view.getRelativeTimeSpanString(ts); + var regexp = new RegExp("^" + expected); + assert.match(result, regexp); + }; + + // check integer timestamp, JS Date object and moment object + checkAbs(timestamp, 'now', 'a few seconds ago'); + checkAbs(new Date(), 'now', 'a few seconds ago'); + checkAbs(moment(), 'now', 'a few seconds ago'); + + // check recent timestamps + checkDiff(30, 'now', 'a few seconds ago'); // 30 seconds + checkDiff(50, '1 min', 'a minute ago'); // >= 45 seconds => 1 minute + checkDiff(40*60, '40 min', '40 minutes ago'); + checkDiff(60*60, '1 hour', 'an hour ago'); + checkDiff(125*60, '2 hours', '2 hours ago'); + + // set to third of month to avoid problems on the 29th/30th/31st + var last_month = moment().subtract(1, 'month').date(3), + months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + day_of_month = new Date().getDate(); + check(brief_view,last_month, months[last_month.month()] + ' 3'); + checkStartsWith(ext_view,last_month, months[last_month.month()] + ' 3'); + + // subtract 26 hours to be safe in case of DST stuff + var yesterday = new Date(timestamp - 26*60*60*1000), + days_of_week = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; + check(brief_view, yesterday, days_of_week[yesterday.getDay()]); + checkStartsWith(ext_view, yesterday, days_of_week[yesterday.getDay()]); + + // Check something long ago + // months are zero-indexed in JS for some reason + check(brief_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012'); + checkStartsWith(ext_view, new Date(2012, 4, 5, 17, 30, 0), 'May 5, 2012'); + }); + +});