Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_edit: Extract topic visibility migration logic.
santhoshkumarCodese2c809502e6d
diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.pyindex 8c4f0de60e7..71c4df98c5c 100644--- a/zerver/actions/message_edit.py+++ b/zerver/actions/message_edit.py@@ -620,6 +620,149 @@ def update_message_content()+def update_user_topic_visibility_policies_on_move(+ is_stream_edited: bool,+ stream_being_edited: Stream,+ orig_topic_name: str,+ 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)+ else:+ orig_topic_user_profile_to_visibility_policy[user_topic.user_profile] = (+ user_topic.visibility_policy+ )++ for user_topic in get_users_with_user_topic_visibility_policy(+ target_stream.id, target_topic_name+ ):+ target_topic_user_profile_to_visibility_policy[user_topic.user_profile] = (+ user_topic.visibility_policy+ )++ # User profiles having any of the visibility policies set for either the original or target topic.+ user_profiles_having_visibility_policy: set[UserProfile] = set(+ itertools.chain(+ orig_topic_user_profile_to_visibility_policy.keys(),+ target_topic_user_profile_to_visibility_policy.keys(),+ )+ )++ user_profiles_for_visibility_policy_pair: dict[tuple[int, int], list[UserProfile]] = (+ defaultdict(list)+ )+ for user_profile_with_policy in user_profiles_having_visibility_policy:+ if user_profile_with_policy not in target_topic_user_profile_to_visibility_policy:+ target_topic_user_profile_to_visibility_policy[user_profile_with_policy] = (+ UserTopic.VisibilityPolicy.INHERIT+ )+ elif user_profile_with_policy not in orig_topic_user_profile_to_visibility_policy:+ orig_topic_user_profile_to_visibility_policy[user_profile_with_policy] = (+ UserTopic.VisibilityPolicy.INHERIT+ )++ orig_topic_visibility_policy = orig_topic_user_profile_to_visibility_policy[+ user_profile_with_policy+ ]+ target_topic_visibility_policy = target_topic_user_profile_to_visibility_policy[+ user_profile_with_policy+ ]+ user_profiles_for_visibility_policy_pair[+ (orig_topic_visibility_policy, target_topic_visibility_policy)+ ].append(user_profile_with_policy)++ # 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, by removing the old+ # topic visibility_policy and creating a new one.+ #+ # Algorithm used for the 'merge userTopic states' case:+ # Using the 'user_profiles_for_visibility_policy_pair' dictionary,+ # we have 'orig_topic_visibility_policy', 'target_topic_visibility_policy',+ # and a list of 'user_profiles' having the mentioned visibility policies.+ #+ # For every 'orig_topic_visibility_policy and target_topic_visibility_policy' pair,+ # we determine the final visibility_policy that should be after the merge.+ # Update the visibility_policy for the concerned set of user_profiles.+ for (+ visibility_policy_pair,+ user_profiles,+ ) in user_profiles_for_visibility_policy_pair.items():+ orig_topic_visibility_policy, target_topic_visibility_policy = visibility_policy_pair++ if orig_topic_visibility_policy != UserTopic.VisibilityPolicy.INHERIT:+ bulk_do_set_user_topic_visibility_policy(+ user_profiles,+ stream_being_edited,+ orig_topic_name,+ visibility_policy=UserTopic.VisibilityPolicy.INHERIT,+ # bulk_do_set_user_topic_visibility_policy with visibility_policy+ # set to 'new_visibility_policy' will send an updated muted topic+ # event, which contains the full set of muted+ # topics, just after this.+ skip_muted_topics_event=True,+ )++ new_visibility_policy = orig_topic_visibility_policy++ if target_topic_has_messages:+ # Here, we handle the complex case when target_topic already has+ # some messages. We determine the resultant visibility_policy+ # based on the visibility_policy of the orig_topic + target_topic.+ # Finally, bulk_update the user_topic rows with the new visibility_policy.+ new_visibility_policy = get_visibility_policy_after_merge(+ orig_topic_visibility_policy, target_topic_visibility_policy+ )+ if new_visibility_policy == target_topic_visibility_policy:+ continue+ bulk_do_set_user_topic_visibility_policy(+ user_profiles,+ target_stream,+ target_topic_name,+ visibility_policy=new_visibility_policy,+ )+ else:+ # This corresponds to the case when messages are moved+ # to a stream-topic pair that didn't exist. There can+ # still be UserTopic rows for the stream-topic pair+ # that didn't exist if the messages in that topic had+ # been deleted.+ if new_visibility_policy == target_topic_visibility_policy:+ # This avoids unnecessary db operations and INFO logs.+ continue+ bulk_do_set_user_topic_visibility_policy(+ user_profiles,+ target_stream,+ target_topic_name,+ visibility_policy=new_visibility_policy,+ )++ return orig_topic_user_profile_to_visibility_policy++def apply_automatic_unmute_follow_topics_policy(user_profile: UserProfile,target_stream: Stream,@@ -1096,139 +1239,18 @@ def do_update_message(## This rule corresponds to checking moved_all_visible_messages.if moved_all_visible_messages:- 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 (- message_edit_request.is_stream_edited- and user_topic.user_profile_id in user_ids_losing_access- ):- stream_inaccessible_to_user_profiles.append(user_topic.user_profile)- else:- orig_topic_user_profile_to_visibility_policy[user_topic.user_profile] = (- user_topic.visibility_policy- )-- for user_topic in get_users_with_user_topic_visibility_policy(- target_stream.id, target_topic_name- ):- target_topic_user_profile_to_visibility_policy[user_topic.user_profile] = (- user_topic.visibility_policy- )-- # User profiles having any of the visibility policies set for either the original or target topic.- user_profiles_having_visibility_policy: set[UserProfile] = set(- itertools.chain(- orig_topic_user_profile_to_visibility_policy.keys(),- target_topic_user_profile_to_visibility_policy.keys(),+ orig_topic_user_profile_to_visibility_policy = (+ update_user_topic_visibility_policies_on_move(+ is_stream_edited=message_edit_request.is_stream_edited,+ stream_being_edited=stream_being_edited,+ orig_topic_name=orig_topic_name,+ target_stream=target_stream,+ target_topic_name=target_topic_name,+ target_topic_has_messages=target_topic_has_messages,+ users_losing_access=users_losing_access,))- user_profiles_for_visibility_policy_pair: dict[tuple[int, int], list[UserProfile]] = (- defaultdict(list)- )- for user_profile_with_policy in user_profiles_having_visibility_policy:- if user_profile_with_policy not in target_topic_user_profile_to_visibility_policy:- target_topic_user_profile_to_visibility_policy[user_profile_with_policy] = (- UserTopic.VisibilityPolicy.INHERIT- )- elif user_profile_with_policy not in orig_topic_user_profile_to_visibility_policy:- orig_topic_user_profile_to_visibility_policy[user_profile_with_policy] = (- UserTopic.VisibilityPolicy.INHERIT- )-- orig_topic_visibility_policy = orig_topic_user_profile_to_visibility_policy[- user_profile_with_policy- ]- target_topic_visibility_policy = target_topic_user_profile_to_visibility_policy[- user_profile_with_policy- ]- user_profiles_for_visibility_policy_pair[- (orig_topic_visibility_policy, target_topic_visibility_policy)- ].append(user_profile_with_policy)-- # 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, by removing the old- # topic visibility_policy and creating a new one.- #- # Algorithm used for the 'merge userTopic states' case:- # Using the 'user_profiles_for_visibility_policy_pair' dictionary,- # we have 'orig_topic_visibility_policy', 'target_topic_visibility_policy',- # and a list of 'user_profiles' having the mentioned visibility policies.- #- # For every 'orig_topic_visibility_policy and target_topic_visibility_policy' pair,- # we determine the final visibility_policy that should be after the merge.- # Update the visibility_policy for the concerned set of user_profiles.- for (- visibility_policy_pair,- user_profiles,- ) in user_profiles_for_visibility_policy_pair.items():- orig_topic_visibility_policy, target_topic_visibility_policy = visibility_policy_pair-- if orig_topic_visibility_policy != UserTopic.VisibilityPolicy.INHERIT:- bulk_do_set_user_topic_visibility_policy(- user_profiles,- stream_being_edited,- orig_topic_name,- visibility_policy=UserTopic.VisibilityPolicy.INHERIT,- # bulk_do_set_user_topic_visibility_policy with visibility_policy- # set to 'new_visibility_policy' will send an updated muted topic- # event, which contains the full set of muted- # topics, just after this.- skip_muted_topics_event=True,- )-- new_visibility_policy = orig_topic_visibility_policy-- if target_topic_has_messages:- # Here, we handle the complex case when target_topic already has- # some messages. We determine the resultant visibility_policy- # based on the visibility_policy of the orig_topic + target_topic.- # Finally, bulk_update the user_topic rows with the new visibility_policy.- new_visibility_policy = get_visibility_policy_after_merge(- orig_topic_visibility_policy, target_topic_visibility_policy- )- if new_visibility_policy == target_topic_visibility_policy:- continue- bulk_do_set_user_topic_visibility_policy(- user_profiles,- target_stream,- target_topic_name,- visibility_policy=new_visibility_policy,- )- else:- # This corresponds to the case when messages are moved- # to a stream-topic pair that didn't exist. There can- # still be UserTopic rows for the stream-topic pair- # that didn't exist if the messages in that topic had- # been deleted.- if new_visibility_policy == target_topic_visibility_policy:- # This avoids unnecessary db operations and INFO logs.- continue- bulk_do_set_user_topic_visibility_policy(- user_profiles,- target_stream,- target_topic_name,- visibility_policy=new_visibility_policy,- )-elif message_edit_request.is_stream_edited or message_edit_request.is_topic_edited:sender = target_message.sender