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.

user_topics: Update move-topic codepath to utilise bulk database operations.

Prakhar Pratyush117264fb3775
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py
index 04efcaf350b..3378356c932 100644
--- a/zerver/actions/message_edit.py
+++ b/zerver/actions/message_edit.py
@@ -1,4 +1,5 @@
import datetime
+from collections import defaultdict
from typing import Any, Dict, Iterable, List, Optional, Set
from django.conf import settings
@@ -19,7 +20,7 @@ from zerver.actions.message_send import (
render_incoming_message,
)
from zerver.actions.uploads import check_attachment_reference_change
-from zerver.actions.user_topics import do_set_user_topic_visibility_policy
+from zerver.actions.user_topics import bulk_do_set_user_topic_visibility_policy
from zerver.lib.exceptions import JsonableError
from zerver.lib.markdown import MessageRenderingResult, topic_links
from zerver.lib.markdown import version as markdown_version
@@ -766,41 +767,46 @@ def do_update_message(
assert stream_being_edited is not None
assert topic_name is not None or new_stream is not None
+ user_profiles_for_visibility_policy: Dict[int, List[UserProfile]] = defaultdict(list)
+ stream_inaccessible_to_user_profiles: List[UserProfile] = []
for user_topic in get_users_with_user_topic_visibility_policy(
stream_being_edited.id, orig_topic_name
):
- # TODO: Ideally, this would be a bulk update operation,
- # because we are doing database operations in a loop here.
- #
- # This loop is only acceptable in production because it is
- # rare for more than a few users to have visibility_policy
- # set for an individual topic that is being moved; as of this
- # writing, no individual topic in Zulip Cloud had been
- # muted by more than 100 users.
-
if (
new_stream is not None
and user_topic.user_profile_id in delete_event_notify_user_ids
):
- # If the messages are being moved to a stream the user
- # cannot access, then we treat this as the
- # messages/topic being deleted for this user. This is
- # important for security reasons; we don't want to
- # give users a UserTopic row in a stream they cannot
- # access. Remove the user topic rows for such users.
- do_set_user_topic_visibility_policy(
- user_topic.user_profile,
- stream_being_edited,
- orig_topic_name,
- visibility_policy=UserTopic.VisibilityPolicy.INHERIT,
- )
+ stream_inaccessible_to_user_profiles.append(user_topic.user_profile)
else:
- # Otherwise, we move the user topic record for the
- # user, but removing the old topic visibility_policy
- # and then creating a new one.
- new_visibility_policy = user_topic.visibility_policy
- do_set_user_topic_visibility_policy(
- user_topic.user_profile,
+ user_profiles_for_visibility_policy[user_topic.visibility_policy].append(
+ user_topic.user_profile
+ )
+
+ # If the messages are being moved to a stream the user
+ # cannot access, then we treat this as the
+ # messages/topic being deleted for this user. This is
+ # important for security reasons; we don't want to
+ # give users a UserTopic row in a stream they cannot
+ # access. Remove the user topic rows for such users.
+ bulk_do_set_user_topic_visibility_policy(
+ stream_inaccessible_to_user_profiles,
+ stream_being_edited,
+ orig_topic_name,
+ visibility_policy=UserTopic.VisibilityPolicy.INHERIT,
+ )
+
+ # If the messages are being moved to a stream the user
+ # can access. We move the user topic records for such
+ # users, but removing the old topic visibility_policy
+ # and then creating a new one.
+ for visibility_policy in UserTopic.VisibilityPolicy.values:
+ # Using the dict, we get the user_profiles lists for the original_topic
+ # that corresponds to each visibility_policy.
+ # Perform the remove and create operation for each of
+ # these user_profiles lists.
+ if visibility_policy in user_profiles_for_visibility_policy:
+ bulk_do_set_user_topic_visibility_policy(
+ user_profiles_for_visibility_policy[visibility_policy],
stream_being_edited,
orig_topic_name,
visibility_policy=UserTopic.VisibilityPolicy.INHERIT,
@@ -811,11 +817,11 @@ def do_update_message(
skip_muted_topics_event=True,
)
- do_set_user_topic_visibility_policy(
- user_topic.user_profile,
+ bulk_do_set_user_topic_visibility_policy(
+ user_profiles_for_visibility_policy[visibility_policy],
new_stream if new_stream is not None else stream_being_edited,
topic_name if topic_name is not None else orig_topic_name,
- visibility_policy=new_visibility_policy,
+ visibility_policy=visibility_policy,
)
send_event(user_profile.realm, event, users_to_be_notified)
diff --git a/zerver/tests/test_message_edit.py b/zerver/tests/test_message_edit.py
index 272c2ebf21b..93350b18335 100644
--- a/zerver/tests/test_message_edit.py
+++ b/zerver/tests/test_message_edit.py
@@ -1357,9 +1357,9 @@ class EditMessageTest(EditMessageTestCase):
users_to_be_notified = list(map(notify, [hamlet.id, cordelia.id, aaron.id]))
change_all_topic_name = "Topic 1 edited"
- # This code path adds 21 (10 + 5/user with muted topics + 1) to
+ # This code path adds 17 (10 + 4/visibility_policy + 1/user + 1) to
# the number of database queries for moving a topic.
- with self.assert_database_query_count(21):
+ with self.assert_database_query_count(17):
check_update_message(
user_profile=hamlet,
message_id=message_id,
@@ -1453,7 +1453,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
- with self.assert_database_query_count(32):
+ with self.assert_database_query_count(28):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -1517,7 +1517,7 @@ class EditMessageTest(EditMessageTestCase):
set_topic_visibility_policy(desdemona, muted_topics, UserTopic.VisibilityPolicy.MUTED)
set_topic_visibility_policy(cordelia, muted_topics, UserTopic.VisibilityPolicy.MUTED)
- with self.assert_database_query_count(32):
+ with self.assert_database_query_count(28):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -1567,6 +1567,7 @@ class EditMessageTest(EditMessageTestCase):
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")
aaron = self.example_user("aaron")
+ othello = self.example_user("othello")
def assert_has_visibility_policy(
user_profile: UserProfile,
@@ -1598,24 +1599,32 @@ class EditMessageTest(EditMessageTestCase):
self.login_user(cordelia)
self.subscribe(aaron, stream_name)
self.login_user(aaron)
+ self.subscribe(othello, stream_name)
+ self.login_user(othello)
- # Initially, hamlet sets visibility_policy as UNMUTED for 'Topic1' and 'Topic2',
+ # Initially, hamlet and othello set visibility_policy as UNMUTED for 'Topic1' and 'Topic2',
# cordelia sets visibility_policy as MUTED for 'Topic1' and 'Topic2', while
# aaron doesn't have a visibility_policy set for 'Topic1' or 'Topic2'.
#
# After moving messages from 'Topic1' to 'Topic 1 edited', the expected behaviour is:
- # hamlet has UNMUTED 'Topic 1 edited' and no visibility_policy set for 'Topic1'
+ # hamlet and othello have UNMUTED 'Topic 1 edited' and no visibility_policy set for 'Topic1'
# cordelia has MUTED 'Topic 1 edited' and no visibility_policy set for 'Topic1'
#
# There is no change in visibility_policy configurations for 'Topic2', i.e.
- # hamlet has UNMUTED 'Topic2' + cordelia has MUTED 'Topic2'
+ # hamlet and othello have UNMUTED 'Topic2' + cordelia has MUTED 'Topic2'
# aaron still doesn't have visibility_policy set for any topic.
+ #
+ # Note: We have used two users with UNMUTED 'Topic1' to verify that the query count
+ # doesn't increase (in order to update UserTopic records) with an increase in users.
+ # (We are using bulk database operations.)
+ # 1 query/user is added in order to send muted_topics event.(which will be deprecated)
topics = [
[stream_name, "Topic1"],
[stream_name, "Topic2"],
]
set_topic_visibility_policy(hamlet, topics, UserTopic.VisibilityPolicy.UNMUTED)
set_topic_visibility_policy(cordelia, topics, UserTopic.VisibilityPolicy.MUTED)
+ set_topic_visibility_policy(othello, topics, UserTopic.VisibilityPolicy.UNMUTED)
# users that need to be notified by send_event in the case of change-topic-name operation.
users_to_be_notified_via_muted_topics_event: List[int] = []
@@ -1630,7 +1639,7 @@ class EditMessageTest(EditMessageTestCase):
users_to_be_notified_via_muted_topics_event.append(user_topic.user_profile_id)
change_all_topic_name = "Topic 1 edited"
- with self.assert_database_query_count(21):
+ with self.assert_database_query_count(22):
check_update_message(
user_profile=hamlet,
message_id=message_id,
@@ -1661,30 +1670,42 @@ class EditMessageTest(EditMessageTestCase):
sorted(users_to_be_notified_via_user_topic_event),
)
+ # No visibility_policy set for 'Topic1'
assert_has_visibility_policy(
hamlet, "Topic1", UserTopic.VisibilityPolicy.UNMUTED, expected=False
)
assert_has_visibility_policy(
cordelia, "Topic1", UserTopic.VisibilityPolicy.MUTED, expected=False
)
+ assert_has_visibility_policy(
+ othello, "Topic1", UserTopic.VisibilityPolicy.UNMUTED, expected=False
+ )
assert_has_visibility_policy(
aaron, "Topic1", UserTopic.VisibilityPolicy.UNMUTED, expected=False
)
+ # No change in visibility_policy configurations for 'Topic2'
assert_has_visibility_policy(
hamlet, "Topic2", UserTopic.VisibilityPolicy.UNMUTED, expected=True
)
assert_has_visibility_policy(
cordelia, "Topic2", UserTopic.VisibilityPolicy.MUTED, expected=True
)
+ assert_has_visibility_policy(
+ othello, "Topic2", UserTopic.VisibilityPolicy.UNMUTED, expected=True
+ )
assert_has_visibility_policy(
aaron, "Topic2", UserTopic.VisibilityPolicy.UNMUTED, expected=False
)
+ # UserTopic records moved to 'Topic 1 edited' after move-topic operation.
assert_has_visibility_policy(
hamlet, change_all_topic_name, UserTopic.VisibilityPolicy.UNMUTED, expected=True
)
assert_has_visibility_policy(
cordelia, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED, expected=True
)
+ assert_has_visibility_policy(
+ othello, change_all_topic_name, UserTopic.VisibilityPolicy.UNMUTED, expected=True
+ )
assert_has_visibility_policy(
aaron, change_all_topic_name, UserTopic.VisibilityPolicy.MUTED, expected=False
)