2022-04-14 23:44:33 +02:00
|
|
|
import time
|
2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime, timedelta
|
2022-04-14 23:44:33 +02:00
|
|
|
|
|
|
|
from django.conf import settings
|
2022-08-24 21:42:44 +02:00
|
|
|
from django.db import transaction
|
2022-04-14 23:44:33 +02:00
|
|
|
|
|
|
|
from zerver.actions.user_activity import update_user_activity_interval
|
2023-04-08 15:52:48 +02:00
|
|
|
from zerver.lib.presence import (
|
|
|
|
format_legacy_presence_dict,
|
|
|
|
user_presence_datetime_with_date_joined_default,
|
|
|
|
)
|
2022-04-14 23:44:33 +02:00
|
|
|
from zerver.lib.queue import queue_json_publish
|
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2023-10-17 12:56:39 +02:00
|
|
|
from zerver.lib.users import get_user_ids_who_can_access_user
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models import Client, UserPresence, UserProfile, get_client
|
|
|
|
from zerver.models.users import active_user_ids
|
2022-04-14 23:44:33 +02:00
|
|
|
from zerver.tornado.django_api import send_event
|
|
|
|
|
|
|
|
|
2022-08-24 21:42:44 +02:00
|
|
|
def send_presence_changed(
|
|
|
|
user_profile: UserProfile, presence: UserPresence, *, force_send_update: bool = False
|
|
|
|
) -> None:
|
2022-04-14 23:44:33 +02:00
|
|
|
# Most presence data is sent to clients in the main presence
|
|
|
|
# endpoint in response to the user's own presence; this results
|
|
|
|
# data that is 1-2 minutes stale for who is online. The flaw with
|
|
|
|
# this plan is when a user comes back online and then immediately
|
|
|
|
# sends a message, recipients may still see that user as offline!
|
|
|
|
# We solve that by sending an immediate presence update clients.
|
|
|
|
#
|
|
|
|
# See https://zulip.readthedocs.io/en/latest/subsystems/presence.html for
|
|
|
|
# internals documentation on presence.
|
2023-10-17 12:56:39 +02:00
|
|
|
if settings.CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE:
|
|
|
|
user_ids = get_user_ids_who_can_access_user(user_profile)
|
|
|
|
else:
|
|
|
|
user_ids = active_user_ids(user_profile.realm_id)
|
|
|
|
|
2022-08-24 21:42:44 +02:00
|
|
|
if (
|
|
|
|
len(user_ids) > settings.USER_LIMIT_FOR_SENDING_PRESENCE_UPDATE_EVENTS
|
|
|
|
and not force_send_update
|
|
|
|
):
|
2022-04-14 23:44:33 +02:00
|
|
|
# These immediate presence generate quadratic work for Tornado
|
|
|
|
# (linear number of users in each event and the frequency of
|
|
|
|
# users coming online grows linearly with userbase too). In
|
|
|
|
# organizations with thousands of users, this can overload
|
|
|
|
# Tornado, especially if much of the realm comes online at the
|
|
|
|
# same time.
|
|
|
|
#
|
|
|
|
# The utility of these live-presence updates goes down as
|
|
|
|
# organizations get bigger (since one is much less likely to
|
|
|
|
# be paying attention to the sidebar); so beyond a limit, we
|
|
|
|
# stop sending them at all.
|
|
|
|
return
|
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
last_active_time = user_presence_datetime_with_date_joined_default(
|
|
|
|
presence.last_active_time, user_profile.date_joined
|
|
|
|
)
|
|
|
|
last_connected_time = user_presence_datetime_with_date_joined_default(
|
|
|
|
presence.last_connected_time, user_profile.date_joined
|
|
|
|
)
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
# The mobile app handles these events so we need to use the old format.
|
|
|
|
# The format of the event should also account for the slim_presence
|
|
|
|
# API parameter when this becomes possible in the future.
|
2023-04-08 15:52:48 +02:00
|
|
|
presence_dict = format_legacy_presence_dict(last_active_time, last_connected_time)
|
2022-04-14 23:44:33 +02:00
|
|
|
event = dict(
|
|
|
|
type="presence",
|
|
|
|
email=user_profile.email,
|
|
|
|
user_id=user_profile.id,
|
|
|
|
server_timestamp=time.time(),
|
|
|
|
presence={presence_dict["client"]: presence_dict},
|
|
|
|
)
|
|
|
|
send_event(user_profile.realm, event, user_ids)
|
|
|
|
|
|
|
|
|
|
|
|
def consolidate_client(client: Client) -> Client:
|
|
|
|
# The web app reports a client as 'website'
|
|
|
|
# The desktop app reports a client as ZulipDesktop
|
|
|
|
# due to it setting a custom user agent. We want both
|
|
|
|
# to count as web users
|
|
|
|
|
|
|
|
# Alias ZulipDesktop to website
|
|
|
|
if client.name in ["ZulipDesktop"]:
|
|
|
|
return get_client("website")
|
|
|
|
else:
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def do_update_user_presence(
|
2022-08-24 21:42:44 +02:00
|
|
|
user_profile: UserProfile,
|
|
|
|
client: Client,
|
2023-11-19 19:45:19 +01:00
|
|
|
log_time: datetime,
|
2022-08-24 21:42:44 +02:00
|
|
|
status: int,
|
|
|
|
*,
|
|
|
|
force_send_update: bool = False,
|
2022-04-14 23:44:33 +02:00
|
|
|
) -> None:
|
|
|
|
client = consolidate_client(client)
|
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
# If the user doesn't have a UserPresence row yet, we create one with
|
|
|
|
# sensible defaults. If we're getting a presence update, clearly the user
|
|
|
|
# at least connected, so last_connected_time should be set. last_active_time
|
|
|
|
# will depend on whether the status sent is idle or active.
|
2022-04-14 23:44:33 +02:00
|
|
|
defaults = dict(
|
2023-04-08 15:52:48 +02:00
|
|
|
last_active_time=None,
|
2020-06-11 16:03:47 +02:00
|
|
|
last_connected_time=log_time,
|
2022-04-14 23:44:33 +02:00
|
|
|
realm_id=user_profile.realm_id,
|
|
|
|
)
|
2023-04-08 15:52:48 +02:00
|
|
|
if status == UserPresence.LEGACY_STATUS_ACTIVE_INT:
|
|
|
|
defaults["last_active_time"] = log_time
|
2022-04-14 23:44:33 +02:00
|
|
|
|
|
|
|
(presence, created) = UserPresence.objects.get_or_create(
|
|
|
|
user_profile=user_profile,
|
|
|
|
defaults=defaults,
|
|
|
|
)
|
|
|
|
|
2023-04-08 15:52:48 +02:00
|
|
|
if presence.last_active_time is not None:
|
|
|
|
time_since_last_active = log_time - presence.last_active_time
|
|
|
|
else:
|
|
|
|
# The user was never active, so let's consider this large to go over any thresholds
|
|
|
|
# we may have.
|
2023-11-19 19:45:19 +01:00
|
|
|
time_since_last_active = timedelta(days=1)
|
2023-04-08 15:52:48 +02:00
|
|
|
if presence.last_connected_time is not None:
|
|
|
|
time_since_last_connected = log_time - presence.last_connected_time
|
|
|
|
else:
|
|
|
|
# Same approach as above.
|
2023-11-19 19:45:19 +01:00
|
|
|
time_since_last_connected = timedelta(days=1)
|
2022-04-14 23:44:33 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
assert (3 * settings.PRESENCE_PING_INTERVAL_SECS + 20) <= settings.OFFLINE_THRESHOLD_SECS
|
2023-11-19 19:45:19 +01:00
|
|
|
now_online = time_since_last_active > timedelta(
|
2020-06-11 16:03:47 +02:00
|
|
|
# Here, we decide whether the user is newly online, and we need to consider
|
|
|
|
# sending an immediate presence update via the events system that this user is now online,
|
|
|
|
# rather than waiting for other clients to poll the presence update.
|
|
|
|
# Sending these presence update events adds load to the system, so we only want to do this
|
|
|
|
# if the user has missed a couple regular presence checkins
|
|
|
|
# (so their state is at least 2 * PRESENCE_PING_INTERVAL_SECS + 10 old),
|
|
|
|
# and also is under the risk of being shown by clients as offline before the next regular presence checkin
|
|
|
|
# (so at least `settings.OFFLINE_THRESHOLD_SECS - settings.PRESENCE_PING_INTERVAL_SECS - 10`).
|
|
|
|
# These two values happen to be the same in the default configuration.
|
|
|
|
seconds=settings.OFFLINE_THRESHOLD_SECS
|
|
|
|
- settings.PRESENCE_PING_INTERVAL_SECS
|
|
|
|
- 10
|
|
|
|
)
|
|
|
|
became_online = status == UserPresence.LEGACY_STATUS_ACTIVE_INT and now_online
|
|
|
|
|
|
|
|
update_fields = []
|
|
|
|
|
|
|
|
# This check is to prevent updating `last_connected_time` several
|
|
|
|
# times per minute with multiple connected browser windows.
|
|
|
|
# We also need to be careful not to wrongly "update" the timestamp if we actually already
|
|
|
|
# have newer presence than the reported log_time.
|
2023-11-19 19:45:19 +01:00
|
|
|
if not created and time_since_last_connected > timedelta(
|
2020-06-11 16:03:47 +02:00
|
|
|
seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS
|
|
|
|
):
|
|
|
|
presence.last_connected_time = log_time
|
|
|
|
update_fields.append("last_connected_time")
|
|
|
|
if (
|
|
|
|
not created
|
|
|
|
and status == UserPresence.LEGACY_STATUS_ACTIVE_INT
|
2023-11-19 19:45:19 +01:00
|
|
|
and time_since_last_active > timedelta(seconds=settings.PRESENCE_UPDATE_MIN_FREQ_SECONDS)
|
2020-06-11 16:03:47 +02:00
|
|
|
):
|
|
|
|
presence.last_active_time = log_time
|
|
|
|
update_fields.append("last_active_time")
|
2023-04-08 15:52:48 +02:00
|
|
|
if presence.last_connected_time is None or log_time > presence.last_connected_time:
|
2020-06-11 16:03:47 +02:00
|
|
|
# Update last_connected_time as well to ensure
|
|
|
|
# last_connected_time >= last_active_time.
|
|
|
|
presence.last_connected_time = log_time
|
|
|
|
update_fields.append("last_connected_time")
|
|
|
|
if len(update_fields) > 0:
|
2022-04-14 23:44:33 +02:00
|
|
|
presence.save(update_fields=update_fields)
|
|
|
|
|
2022-08-24 21:42:44 +02:00
|
|
|
if force_send_update or (
|
|
|
|
not user_profile.realm.presence_disabled and (created or became_online)
|
|
|
|
):
|
|
|
|
# We do a the transaction.on_commit here, rather than inside
|
|
|
|
# send_presence_changed, to help keep presence transactions
|
|
|
|
# brief; the active_user_ids call there is more expensive than
|
|
|
|
# this whole function.
|
|
|
|
transaction.on_commit(
|
|
|
|
lambda: send_presence_changed(
|
|
|
|
user_profile, presence, force_send_update=force_send_update
|
|
|
|
)
|
|
|
|
)
|
2022-04-14 23:44:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def update_user_presence(
|
|
|
|
user_profile: UserProfile,
|
|
|
|
client: Client,
|
2023-11-19 19:45:19 +01:00
|
|
|
log_time: datetime,
|
2022-04-14 23:44:33 +02:00
|
|
|
status: int,
|
|
|
|
new_user_input: bool,
|
|
|
|
) -> None:
|
|
|
|
event = {
|
|
|
|
"user_profile_id": user_profile.id,
|
|
|
|
"status": status,
|
|
|
|
"time": datetime_to_timestamp(log_time),
|
|
|
|
"client": client.name,
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_json_publish("user_presence", event)
|
|
|
|
|
|
|
|
if new_user_input:
|
|
|
|
update_user_activity_interval(user_profile, log_time)
|