Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_topics: Refactor build_topic_mute_checker to support each policy.
Prakhar Pratyushfaa98317bc2a
diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex 5cde4d16fdd..bd1a3b8f8e8 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -53,7 +53,7 @@ from zerver.lib.topic import DB_TOPIC_NAME, MESSAGE__TOPIC, TOPIC_LINKS, TOPIC_Nfrom zerver.lib.types import DisplayRecipientT, EditHistoryEvent, UserDisplayRecipientfrom zerver.lib.url_preview.types import UrlEmbedDatafrom zerver.lib.user_groups import is_user_in_group-from zerver.lib.user_topics import build_topic_mute_checker, topic_has_visibility_policy+from zerver.lib.user_topics import build_get_topic_visibility_policy, topic_has_visibility_policyfrom zerver.models import (MAX_TOPIC_NAME_LENGTH,Message,@@ -1111,13 +1111,14 @@ def extract_unread_data_from_um_rows(muted_stream_ids = get_muted_stream_ids(user_profile)raw_unread_messages["muted_stream_ids"] = muted_stream_ids- topic_mute_checker = build_topic_mute_checker(user_profile)+ get_topic_visibility_policy = build_get_topic_visibility_policy(user_profile)def is_row_muted(stream_id: int, recipient_id: int, topic: str) -> bool:if stream_id in muted_stream_ids:return True- if topic_mute_checker(recipient_id, topic):+ visibility_policy = get_topic_visibility_policy(recipient_id, topic)+ if visibility_policy == UserTopic.VisibilityPolicy.MUTED:return True# Messages sent by muted users are never unread, so we don'tdiff --git a/zerver/lib/user_topics.py b/zerver/lib/user_topics.pyindex e99f96e5da9..554a0bb8711 100644--- a/zerver/lib/user_topics.py+++ b/zerver/lib/user_topics.py@@ -1,6 +1,7 @@import datetimeimport logging-from typing import Callable, List, Optional, Tuple, TypedDict+from collections import defaultdict+from typing import Callable, Dict, List, Optional, Tuple, TypedDictfrom django.db import transactionfrom django.db.models import QuerySet@@ -233,24 +234,32 @@ def exclude_topic_mutes(return [*conditions, condition]-def build_topic_mute_checker(user_profile: UserProfile) -> Callable[[int, str], bool]:- rows = UserTopic.objects.filter(- user_profile=user_profile, visibility_policy=UserTopic.VisibilityPolicy.MUTED- ).values(+def build_get_topic_visibility_policy(+ user_profile: UserProfile,+) -> Callable[[int, str], int]:+ """Prefetch the visibility policies the user has configured for+ various topics.++ The prefetching helps to avoid the db queries later in the loop+ to determine the user's visibility policy for a topic.+ """+ rows = UserTopic.objects.filter(user_profile=user_profile).values("recipient_id","topic_name",+ "visibility_policy",)- tups = set()+ topic_to_visiblity_policy: Dict[Tuple[int, str], int] = defaultdict(int)for row in rows:recipient_id = row["recipient_id"]topic_name = row["topic_name"]- tups.add((recipient_id, topic_name.lower()))+ visibility_policy = row["visibility_policy"]+ topic_to_visiblity_policy[(recipient_id, topic_name)] = visibility_policy- def is_muted(recipient_id: int, topic: str) -> bool:- return (recipient_id, topic.lower()) in tups+ def get_topic_visibility_policy(recipient_id: int, topic: str) -> int:+ return topic_to_visiblity_policy[(recipient_id, topic.lower())]- return is_muted+ return get_topic_visibility_policydef get_users_with_user_topic_visibility_policy(