From 2dddf8d8b9d4e9f2d23e46940dfb963a2ade05be Mon Sep 17 00:00:00 2001 From: vinitS101 Date: Thu, 28 Feb 2019 20:39:03 +0530 Subject: [PATCH] refactor: Move user_last_seen_time_status() to buddy_data. This is a pure data function, so it shouldn't be in popovers.js file (Steve Howell added test coverage here, and tabbott removed an accidental functional change.) --- frontend_tests/node_tests/buddy_data.js | 37 +++++++++++++++++++++++++ static/js/buddy_data.js | 24 ++++++++++++++++ static/js/popovers.js | 30 ++------------------ 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/frontend_tests/node_tests/buddy_data.js b/frontend_tests/node_tests/buddy_data.js index 3f676b8633..4c22fbbd7a 100644 --- a/frontend_tests/node_tests/buddy_data.js +++ b/frontend_tests/node_tests/buddy_data.js @@ -7,6 +7,7 @@ zrequire('presence'); zrequire('util'); zrequire('user_status'); zrequire('buddy_data'); +set_global('timerender', {}); // The buddy_data module is mostly tested indirectly through // activity.js, but we should feel free to add direct tests @@ -24,6 +25,12 @@ const me = { email: 'self@example.com', }; +const old_user = { + user_id: 9999, + full_name: 'Old User', + email: 'old_user@example.com', +}; + const bot = { user_id: 55555, full_name: 'Red Herring Bot', @@ -44,6 +51,7 @@ function make_people() { people.add_in_realm(bot); people.add_in_realm(selma); people.add_in_realm(me); + people.add_in_realm(old_user); people.initialize_current_user(me.user_id); } @@ -188,3 +196,32 @@ run_test('level', () => { assert.equal(buddy_data.level(me.user_id), 0); assert.equal(buddy_data.level(selma.user_id), 3); }); + +run_test('user_last_seen_time_status', () => { + assert.equal(buddy_data.user_last_seen_time_status(selma.user_id), + 'translated: Active now'); + + page_params.realm_is_zephyr_mirror_realm = true; + assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id), + 'translated: Unknown'); + page_params.realm_is_zephyr_mirror_realm = false; + assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id), + 'translated: More than 2 weeks ago'); + + presence.last_active_date = (user_id) => { + assert.equal(user_id, old_user.user_id); + + return { + clone: () => 'date-stub', + }; + }; + + timerender.last_seen_status_from_date = (date) => { + assert.equal(date, 'date-stub'); + return 'May 12'; + }; + + assert.equal(buddy_data.user_last_seen_time_status(old_user.user_id), + 'May 12'); + +}); diff --git a/static/js/buddy_data.js b/static/js/buddy_data.js index 8f713bdf3d..04acc6de9c 100644 --- a/static/js/buddy_data.js +++ b/static/js/buddy_data.js @@ -150,6 +150,30 @@ exports.my_user_status = function (user_id) { return i18n.t('(you)'); }; +exports.user_last_seen_time_status = function (user_id) { + var status = presence.get_status(user_id); + if (status === "active") { + return i18n.t("Active now"); + } + + if (page_params.realm_is_zephyr_mirror_realm) { + // We don't send presence data to clients in Zephyr mirroring realms + return i18n.t("Unknown"); + } + + // There are situations where the client has incomplete presence + // history on a user. This can happen when users are deactivated, + // or when they just haven't been present in a long time (and we + // may have queries on presence that go back only N weeks). + // + // We give the somewhat vague status of "Unknown" for these users. + var last_active_date = presence.last_active_date(user_id); + if (last_active_date === undefined) { + return i18n.t("More than 2 weeks ago"); + } + return timerender.last_seen_status_from_date(last_active_date.clone()); +}; + exports.user_title = function (user_id) { var buddy_status = exports.buddy_status(user_id); var type_desc = presence_descriptions[buddy_status]; diff --git a/static/js/popovers.js b/static/js/popovers.js index 7f2553e1ef..4eedcaa770 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -77,30 +77,6 @@ function load_medium_avatar(user, elt) { }); } -function user_last_seen_time_status(user_id) { - var status = presence.get_status(user_id); - if (status === "active") { - return i18n.t("Active now"); - } - - if (page_params.realm_is_zephyr_mirror_realm) { - // We don't send presence data to clients in Zephyr mirroring realms - return i18n.t("Unknown"); - } - - // There are situations where the client has incomplete presence - // history on a user. This can happen when users are deactivated, - // or when they just haven't been present in a long time (and we - // may have queries on presence that go back only N weeks). - // - // We give the somewhat vague status of "Unknown" for these users. - var last_active_date = presence.last_active_date(user_id); - if (last_active_date === undefined) { - return i18n.t("More than 2 weeks ago"); - } - return timerender.last_seen_status_from_date(last_active_date.clone()); -} - function calculate_info_popover_placement(size, elt) { var ypos = elt.offset().top; @@ -144,7 +120,7 @@ function render_user_info_popover(user, popover_element, is_sender_popover, priv user_email: user.email, user_full_name: user.full_name, user_id: user.user_id, - user_last_seen_time_status: user_last_seen_time_status(user.user_id), + user_last_seen_time_status: buddy_data.user_last_seen_time_status(user.user_id), user_time: people.get_user_time(user.user_id), user_type: people.get_user_type(user.user_id), status_text: user_status.get_status_text(user.user_id), @@ -293,7 +269,7 @@ exports.show_user_profile = function (user) { user_avatar: "avatar/" + user.email + "/medium", is_me: people.is_current_user(user.email), date_joined: moment(user.date_joined).format(localFormat), - last_seen: user_last_seen_time_status(user.user_id), + last_seen: buddy_data.user_last_seen_time_status(user.user_id), user_time: people.get_user_time(user.user_id), user_type: people.get_user_type(user.user_id), user_is_guest: user.is_guest, @@ -333,7 +309,7 @@ function fetch_group_members(member_ids) { return Object.assign({}, p, { user_circle_class: buddy_data.get_user_circle_class(p.user_id), is_active: people.is_active_user_for_popover(p.user_id), - user_last_seen_time_status: user_last_seen_time_status(p.user_id), + user_last_seen_time_status: buddy_data.user_last_seen_time_status(p.user_id), }); }); }