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.)
This commit is contained in:
vinitS101 2019-02-28 20:39:03 +05:30 committed by Tim Abbott
parent 0dcfc22406
commit 2dddf8d8b9
3 changed files with 64 additions and 27 deletions

View File

@ -7,6 +7,7 @@ zrequire('presence');
zrequire('util'); zrequire('util');
zrequire('user_status'); zrequire('user_status');
zrequire('buddy_data'); zrequire('buddy_data');
set_global('timerender', {});
// The buddy_data module is mostly tested indirectly through // The buddy_data module is mostly tested indirectly through
// activity.js, but we should feel free to add direct tests // activity.js, but we should feel free to add direct tests
@ -24,6 +25,12 @@ const me = {
email: 'self@example.com', email: 'self@example.com',
}; };
const old_user = {
user_id: 9999,
full_name: 'Old User',
email: 'old_user@example.com',
};
const bot = { const bot = {
user_id: 55555, user_id: 55555,
full_name: 'Red Herring Bot', full_name: 'Red Herring Bot',
@ -44,6 +51,7 @@ function make_people() {
people.add_in_realm(bot); people.add_in_realm(bot);
people.add_in_realm(selma); people.add_in_realm(selma);
people.add_in_realm(me); people.add_in_realm(me);
people.add_in_realm(old_user);
people.initialize_current_user(me.user_id); 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(me.user_id), 0);
assert.equal(buddy_data.level(selma.user_id), 3); 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');
});

View File

@ -150,6 +150,30 @@ exports.my_user_status = function (user_id) {
return i18n.t('(you)'); 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) { exports.user_title = function (user_id) {
var buddy_status = exports.buddy_status(user_id); var buddy_status = exports.buddy_status(user_id);
var type_desc = presence_descriptions[buddy_status]; var type_desc = presence_descriptions[buddy_status];

View File

@ -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) { function calculate_info_popover_placement(size, elt) {
var ypos = elt.offset().top; 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_email: user.email,
user_full_name: user.full_name, user_full_name: user.full_name,
user_id: user.user_id, 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_time: people.get_user_time(user.user_id),
user_type: people.get_user_type(user.user_id), user_type: people.get_user_type(user.user_id),
status_text: user_status.get_status_text(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", user_avatar: "avatar/" + user.email + "/medium",
is_me: people.is_current_user(user.email), is_me: people.is_current_user(user.email),
date_joined: moment(user.date_joined).format(localFormat), 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_time: people.get_user_time(user.user_id),
user_type: people.get_user_type(user.user_id), user_type: people.get_user_type(user.user_id),
user_is_guest: user.is_guest, user_is_guest: user.is_guest,
@ -333,7 +309,7 @@ function fetch_group_members(member_ids) {
return Object.assign({}, p, { return Object.assign({}, p, {
user_circle_class: buddy_data.get_user_circle_class(p.user_id), user_circle_class: buddy_data.get_user_circle_class(p.user_id),
is_active: people.is_active_user_for_popover(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),
}); });
}); });
} }