Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

timerender: Revert extra changes to time in recent conversations.

Joseph Ho1e98a659c16f
diff --git a/web/src/recent_topics_ui.js b/web/src/recent_topics_ui.js
index a47c7860156..f569f00fab5 100644
--- a/web/src/recent_topics_ui.js
+++ b/web/src/recent_topics_ui.js
@@ -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;
diff --git a/web/src/settings_exports.js b/web/src/settings_exports.js
index 612a9f997be..0d64f7fec07 100644
--- a/web/src/settings_exports.js
+++ b/web/src/settings_exports.js
@@ -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,
diff --git a/web/src/timerender.ts b/web/src/timerender.ts
index 03367cfd2a6..020d0e137f6 100644
--- a/web/src/timerender.ts
+++ b/web/src/timerender.ts
@@ -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,
diff --git a/web/tests/recent_topics.test.js b/web/tests/recent_topics.test.js
index 188614c90a9..6868704286d 100644
--- a/web/tests/recent_topics.test.js
+++ b/web/tests/recent_topics.test.js
@@ -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", {
diff --git a/web/tests/timerender.test.js b/web/tests/timerender.test.js
index 31050ed185c..3284abed412 100644
--- a/web/tests/timerender.test.js
+++ b/web/tests/timerender.test.js
@@ -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;