From f13cb94d8402edce037a58837da91c8eaeceb503 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 17 Sep 2017 11:19:12 -0700 Subject: [PATCH] Have exclude_topic_mutes() accept a stream id. We want to convert stream names to stream ids as close to the "edges" of our system as possible, so we let our caller do the work of finding the stream id for a stream narrow. --- tools/linter_lib/custom_check.py | 2 ++ zerver/lib/topic_mutes.py | 14 +++++--------- zerver/views/messages.py | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py index 48e262d36f..d91aca9679 100644 --- a/tools/linter_lib/custom_check.py +++ b/tools/linter_lib/custom_check.py @@ -298,6 +298,8 @@ def build_custom_checkers(by_lang): ('zerver/lib/actions.py', 'get_stream(signups_stream, admin_realm)'), # Here we need get_stream to access streams you've since unsubscribed from. ('zerver/views/messages.py', 'stream = get_stream(operand, self.user_profile.realm)'), + # Use stream_id to exclude mutes. + ('zerver/views/messages.py', 'stream_id = get_stream(stream_name, user_profile.realm).id'), ]), 'description': 'Please use access_stream_by_*() to fetch Stream objects', }, diff --git a/zerver/lib/topic_mutes.py b/zerver/lib/topic_mutes.py index eb3ee70687..db1c459273 100644 --- a/zerver/lib/topic_mutes.py +++ b/zerver/lib/topic_mutes.py @@ -1,7 +1,7 @@ from __future__ import absolute_import from zerver.models import UserProfile -from typing import Any, Callable, Dict, List, Text +from typing import Any, Callable, Dict, List, Optional, Text from zerver.models import ( bulk_get_recipients, @@ -89,20 +89,16 @@ def topic_is_muted(user_profile, stream, topic_name): ).exists() return is_muted -def exclude_topic_mutes(conditions, user_profile, stream_name): - # type: (List[Selectable], UserProfile, Text) -> List[Selectable] +def exclude_topic_mutes(conditions, user_profile, stream_id): + # type: (List[Selectable], UserProfile, Optional[int]) -> List[Selectable] query = MutedTopic.objects.filter( user_profile=user_profile, ) - if stream_name is not None: + if stream_id is not None: # If we are narrowed to a stream, we can optimize the query # by not considering topic mutes outside the stream. - try: - stream = get_stream(stream_name, user_profile.realm) - query = query.filter(stream=stream) - except Stream.DoesNotExist: - pass + query = query.filter(stream_id=stream_id) query = query.values( 'recipient_id', diff --git a/zerver/views/messages.py b/zerver/views/messages.py index 63346f3176..1afb280b92 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -530,7 +530,18 @@ def exclude_muting_conditions(user_profile, narrow): conditions = [] stream_name = get_stream_name_from_narrow(narrow) - if stream_name is None: + stream_id = None + if stream_name is not None: + try: + # Note that this code works around a lint rule that + # says we should use access_stream_by_name to get the + # stream. It is okay here, because we are only using + # the stream id to exclude data, not to include results. + stream_id = get_stream(stream_name, user_profile.realm).id + except Stream.DoesNotExist: + pass + + if stream_id is None: rows = Subscription.objects.filter( user_profile=user_profile, active=True, @@ -543,7 +554,7 @@ def exclude_muting_conditions(user_profile, narrow): condition = not_(column("recipient_id").in_(muted_recipient_ids)) conditions.append(condition) - conditions = exclude_topic_mutes(conditions, user_profile, stream_name) + conditions = exclude_topic_mutes(conditions, user_profile, stream_id) return conditions