← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~cjohnston/launchpad/ic-approx-time into lp:launchpad

 

Chris Johnston has proposed merging lp:~cjohnston/launchpad/ic-approx-time into lp:launchpad.

Commit message:
Switch ICs to use relative time stamps similar to other comment sections of LP.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjohnston/launchpad/ic-approx-time/+merge/225420
-- 
https://code.launchpad.net/~cjohnston/launchpad/ic-approx-time/+merge/225420
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~cjohnston/launchpad/ic-approx-time into lp:launchpad.
=== modified file 'lib/lp/code/javascript/branchmergeproposal.inlinecomments.js'
--- lib/lp/code/javascript/branchmergeproposal.inlinecomments.js	2014-05-29 16:30:42 +0000
+++ lib/lp/code/javascript/branchmergeproposal.inlinecomments.js	2014-07-03 03:01:45 +0000
@@ -127,6 +127,37 @@
         'tr[id^="diff-line"]');
 };
 
+
+namespace.approximatedate = function(date) {
+    // Display approximate time an event happened when it happened less than 1
+    // day ago.
+    var now = Date.now();
+    var timedelta = now - Date.parse(date);
+    days = timedelta / 86400000
+    hours = timedelta / 3600000
+    minutes = timedelta / 60000
+    if (timedelta > '86400000') {
+        return 'on ' + Y.Date.format(new Date(date), {format: '%Y-%m-%d'});
+    } else {
+        if (days >= 1) {
+            amount = days
+            unit = "day"
+        } else if (hours >= 1) {
+            amount = hours
+            unit = "hour"
+        } else if (minutes >= 1) {
+            amount = minutes
+            unit = "minute"
+        } else {
+            return "a moment ago"
+        }
+        if (parseInt(amount) > 1) {
+            unit = unit + 's'
+        }
+        return parseInt(amount) + ' ' + unit + ' ago'
+    }
+};
+
 namespace.create_row = function(comment) {
     // Find or create the container for this line's comments.
     var comments_tr = Y.one('#comments-diff-line-' + comment.line_number);
@@ -184,9 +215,8 @@
         // XXX cprov 20140226: the date should be formatted according to
         // the user locale/timezone similar to fmt:displaydate.
         // We should leverage Y.Intl features.
-        var fmt_date = 'on ' + Y.Date.format(
-            new Date(comment.date), {format: '%Y-%m-%d'});
-        headerspan.one('span').set('text', fmt_date);
+        var date = namespace.approximatedate(comment.date);
+        headerspan.one('span').set('text', date);
         newrow.one('.boardCommentDetails').append(headerspan);
         newrow.one('.boardCommentBody div').append('<span></span>');
         newrow.one('.boardCommentBody').one('span').set('text', comment.text);

=== modified file 'lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js'
--- lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js	2014-05-26 12:44:57 +0000
+++ lib/lp/code/javascript/tests/test_branchmergeproposal.inlinecomments.js	2014-07-03 03:01:45 +0000
@@ -70,25 +70,44 @@
                 "ws.op=getInlineComments&previewdiff_id=1",
                 mockio.requests[0].config.data);
             mockio.last_request = mockio.requests[0];
+            var now = Date.now()
             published_comments = [
                 {'line_number': '2',
                  'person': person_obj,
                  'text': 'This is preloaded.',
-                 'date': '2012-08-12T10:00:00.00001+00:00'}
+                 'date': '2012-08-12T10:00:00.00001+00:00'},
+                {'line_number': '3',
+                 'person': person_obj,
+                 'text': 'This is good stuff.',
+                 'date': new Date(now - 150).toISOString(), 
+                },
+                {'line_number': '3',
+                 'person': person_obj,
+                 'text': 'This is bad stuff.',
+                 'date': new Date(now - 65000).toISOString(), 
+                },
+                {'line_number': '3',
+                 'person': person_obj,
+                 'text': 'This is great.',
+                 'date': new Date(now - 12600000).toISOString(), 
+                },
             ];
             mockio.success({
                 responseText: Y.JSON.stringify(published_comments),
                 responseHeaders: {'Content-Type': 'application/json'}});
 
             // Published comment is displayed.
-            var comments = Y.one('#diff-line-2').next().one('div');
+            var first_comments = Y.one('#diff-line-2').next().one('div');
             // XXX cprov 20140226: test disabled due to ES4 lack of
             // ISO8601 support. Although the vast majority of production
             // clients run ES5.
             //Y.Assert.areEqual(
             //    'Foo Bar (name16) wrote on 2012-08-12',
             //    header.get('text'));
-            var first = comments.one('div:first-child')
+            var first = first_comments.one('div:first-child')
+            Y.Assert.areEqual(
+                'Foo Bar (name16) wrote on 2012-08-12:',
+                first.one('.boardCommentDetails').get('text'));
             Y.Assert.areEqual(
                 'This is preloaded.',
                 first.one('.boardCommentBody').get('text'));
@@ -103,12 +122,26 @@
                 'Boing!', second.one('.boardCommentBody').get('text'));
 
             // Draft comment for line 3 is displayed.
-            var third = Y.one('#diff-line-3').next();
+            var second_comments = Y.one('#diff-line-3').next().one('div');
+            var third = second_comments.one('div:first-child')
+            console.log(third);
             Y.Assert.areEqual(
-                'Unsaved comment',
+                'Foo Bar (name16) wrote a moment ago:',
                 third.one('.boardCommentDetails').get('text'));
-            Y.Assert.areEqual(
-                'Zoing!', third.one('.boardCommentBody').get('text'));
+            var fourth = third.next()
+            Y.Assert.areEqual(
+                'Foo Bar (name16) wrote 1 minute ago:',
+                fourth.one('.boardCommentDetails').get('text'));
+            var fifth = fourth.next()
+            Y.Assert.areEqual(
+                'Foo Bar (name16) wrote 3 hours ago:',
+                fifth.one('.boardCommentDetails').get('text'));
+            var sixth = fifth.next()
+            Y.Assert.areEqual(
+                'Unsaved comment',
+                sixth.one('.boardCommentDetails').get('text'));
+            Y.Assert.areEqual(
+                'Zoing!', sixth.one('.boardCommentBody').get('text'));
         },
 
         test_draft_handler: function() {


Follow ups