timerender: Revert extra changes to time in recent conversations.

In #25012, which was intended to only modify the buddy list, we
accidentally changed the rendering of times for several other code
paths, including recent conversations, that were using the
last_status_from_time function, whose name really suggested it only
was used for the buddy list.

Extract a new function with a better name for the more common relative
time use case.
This commit is contained in:
Joseph Ho
2023-04-07 20:13:36 -04:00
committed by Tim Abbott
parent 85029d604a
commit 1e98a659c1
5 changed files with 107 additions and 5 deletions

View File

@@ -363,7 +363,7 @@ function format_conversation(conversation_data) {
context.full_last_msg_date_time = timerender.get_full_datetime(time);
context.conversation_key = get_key_from_message(last_msg);
context.unread_count = message_to_conversation_unread_count(last_msg);
context.last_msg_time = timerender.last_seen_status_from_date(time);
context.last_msg_time = timerender.relative_time_string_from_date(time);
context.is_private = last_msg.type === "private";
let all_senders;
let senders;

View File

@@ -46,13 +46,13 @@ export function populate_exports_table(exports) {
let deleted_timestamp = data.deleted_timestamp;
if (failed_timestamp !== null) {
failed_timestamp = timerender.last_seen_status_from_date(
failed_timestamp = timerender.relative_time_string_from_date(
new Date(failed_timestamp * 1000),
);
}
if (deleted_timestamp !== null) {
deleted_timestamp = timerender.last_seen_status_from_date(
deleted_timestamp = timerender.relative_time_string_from_date(
new Date(deleted_timestamp * 1000),
);
}
@@ -62,7 +62,7 @@ export function populate_exports_table(exports) {
id: data.id,
acting_user: people.get_full_name(data.acting_user_id),
// Convert seconds -> milliseconds
event_time: timerender.last_seen_status_from_date(
event_time: timerender.relative_time_string_from_date(
new Date(data.export_time * 1000),
),
url: data.export_url,

View File

@@ -187,6 +187,53 @@ export function render_now(time: Date, today = new Date()): TimeRender {
};
}
// Relative time rendering for use in most screens like Recent conversations.
//
// Current date is passed as an argument for unit testing
export function relative_time_string_from_date(
last_active_date: Date,
current_date = new Date(),
): string {
const minutes = differenceInMinutes(current_date, last_active_date);
if (minutes <= 2) {
return $t({defaultMessage: "Just now"});
}
if (minutes < 60) {
return $t({defaultMessage: "{minutes} minutes ago"}, {minutes});
}
const days_old = differenceInCalendarDays(current_date, last_active_date);
const hours = Math.floor(minutes / 60);
if (hours < 24) {
if (hours === 1) {
return $t({defaultMessage: "An hour ago"});
}
return $t({defaultMessage: "{hours} hours ago"}, {hours});
}
if (days_old === 1) {
return $t({defaultMessage: "Yesterday"});
}
if (days_old < 90) {
return $t({defaultMessage: "{days_old} days ago"}, {days_old});
} else if (
days_old > 90 &&
days_old < 365 &&
last_active_date.getFullYear() === current_date.getFullYear()
) {
// Online more than 90 days ago, in the same year
return get_localized_date_or_time_for_format(last_active_date, "dayofyear");
}
return get_localized_date_or_time_for_format(last_active_date, "dayofyear_year");
}
// Relative time logic variant use in the buddy list, where every
// string has "Active" init. This is hard to deduplicate with
// relative_time_string_from_date because of complexities involved in i18n and
// word order.
//
// Current date is passed as an argument for unit testing
export function last_seen_status_from_date(
last_active_date: Date,

View File

@@ -136,7 +136,7 @@ mock_esm("../src/stream_list", {
handle_narrow_deactivated: noop,
});
mock_esm("../src/timerender", {
last_seen_status_from_date: () => "Just now",
relative_time_string_from_date: () => "Just now",
get_full_datetime: () => "date at time",
});
mock_esm("../src/sub_store", {

View File

@@ -518,6 +518,61 @@ run_test("last_seen_status_from_date", () => {
assert_same({hours: -24}, $t({defaultMessage: "Active yesterday"}));
});
run_test("relative_time_string_from_date", () => {
// Set base_date to March 1 2016 12.30 AM (months are zero based)
let base_date = new Date(2016, 2, 1, 0, 30);
function assert_same(duration, expected_status) {
const past_date = add(base_date, duration);
const actual_status = timerender.relative_time_string_from_date(past_date, base_date);
assert.equal(actual_status, expected_status);
}
assert_same({seconds: -20}, $t({defaultMessage: "Just now"}));
assert_same({minutes: -1}, $t({defaultMessage: "Just now"}));
assert_same({minutes: -2}, $t({defaultMessage: "Just now"}));
assert_same({minutes: -30}, $t({defaultMessage: "30 minutes ago"}));
assert_same({hours: -1}, $t({defaultMessage: "An hour ago"}));
assert_same({hours: -2}, $t({defaultMessage: "2 hours ago"}));
assert_same({hours: -20}, $t({defaultMessage: "20 hours ago"}));
assert_same({hours: -24}, $t({defaultMessage: "Yesterday"}));
assert_same({hours: -48}, $t({defaultMessage: "2 days ago"}));
assert_same({days: -2}, $t({defaultMessage: "2 days ago"}));
assert_same({days: -61}, $t({defaultMessage: "61 days ago"}));
assert_same({days: -300}, "May 6, 2015");
assert_same({days: -366}, "Mar 1, 2015");
assert_same({years: -3}, "Mar 1, 2013");
// Set base_date to May 1 2016 12.30 AM (months are zero based)
base_date = new Date(2016, 4, 1, 0, 30);
assert_same({days: -91}, "Jan 31");
// Set base_date to May 1 2016 10.30 PM (months are zero based)
base_date = new Date(2016, 4, 2, 23, 30);
assert_same({hours: -1}, $t({defaultMessage: "An hour ago"}));
assert_same({hours: -2}, $t({defaultMessage: "2 hours ago"}));
assert_same({hours: -12}, $t({defaultMessage: "12 hours ago"}));
assert_same({hours: -24}, $t({defaultMessage: "Yesterday"}));
});
run_test("set_full_datetime", () => {
let time = date_2019;