Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
presence: Switch web app to modern single-user presence API format.
Tim Abbottba79f0178975
diff --git a/web/src/presence.ts b/web/src/presence.tsindex 1e9117fd6e3..f5101fb0ae8 100644--- a/web/src/presence.ts+++ b/web/src/presence.ts@@ -29,14 +29,11 @@ export type PresenceInfoFromEvent = z.output<typeof presence_info_from_event_schexport const user_last_seen_response_schema = z.object({result: z.string(),msg: z.optional(z.string()),+ server_timestamp: z.optional(z.number()),presence: z.optional(z.object({- /* We ignore the keys other than aggregated, since they just contain- duplicate data. */- aggregated: z.object({- status: z.enum(["active", "idle", "offline"]),- timestamp: z.number(),- }),+ active_timestamp: z.optional(z.number()),+ idle_timestamp: z.optional(z.number()),}),),});diff --git a/web/src/user_card_popover.ts b/web/src/user_card_popover.tsindex 26e973d51c7..325d24c7ab7 100644--- a/web/src/user_card_popover.ts+++ b/web/src/user_card_popover.ts@@ -267,12 +267,17 @@ export let fetch_presence_for_popover = (user_id: number): void => {const response = parsed_data.data;- if (response.result === "success" && response.presence) {- const {aggregated} = response.presence;- presence.presence_info.set(user_id, {- status: aggregated.status,- last_active: aggregated.timestamp,- });+ if (+ response.result === "success" &&+ response.presence &&+ response.server_timestamp !== undefined+ ) {+ const raw = {+ ...response.presence,+ server_timestamp: response.server_timestamp,+ };+ const user = people.get_by_user_id(user_id);+ presence.presence_info.set(user_id, presence.status_from_raw(raw, user));// Update the user's last seen time in the user card// popover once we have their presence information, ifdiff --git a/web/tests/presence.test.cjs b/web/tests/presence.test.cjsindex ab6a0501415..a63384f5df5 100644--- a/web/tests/presence.test.cjs+++ b/web/tests/presence.test.cjs@@ -370,3 +370,39 @@ test("update_info_from_event", () => {last_active: 1000,});});++test("user_last_seen_response_schema", () => {+ // Modern format with both timestamps.+ const with_both_timestamps = {+ result: "success",+ msg: "",+ server_timestamp: 1656958540,+ presence: {+ active_timestamp: 1656958520,+ idle_timestamp: 1656958530,+ },+ };+ const parsed_both = presence.user_last_seen_response_schema.safeParse(with_both_timestamps);+ assert.ok(parsed_both.success);+ assert.equal(parsed_both.data.server_timestamp, 1656958540);+ assert.equal(parsed_both.data.presence?.active_timestamp, 1656958520);+ assert.equal(parsed_both.data.presence?.idle_timestamp, 1656958530);++ // Only active_timestamp present; idle_timestamp may be omitted+ // if the user has only ever been active.+ const with_active_only = {+ result: "success",+ msg: "",+ presence: {active_timestamp: 1656958520},+ };+ const parsed_active = presence.user_last_seen_response_schema.safeParse(with_active_only);+ assert.ok(parsed_active.success);+ assert.equal(parsed_active.data.presence?.active_timestamp, 1656958520);+ assert.equal(parsed_active.data.presence?.idle_timestamp, undefined);++ // No presence data.+ const without_presence = {result: "success", msg: ""};+ const parsed_none = presence.user_last_seen_response_schema.safeParse(without_presence);+ assert.ok(parsed_none.success);+ assert.equal(parsed_none.data.presence, undefined);+});