Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

presence: Use the narrow user cache.

Alex Vandiverf58c29b29083
diff --git a/zerver/decorator.py b/zerver/decorator.py
index 2e34cbced82..0fc1b7e5f28 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -550,7 +550,9 @@ def human_users_only(
request: HttpRequest, /, *args: ParamT.args, **kwargs: ParamT.kwargs
) -> HttpResponse:
assert request.user.is_authenticated
- if request.user.is_bot:
+ # Check bot_type here, rather than is_bot, because the
+ # narrow user cache only has that (nullable) type
+ if request.user.bot_type is not None:
raise JsonableError(_("This endpoint does not accept bot requests."))
return view_func(request, *args, **kwargs)
@@ -681,7 +683,9 @@ def require_member_or_admin(
) -> HttpResponse:
if user_profile.is_guest:
raise JsonableError(_("Not allowed for guest users"))
- if user_profile.is_bot:
+ # Check bot_type here, rather than is_bot, because the
+ # narrow user cache only has that (nullable) type
+ if user_profile.bot_type is not None:
raise JsonableError(_("This endpoint does not accept bot requests."))
return view_func(request, user_profile, *args, **kwargs)
diff --git a/zerver/models/users.py b/zerver/models/users.py
index 3bac3e823e7..960c2d360ca 100644
--- a/zerver/models/users.py
+++ b/zerver/models/users.py
@@ -964,6 +964,7 @@ def get_user_profile_narrow_by_id(user_profile_id: int) -> UserProfile:
"id",
"bot_type",
"is_active",
+ "presence_enabled",
"rate_limits",
"role",
"recipient_id",
diff --git a/zerver/tests/test_presence.py b/zerver/tests/test_presence.py
index 5fab91acdba..1122d7cdd96 100644
--- a/zerver/tests/test_presence.py
+++ b/zerver/tests/test_presence.py
@@ -679,6 +679,31 @@ class UserPresenceTests(ZulipTestCase):
result_dict = self.assert_json_success(result)
self.assertEqual(set(result_dict["presences"].keys()), {othello.email})
+ def test_query_counts(self) -> None:
+ self.login("hamlet")
+ with self.assert_database_query_count(6):
+ # 1. session
+ # 2. narrow user cache
+ # 3. client
+ # 4. lock the userpresence row
+ # 5. update the userpresence row
+ # 6. select other userpresence data
+ self.assert_json_success(
+ self.client_post("/json/users/me/presence", {"status": "active"})
+ )
+
+ with self.assert_database_query_count(3, keep_cache_warm=True):
+ # With a warm cache, we skip the first three queries
+ self.assert_json_success(
+ self.client_post("/json/users/me/presence", {"status": "active"})
+ )
+
+ with self.assert_database_query_count(3, keep_cache_warm=True):
+ # It's the same story if we're becoming idle, as well
+ self.assert_json_success(
+ self.client_post("/json/users/me/presence", {"status": "idle"})
+ )
+
class SingleUserPresenceTests(ZulipTestCase):
def test_email_access(self) -> None:
diff --git a/zerver/views/presence.py b/zerver/views/presence.py
index e92b7c6db41..be39ef2bedb 100644
--- a/zerver/views/presence.py
+++ b/zerver/views/presence.py
@@ -40,7 +40,11 @@ def get_presence_backend(
except UserProfile.DoesNotExist:
raise JsonableError(_("No such user"))
- if target.is_bot:
+ # Check bot_type here, rather than is_bot, because that matches
+ # authenticated_json_view's check of .is_incoming_webhook; the
+ # narrow user cache can hence be optimized to only have the
+ # nullable bot_type, as long as this check matches.
+ if target.bot_type is not None:
raise JsonableError(_("Presence is not supported for bot users."))
if settings.CAN_ACCESS_ALL_USERS_GROUP_LIMITS_PRESENCE and not check_can_access_user(
diff --git a/zproject/urls.py b/zproject/urls.py
index 5f304ea8761..0031f1eb0a5 100644
--- a/zproject/urls.py
+++ b/zproject/urls.py
@@ -416,7 +416,9 @@ v1_api_and_json_patterns = [
rest_path("users/me/android_gcm_reg_id", POST=add_android_reg_id, DELETE=remove_android_reg_id),
rest_path("mobile_push/test_notification", POST=send_test_push_notification_api),
# users/*/presence => zerver.views.presence.
- rest_path("users/me/presence", POST=update_active_status_backend),
+ rest_path(
+ "users/me/presence", POST=(update_active_status_backend, {"narrow_user_session_cache"})
+ ),
# It's important that this sit after users/me/presence so that
# Django's URL resolution order doesn't break the
# /users/me/presence endpoint.