presence: Use server-provided params in the webapp.

As of the previous commit, the server provides these parameters in
page_params. The defaults match the values hard-coded in the webapp so
far - so get rid of the hard-coded values in favor of taking them from
page_params.
This commit is contained in:
Mateusz Mandera 2023-02-21 11:52:30 +01:00 committed by Tim Abbott
parent a593089770
commit 8ef889f392
6 changed files with 16 additions and 12 deletions

View File

@ -577,6 +577,8 @@ test("update_presence_info", ({override}) => {
override(pm_list, "update_private_messages", () => {});
page_params.realm_presence_disabled = false;
page_params.server_presence_ping_interval_seconds = 50;
page_params.server_presence_offline_threshold_seconds = 140;
const server_time = 500;
const info = {

View File

@ -422,6 +422,8 @@ test("always show me", () => {
});
test("level", () => {
page_params.server_presence_offline_threshold_seconds = 140;
add_canned_users();
assert.equal(buddy_data.level(me.user_id), 0);
assert.equal(buddy_data.level(selma.user_id), 3);

View File

@ -5,7 +5,7 @@ const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const {user_settings} = require("../zjsunit/zpage_params");
const {page_params, user_settings} = require("../zjsunit/zpage_params");
const reload_state = mock_esm("../../static/js/reload_state", {
is_in_progress: () => false,
@ -78,6 +78,7 @@ people.initialize_current_user(me.user_id);
function test(label, f) {
run_test(label, ({override}) => {
page_params.server_presence_offline_threshold_seconds = OFFLINE_THRESHOLD_SECS;
user_settings.presence_enabled = true;
presence.clear_internal_data();
f({override});

View File

@ -27,8 +27,6 @@ export let user_filter;
/* Broadcast "idle" to server after 5 minutes of local inactivity */
const DEFAULT_IDLE_TIMEOUT_MS = 5 * 60 * 1000;
/* Time between keep-alive pings */
const ACTIVE_PING_INTERVAL_MS = 50 * 1000;
/* Keep in sync with views.py:update_active_status_backend() */
export const ACTIVE = "active";
@ -246,7 +244,9 @@ export function initialize() {
send_presence_to_server(true);
}
util.call_function_periodically(get_full_presence_list_update, ACTIVE_PING_INTERVAL_MS);
/* Time between keep-alive pings */
const active_ping_interval_ms = page_params.server_presence_ping_interval_seconds * 1000;
util.call_function_periodically(get_full_presence_list_update, active_ping_interval_ms);
// Let the server know we're here, but pass "false" for
// want_redraw, since we just got all this info in page_params.

View File

@ -1,4 +1,5 @@
import * as blueslip from "./blueslip";
import {page_params} from "./page_params";
import * as people from "./people";
import * as reload_state from "./reload_state";
import {user_settings} from "./user_settings";
@ -22,11 +23,6 @@ export function clear_internal_data() {
presence_info.clear();
}
/* Mark users as offline after 140 seconds since their last checkin,
* Keep in sync with zerver/tornado/event_queue.py:receiver_is_idle
*/
const OFFLINE_THRESHOLD_SECS = 140;
const BIG_REALM_COUNT = 250;
export function get_status(user_id) {
@ -58,6 +54,10 @@ export function status_from_raw(raw) {
server_timestamp: 1585745140
}
*/
/* Mark users as offline after this many seconds since their last checkin, */
const offline_threshold_secs = page_params.server_presence_offline_threshold_seconds;
function age(timestamp) {
return raw.server_timestamp - (timestamp || 0);
}
@ -79,14 +79,14 @@ export function status_from_raw(raw) {
show the user as active (even if there's a newer
timestamp for idle).
*/
if (age(active_timestamp) < OFFLINE_THRESHOLD_SECS) {
if (age(active_timestamp) < offline_threshold_secs) {
return {
status: "active",
last_active,
};
}
if (age(idle_timestamp) < OFFLINE_THRESHOLD_SECS) {
if (age(idle_timestamp) < offline_threshold_secs) {
return {
status: "idle",
last_active,

View File

@ -488,7 +488,6 @@ LOG_API_EVENT_TYPES = False
STAGING = False
# How long to wait before presence should treat a user as offline.
# Should match the presence.js constant.
OFFLINE_THRESHOLD_SECS = 140
# How often a client should ping by asking for presence data of all users.