From 72e0084692b3d8ae75452afa03726136852d9582 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Tue, 26 Sep 2023 15:18:34 +0530 Subject: [PATCH] muted_stream_ids: Use set for O(1) search operation. Update 'get_muted_stream_ids' to return a set of IDs instead of a list. This will help to avoid linear time search operations later while using 'if stream_id in muted_streams_ids'. --- zerver/lib/message.py | 10 +++++----- zerver/tests/test_message_flags.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/zerver/lib/message.py b/zerver/lib/message.py index bd1a3b8f8e..cd109c95ee 100644 --- a/zerver/lib/message.py +++ b/zerver/lib/message.py @@ -110,7 +110,7 @@ class RawUnreadMessagesResult(TypedDict): stream_dict: Dict[int, RawUnreadStreamDict] huddle_dict: Dict[int, RawUnreadHuddleDict] mentions: Set[int] - muted_stream_ids: List[int] + muted_stream_ids: Set[int] unmuted_stream_msgs: Set[int] old_unreads_missing: bool @@ -1014,7 +1014,7 @@ def get_inactive_recipient_ids(user_profile: UserProfile) -> List[int]: return inactive_recipient_ids -def get_muted_stream_ids(user_profile: UserProfile) -> List[int]: +def get_muted_stream_ids(user_profile: UserProfile) -> Set[int]: rows = ( get_stream_subscriptions_for_user(user_profile) .filter( @@ -1025,7 +1025,7 @@ def get_muted_stream_ids(user_profile: UserProfile) -> List[int]: "recipient__type_id", ) ) - muted_stream_ids = [row["recipient__type_id"] for row in rows] + muted_stream_ids = {row["recipient__type_id"] for row in rows} return muted_stream_ids @@ -1090,6 +1090,7 @@ def extract_unread_data_from_um_rows( ) -> RawUnreadMessagesResult: pm_dict: Dict[int, RawUnreadDirectMessageDict] = {} stream_dict: Dict[int, RawUnreadStreamDict] = {} + muted_stream_ids: Set[int] = set() unmuted_stream_msgs: Set[int] = set() huddle_dict: Dict[int, RawUnreadHuddleDict] = {} mentions: Set[int] = set() @@ -1098,7 +1099,7 @@ def extract_unread_data_from_um_rows( raw_unread_messages: RawUnreadMessagesResult = dict( pm_dict=pm_dict, stream_dict=stream_dict, - muted_stream_ids=[], + muted_stream_ids=muted_stream_ids, unmuted_stream_msgs=unmuted_stream_msgs, huddle_dict=huddle_dict, mentions=mentions, @@ -1109,7 +1110,6 @@ def extract_unread_data_from_um_rows( return raw_unread_messages muted_stream_ids = get_muted_stream_ids(user_profile) - raw_unread_messages["muted_stream_ids"] = muted_stream_ids get_topic_visibility_policy = build_get_topic_visibility_policy(user_profile) diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.py index 2706f4edae..d517d55de7 100644 --- a/zerver/tests/test_message_flags.py +++ b/zerver/tests/test_message_flags.py @@ -1611,7 +1611,7 @@ class MarkUnreadTest(ZulipTestCase): stream_dict={}, huddle_dict={}, mentions=set(), - muted_stream_ids=[], + muted_stream_ids=set(), unmuted_stream_msgs=set(), old_unreads_missing=False, ) @@ -1633,7 +1633,7 @@ class MarkUnreadTest(ZulipTestCase): stream_dict={}, huddle_dict={}, mentions=set(), - muted_stream_ids=[], + muted_stream_ids=set(), unmuted_stream_msgs=set(), old_unreads_missing=False, )