Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
presence_data: Fix last_active_time for users with no last_active_time.
Maneesh Shukla606d1e96912a
diff --git a/web/src/presence.ts b/web/src/presence.tsindex e256712b014..179fb0be6b2 100644--- a/web/src/presence.ts+++ b/web/src/presence.ts@@ -75,7 +75,7 @@ export function get_active_or_idle_user_ids(): number[] {.map((entry) => entry[0]);}-export function status_from_raw(raw: RawPresence): PresenceStatus {+export function status_from_raw(raw: RawPresence, user_id: number): PresenceStatus {/*Example of `raw`:@@ -116,7 +116,9 @@ export function status_from_raw(raw: RawPresence): PresenceStatus {}if (age(idle_timestamp) < offline_threshold_secs) {- last_active = idle_timestamp;+ // idle_timestamp >= active_timestamp usually, but it's+ // harmless to just take the maximum for readability.+ last_active = Math.max(active_timestamp ?? 0, idle_timestamp ?? 0);return {status: "idle",last_active,@@ -124,14 +126,19 @@ export function status_from_raw(raw: RawPresence): PresenceStatus {}/*- We always want to prioritize the last time the user- was active 'active_timestamp' to be displayed in the- popover. This since it is the most relevant information- for other users and matches the formatting of the string- in the popover.+ We always want to prioritize the last time the user was active+ 'active_timestamp' to be displayed in the popover. This since+ it is the most relevant information for other users and+ matches the formatting of the string in the popover. For users+ who've never logged in, we fall back to when they joined.*/- last_active = active_timestamp ?? idle_timestamp;+ const user = people.get_by_user_id(user_id);+ let date_joined_timestamp = 0;+ if (user?.date_joined) {+ date_joined_timestamp = new Date(user.date_joined).getTime() / 1000;+ }+ last_active = Math.max(active_timestamp ?? 0, date_joined_timestamp);return {status: "offline",@@ -182,7 +189,7 @@ export function update_info_from_event(raw_info.set(user_id, raw);- const status = status_from_raw(raw);+ const status = status_from_raw(raw, user_id);presence_info.set(user_id, status);}@@ -249,7 +256,7 @@ export function set_info(raw_info.set(user_id, raw);- const status = status_from_raw(raw);+ const status = status_from_raw(raw, user_id);presence_info.set(user_id, status);}for (const user_id of all_active_or_idle_user_ids) {diff --git a/web/tests/presence.test.cjs b/web/tests/presence.test.cjsindex 3ad3aff97fb..8ba68bb3450 100644--- a/web/tests/presence.test.cjs+++ b/web/tests/presence.test.cjs@@ -62,6 +62,8 @@ const john = {email: "[email protected]",user_id: 8,full_name: "John Doe",+ // Second 77.+ date_joined: "1970-01-01 00:01:15 UTC",};const jane = {@@ -113,7 +115,7 @@ test("status_from_raw", () => {active_timestamp: now - OFFLINE_THRESHOLD_SECS / 2,};- assert.deepEqual(status_from_raw(raw), {+ assert.deepEqual(status_from_raw(raw, alice.user_id), {status: "active",last_active: raw.active_timestamp,});@@ -123,7 +125,7 @@ test("status_from_raw", () => {active_timestamp: now - OFFLINE_THRESHOLD_SECS * 2,};- assert.deepEqual(status_from_raw(raw), {+ assert.deepEqual(status_from_raw(raw, alice.user_id), {status: "offline",last_active: raw.active_timestamp,});@@ -133,10 +135,23 @@ test("status_from_raw", () => {idle_timestamp: now - OFFLINE_THRESHOLD_SECS / 2,};- assert.deepEqual(status_from_raw(raw), {+ assert.deepEqual(status_from_raw(raw, alice.user_id), {status: "idle",last_active: raw.idle_timestamp,});++ const user = people.get_by_user_id(alice.user_id);+ user.date_joined = new Date((now - OFFLINE_THRESHOLD_SECS * 200) * 1000);++ raw = {+ server_timestamp: now,+ active_timestamp: now - OFFLINE_THRESHOLD_SECS * 200,+ idle_timestamp: now - OFFLINE_THRESHOLD_SECS * 100,+ };+ assert.deepEqual(status_from_raw(raw, alice.user_id), {+ status: "offline",+ last_active: raw.active_timestamp,+ });});test("set_presence_info", () => {@@ -217,7 +232,8 @@ test("set_presence_info", () => {assert.deepEqual(presence.presence_info.get(john.user_id), {status: "offline",- last_active: a_while_ago,+ // Fall back to date_joined, which we set to 75 seconds after the epoch above.+ last_active: 75,});assert.equal(presence.get_status(john.user_id), "offline");@@ -261,7 +277,9 @@ test("missing values", () => {assert.deepEqual(presence.presence_info.get(zoe.user_id), {status: "offline",- last_active: undefined,+ // This shouldn't happen in reality, but covers not crashing+ // if we don't have a last_active_time.+ last_active: 0,});});