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.

message_edit: Migrate UserTopic rows only on content access.

Sahil Batrada0b0d7c8c82
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.py
index 443e1b40246..37b0fea5157 100644
--- a/zerver/actions/message_edit.py
+++ b/zerver/actions/message_edit.py
@@ -8,7 +8,7 @@ from typing import Any
from django.conf import settings
from django.db import transaction
-from django.db.models import Q, QuerySet
+from django.db.models import Exists, OuterRef, Q, QuerySet
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
@@ -63,6 +63,7 @@ from zerver.lib.streams import (
check_stream_access_based_on_can_send_message_group,
get_stream_topics_policy,
notify_stream_is_recently_active_update,
+ user_has_content_access,
)
from zerver.lib.string_validation import check_stream_topic
from zerver.lib.thumbnail import manifest_and_get_user_upload_previews, rewrite_thumbnailed_images
@@ -83,7 +84,10 @@ from zerver.lib.topic import (
from zerver.lib.topic_link_util import get_stream_topic_link_syntax
from zerver.lib.types import DirectMessageEditRequest, EditHistoryEvent, StreamMessageEditRequest
from zerver.lib.url_encoding import stream_message_url
-from zerver.lib.user_groups import UserGroupMembershipDetails
+from zerver.lib.user_groups import (
+ UserGroupMembershipDetails,
+ get_user_id_annotated_recursive_membership_groups_for_users,
+)
from zerver.lib.user_message import bulk_insert_all_ums
from zerver.lib.user_topics import get_users_with_user_topic_visibility_policy
from zerver.lib.widget import is_widget_message
@@ -629,21 +633,56 @@ def update_user_topic_visibility_policies_on_move(
target_stream: Stream,
target_topic_name: str,
target_topic_has_messages: bool,
- users_losing_access: Iterable[UserProfile],
) -> dict[UserProfile, int]:
stream_inaccessible_to_user_profiles: list[UserProfile] = []
orig_topic_user_profile_to_visibility_policy: dict[UserProfile, int] = {}
target_topic_user_profile_to_visibility_policy: dict[UserProfile, int] = {}
- user_ids_losing_access = {user.id for user in users_losing_access}
- for user_topic in get_users_with_user_topic_visibility_policy(
- stream_being_edited.id, orig_topic_name
- ):
- if is_stream_edited and user_topic.user_profile_id in user_ids_losing_access:
- stream_inaccessible_to_user_profiles.append(user_topic.user_profile)
+ # We annotate whether each user is subscribed to the target
+ # channel, so user_has_content_access below needs no extra
+ # per-user or subscriber query.
+ assert target_stream.recipient_id is not None
+ orig_topic_user_topics = list(
+ get_users_with_user_topic_visibility_policy(
+ stream_being_edited.id, orig_topic_name
+ ).annotate(
+ is_target_stream_subscriber=Exists(
+ Subscription.objects.filter(
+ recipient_id=target_stream.recipient_id,
+ user_profile_id=OuterRef("user_profile_id"),
+ active=True,
+ )
+ )
+ )
+ )
+
+ recursive_group_ids_by_user: dict[int, set[int]] = {}
+ if is_stream_edited and not target_stream.is_public():
+ # Do not compute group memberships for public streams as it
+ # is required only to check content access to private streams.
+ for group in get_user_id_annotated_recursive_membership_groups_for_users(
+ [user_topic.user_profile_id for user_topic in orig_topic_user_topics]
+ ):
+ recursive_group_ids_by_user.setdefault(group.user_id, set()).add(group.id) # type: ignore[attr-defined] # user_id is an annotated field.
+
+ for orig_user_topic in orig_topic_user_topics:
+ user_profile = orig_user_topic.user_profile
+ if is_stream_edited and not user_has_content_access(
+ user_profile,
+ target_stream,
+ UserGroupMembershipDetails(
+ user_recursive_group_ids=recursive_group_ids_by_user.get(user_profile.id, set())
+ ),
+ is_subscribed=orig_user_topic.is_target_stream_subscriber,
+ ):
+ # A topic visibility policy is migrated to the target channel
+ # only for users who have content access to it. Users without
+ # content access have their policy removed instead, so we never
+ # leave a UserTopic row referencing a channel they cannot access.
+ stream_inaccessible_to_user_profiles.append(user_profile)
else:
- orig_topic_user_profile_to_visibility_policy[user_topic.user_profile] = (
- user_topic.visibility_policy
+ orig_topic_user_profile_to_visibility_policy[user_profile] = (
+ orig_user_topic.visibility_policy
)
for user_topic in get_users_with_user_topic_visibility_policy(
@@ -1293,7 +1332,6 @@ def do_update_message(
target_stream=target_stream,
target_topic_name=target_topic_name,
target_topic_has_messages=target_topic_has_messages,
- users_losing_access=users_losing_access,
)
)
diff --git a/zerver/tests/test_message_move_topic.py b/zerver/tests/test_message_move_topic.py
index 30687d24ee6..5d350932a16 100644
--- a/zerver/tests/test_message_move_topic.py
+++ b/zerver/tests/test_message_move_topic.py
@@ -706,7 +706,7 @@ class MessageMoveTopicTest(ZulipTestCase):
]
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(31):
+ with self.assert_database_query_count(32):
check_update_message(
user_profile=desdemona,
message_id=message_id,
@@ -781,6 +781,85 @@ class MessageMoveTopicTest(ZulipTestCase):
assert_is_topic_muted(cordelia, new_public_stream.id, "final topic name", muted=False)
assert_is_topic_muted(aaron, new_public_stream.id, "final topic name", muted=False)
+ def test_move_topic_to_private_stream_migrates_policy_by_content_access(self) -> None:
+ # When a topic is moved to a channel, its visibility policies are
+ # migrated for exactly the users who have content access to the
+ # target channel.
+ hamlet = self.example_user("hamlet")
+ desdemona = self.example_user("desdemona")
+ othello = self.example_user("othello")
+ cordelia = self.example_user("cordelia")
+ iago = self.example_user("iago")
+
+ stream_name = "public source"
+ stream = self.make_stream(stream_name)
+ self.subscribe(hamlet, stream_name)
+ self.login_user(hamlet)
+ message_id = self.send_stream_message(
+ hamlet, stream_name, topic_name="topic", content="Hello World"
+ )
+
+ # A private channel with protected history.
+ private_stream = self.make_stream(
+ "private target", invite_only=True, history_public_to_subscribers=False
+ )
+ self.subscribe(desdemona, private_stream.name)
+ # Othello has content access to the target via subscription.
+ self.subscribe(othello, private_stream.name)
+ # Cordelia has content access via a content-access group,
+ # without being subscribed to the target.
+ content_access_group = check_add_user_group(
+ hamlet.realm, "target-content-access", [cordelia], acting_user=hamlet
+ )
+ do_change_stream_group_based_setting(
+ private_stream,
+ "can_add_subscribers_group",
+ content_access_group,
+ acting_user=desdemona,
+ )
+
+ # None of Othello, Cordelia, or Iago is subscribed to the source
+ # channel.
+ muted_topics = [[stream_name, "topic"]]
+ for user in [othello, cordelia, iago]:
+ set_topic_visibility_policy(user, muted_topics, UserTopic.VisibilityPolicy.MUTED)
+
+ check_update_message(
+ user_profile=desdemona,
+ message_id=message_id,
+ stream_id=private_stream.id,
+ propagate_mode="change_all",
+ send_notification_to_old_thread=False,
+ send_notification_to_new_thread=False,
+ content=None,
+ )
+
+ # Othello and Cordelia keep the UserTopic row, migrated to
+ # the private channel.
+ for user in [othello, cordelia]:
+ self.assertFalse(
+ UserTopic.objects.filter(
+ user_profile=user, stream=stream.id, topic_name="topic"
+ ).exists()
+ )
+ self.assertTrue(
+ topic_has_visibility_policy(
+ user, private_stream.id, "topic", UserTopic.VisibilityPolicy.MUTED
+ )
+ )
+
+ # Iago has no content access, so the UserTopic row is removed.
+ self.assertFalse(
+ UserTopic.objects.filter(
+ user_profile=iago, stream=stream.id, topic_name="topic"
+ ).exists()
+ )
+ self.assertFalse(
+ UserTopic.objects.filter(
+ user_profile=iago, stream=private_stream.id, topic_name="topic"
+ ).exists()
+ )
+
@mock.patch("zerver.actions.user_topics.send_event_on_commit")
def test_edit_unmuted_topic(self, mock_send_event_on_commit: mock.MagicMock) -> None:
stream_name = "Stream 123"