mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
This commit renames the 'queue_json_publish' function to 'queue_json_publish_rollback_unsafe' to reflect the fact that it doesn't wait for the db transaction (within which it gets called, if any) to commit and sends event irrespective of commit or rollback. In most of the cases we don't want to send event in the case of rollbacks, so the caller should be aware that calling the function directly is rollback unsafe. Fixes part of #30489.
35 lines
1.5 KiB
Python
35 lines
1.5 KiB
Python
from datetime import datetime
|
|
|
|
from zerver.lib.queue import queue_json_publish_rollback_unsafe
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
|
from zerver.models import UserActivityInterval, UserProfile
|
|
|
|
|
|
def do_update_user_activity_interval(user_profile: UserProfile, log_time: datetime) -> None:
|
|
effective_end = log_time + UserActivityInterval.MIN_INTERVAL_LENGTH
|
|
# This code isn't perfect, because with various races we might end
|
|
# up creating two overlapping intervals, but that shouldn't happen
|
|
# often, and can be corrected for in post-processing
|
|
try:
|
|
last = UserActivityInterval.objects.filter(user_profile=user_profile).order_by("-end")[0]
|
|
# Two intervals overlap iff each interval ends after the other
|
|
# begins. In this case, we just extend the old interval to
|
|
# include the new interval.
|
|
if log_time <= last.end and effective_end >= last.start:
|
|
last.end = max(last.end, effective_end)
|
|
last.start = min(last.start, log_time)
|
|
last.save(update_fields=["start", "end"])
|
|
return
|
|
except IndexError:
|
|
pass
|
|
|
|
# Otherwise, the intervals don't overlap, so we should make a new one
|
|
UserActivityInterval.objects.create(
|
|
user_profile=user_profile, start=log_time, end=effective_end
|
|
)
|
|
|
|
|
|
def update_user_activity_interval(user_profile: UserProfile, log_time: datetime) -> None:
|
|
event = {"user_profile_id": user_profile.id, "time": datetime_to_timestamp(log_time)}
|
|
queue_json_publish_rollback_unsafe("user_activity_interval", event)
|