addressee: Add support for stream IDs to Addressee.legacy_build().

This commit is contained in:
Eeshan Garg 2019-01-25 23:09:02 -03:30 committed by Tim Abbott
parent 9b25f8789f
commit b8221555d2
2 changed files with 38 additions and 2 deletions

View File

@ -66,11 +66,13 @@ class Addressee:
def __init__(self, msg_type: str,
user_profiles: Optional[Sequence[UserProfile]]=None,
stream_name: Optional[str]=None,
stream_id: Optional[int]=None,
topic: Optional[str]=None) -> None:
assert(msg_type in ['stream', 'private'])
self._msg_type = msg_type
self._user_profiles = user_profiles
self._stream_name = stream_name
self._stream_id = stream_id
self._topic = topic
def is_stream(self) -> bool:
@ -88,6 +90,10 @@ class Addressee:
assert(self._stream_name is not None)
return self._stream_name
def stream_id(self) -> Optional[int]:
assert(self.is_stream())
return self._stream_id
def topic(self) -> str:
assert(self.is_stream())
assert(self._topic is not None)
@ -111,17 +117,22 @@ class Addressee:
raise JsonableError(_("Cannot send to multiple streams"))
if message_to:
stream_name = cast(str, message_to[0])
stream_name_or_id = message_to[0]
else:
# This is a hack to deal with the fact that we still support
# default streams (and the None will be converted later in the
# callpath).
if sender.default_sending_stream:
# Use the users default stream
stream_name = sender.default_sending_stream.name
stream_name_or_id = sender.default_sending_stream.name
else:
raise JsonableError(_('Missing stream'))
if isinstance(stream_name_or_id, int):
stream_id = cast(int, stream_name_or_id)
return Addressee.for_stream_id(stream_id, topic_name)
stream_name = cast(str, stream_name_or_id)
return Addressee.for_stream(stream_name, topic_name)
elif message_type_name == 'private':
if not message_to:
@ -145,6 +156,15 @@ class Addressee:
topic=topic,
)
@staticmethod
def for_stream_id(stream_id: int, topic: str) -> 'Addressee':
topic = validate_topic(topic)
return Addressee(
msg_type='stream',
stream_id=stream_id,
topic=topic,
)
@staticmethod
def for_private(emails: Sequence[str], realm: Realm) -> 'Addressee':
assert len(emails) > 0

View File

@ -429,6 +429,22 @@ class TestAddressee(ZulipTestCase):
self.assertEqual(set(result_user_ids), set(user_ids))
def test_addressee_legacy_build_for_stream_id(self) -> None:
realm = get_realm('zulip')
self.login(self.example_email('iago'))
sender = self.example_user('iago')
self.subscribe(sender, "Denmark")
stream = get_stream('Denmark', realm)
result = Addressee.legacy_build(
sender=sender, message_type_name='stream',
message_to=[stream.id], topic_name='random_topic',
realm=realm
)
stream_id = result.stream_id()
self.assertEqual(stream.id, stream_id)
class InternalPrepTest(ZulipTestCase):
def test_returns_for_internal_sends(self) -> None: