Add StreamTopicTarget.user_ids_muting_topic().

This commit is contained in:
Steve Howell
2017-10-23 10:39:12 -07:00
committed by Tim Abbott
parent 0164cfdf5c
commit 78b1a8442b
2 changed files with 73 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
from typing import (Dict, List, Text, Set)
from zerver.models import MutedTopic
class StreamTopicTarget(object):
'''
This class is designed to help us move to a
StreamTopic table or something similar. It isolates
places where we are are still using `subject` or
`topic_name` as a key into tables.
'''
def __init__(self, stream_id, topic_name):
# type: (int, Text) -> None
self.stream_id = stream_id
self.topic_name = topic_name
def user_ids_muting_topic(self):
# type: () -> Set[int]
query = MutedTopic.objects.filter(
stream_id=self.stream_id,
topic_name__iexact=self.topic_name,
).values(
'user_profile_id',
)
return {
row['user_profile_id']
for row in query
}

View File

@@ -6,7 +6,17 @@ from mock import patch
from typing import Any, Dict
from zerver.lib.test_classes import ZulipTestCase
from zerver.models import get_realm, get_user, get_stream, get_recipient, Recipient
from zerver.lib.stream_topic import StreamTopicTarget
from zerver.models import (
get_realm,
get_recipient,
get_stream,
get_user,
Recipient,
UserProfile,
)
from zerver.lib.topic_mutes import (
add_topic_mute,
get_topic_mutes,
@@ -14,6 +24,40 @@ from zerver.lib.topic_mutes import (
)
class MutedTopicsTests(ZulipTestCase):
def test_user_ids_muting_topic(self):
# type: () -> None
hamlet = self.example_user('hamlet')
cordelia = self.example_user('cordelia')
realm = hamlet.realm
stream = get_stream(u'Verona', realm)
recipient = get_recipient(Recipient.STREAM, stream.id)
topic_name = 'teST topic'
stream_topic_target = StreamTopicTarget(
stream_id=stream.id,
topic_name=topic_name,
)
user_ids = stream_topic_target.user_ids_muting_topic()
self.assertEqual(user_ids, set())
def mute_user(user):
# type: (UserProfile) -> None
add_topic_mute(
user_profile=user,
stream_id=stream.id,
recipient_id=recipient.id,
topic_name='test TOPIC',
)
mute_user(hamlet)
user_ids = stream_topic_target.user_ids_muting_topic()
self.assertEqual(user_ids, {hamlet.id})
mute_user(cordelia)
user_ids = stream_topic_target.user_ids_muting_topic()
self.assertEqual(user_ids, {hamlet.id, cordelia.id})
def test_add_muted_topic(self):
# type: () -> None
email = self.example_email('hamlet')