Add function to get last seen status from last active date.

This commit is contained in:
hackerkid 2017-05-12 23:46:39 +05:30 committed by showell
parent 1ba7d1ec68
commit a158676fb0
3 changed files with 94 additions and 0 deletions

View File

@ -13,6 +13,14 @@ set_global('document', {
}, },
}); });
set_global('i18n', {
t: function (string) {
return string;
},
});
set_global('XDate', require("xdate"));
add_dependencies({ add_dependencies({
Handlebars: 'handlebars', Handlebars: 'handlebars',
templates: 'js/templates', templates: 'js/templates',
@ -25,6 +33,7 @@ add_dependencies({
narrow: 'js/narrow.js', narrow: 'js/narrow.js',
presence: 'js/presence.js', presence: 'js/presence.js',
activity: 'js/activity.js', activity: 'js/activity.js',
timerender: 'js/timerender.js',
}); });
var presence = global.presence; var presence = global.presence;

View File

@ -187,3 +187,55 @@ var timerender = require('js/timerender.js');
var actual = attrs.get('title'); var actual = attrs.get('title');
assert.equal(expected, actual); assert.equal(expected, actual);
}()); }());
(function test_last_seen_status_from_date() {
// Set base_dateto to March 1 2016 12.30 AM (months are zero based)
var base_date = new XDate(2016, 2, 1, 0, 30);
function assert_same(modifier, expected_status) {
var past_date = base_date.clone();
past_date = modifier(past_date);
var actual_status = timerender.last_seen_status_from_date(past_date, base_date);
assert.equal(actual_status, expected_status);
}
assert_same(function (d) { return d.addSeconds(-20); },
i18n.t("Last seen just now"));
assert_same(function (d) { return d.addMinutes(-1); },
i18n.t("Last seen just now"));
assert_same(function (d) { return d.addMinutes(-2); },
i18n.t("Last seen just now"));
assert_same(function (d) { return d.addMinutes(-30); },
i18n.t("Last seen 30 minutes ago"));
assert_same(function (d) { return d.addHours(-1); },
i18n.t("Last seen an hour ago"));
assert_same(function (d) { return d.addHours(-2); },
i18n.t("Last seen 2 hours ago"));
assert_same(function (d) { return d.addHours(-20); },
i18n.t("Last seen 20 hours ago"));
assert_same(function (d) { return d.addDays(-1); },
i18n.t("Last seen yesterday"));
assert_same(function (d) { return d.addDays(-2); },
i18n.t("Last seen on Feb 28"));
assert_same(function (d) { return d.addDays(-61); },
i18n.t("Last seen on Dec 31"));
assert_same(function (d) { return d.addDays(-300); },
i18n.t("Last seen on May 06"));
assert_same(function (d) { return d.addDays(-366); },
i18n.t("Last seen on Mar 01, 2015"));
assert_same(function (d) { return d.addYears(-3); },
i18n.t("Last seen on Mar 01, 2013"));
}());

View File

@ -62,6 +62,39 @@ exports.render_now = function (time, today) {
}; };
}; };
// Current date is passed as an argument for unit testing
exports.last_seen_status_from_date = function (last_active_date, current_date) {
if (typeof current_date === 'undefined') {
current_date = new XDate();
}
var minutes = Math.floor(last_active_date.diffMinutes(current_date));
if (minutes <= 2) {
return i18n.t("Last seen just now");
}
if (minutes < 60) {
return i18n.t("Last seen " + minutes +" minutes ago");
}
var hours = Math.floor(minutes / 60);
if (hours === 1) {
return i18n.t("Last seen an hour ago");
}
if (hours < 24) {
return i18n.t("Last seen " + hours + " hours ago");
}
var days = Math.floor(hours / 24);
if (days === 1) {
return [i18n.t("Last seen yesterday")];
}
if (days < 365) {
return i18n.t("Last seen on " + last_active_date.toString("MMM\xa0dd"));
}
return i18n.t("Last seen on " + last_active_date.toString("MMM\xa0dd,\xa0yyyy"));
};
// List of the dates that need to be updated when the day changes. // List of the dates that need to be updated when the day changes.
// Each timestamp is represented as a list of length 2: // Each timestamp is represented as a list of length 2:
// [id of the span element, XDate representing the time] // [id of the span element, XDate representing the time]