topic_mutes: Filter deactivated streams from get_topic_mutes.

Updated database query to filter out deactivated streams from the
return of the get_topic_mutes method. Added optional
include_deactivated parameter to the method to make the behavior
default but overrideable. Added test case in test_muting for these
changes. Fixes blueslip warnings thrown by muting.js set_muted_topics
when passed deactivated streams via page_params.
This commit is contained in:
lukem1 2021-02-15 20:26:56 -05:00 committed by Tim Abbott
parent ec80d2d5db
commit 105a5a95ee
2 changed files with 35 additions and 2 deletions

View File

@ -9,8 +9,15 @@ from zerver.lib.topic import topic_match_sa
from zerver.models import MutedTopic, UserProfile, get_stream
def get_topic_mutes(user_profile: UserProfile) -> List[Tuple[str, str, float]]:
rows = MutedTopic.objects.filter(user_profile=user_profile).values(
def get_topic_mutes(
user_profile: UserProfile, include_deactivated: bool = False
) -> List[Tuple[str, str, float]]:
query = MutedTopic.objects.filter(user_profile=user_profile)
# Exclude muted topics that are part of deactivated streams unless
# explicitly requested.
if not include_deactivated:
query = query.filter(stream__deactivated=False)
rows = query.values(
"stream__name",
"topic_name",
"date_muted",
@ -88,6 +95,9 @@ def topic_is_muted(user_profile: UserProfile, stream_id: int, topic_name: str) -
def exclude_topic_mutes(
conditions: List[ClauseElement], user_profile: UserProfile, stream_id: Optional[int]
) -> List[ClauseElement]:
# Note: Unlike get_topic_mutes, here we always want to
# consider topics in deactivated streams, so they are
# never filtered from the query in this method.
query = MutedTopic.objects.filter(
user_profile=user_profile,
)

View File

@ -16,6 +16,29 @@ from zerver.models import MutedTopic, UserProfile, get_stream
class MutedTopicsTests(ZulipTestCase):
def test_get_deactivated_muted_topic(self) -> None:
user = self.example_user("hamlet")
self.login_user(user)
stream = get_stream("Verona", user.realm)
recipient = stream.recipient
mock_date_muted = datetime(2020, 1, 1, tzinfo=timezone.utc).timestamp()
add_topic_mute(
user_profile=user,
stream_id=stream.id,
recipient_id=recipient.id,
topic_name="Verona3",
date_muted=datetime(2020, 1, 1, tzinfo=timezone.utc),
)
stream.deactivated = True
stream.save()
self.assertNotIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user))
self.assertIn((stream.name, "Verona3", mock_date_muted), get_topic_mutes(user, True))
def test_user_ids_muting_topic(self) -> None:
hamlet = self.example_user("hamlet")
cordelia = self.example_user("cordelia")