internal_send_stream_message: Support accepting a Stream object.

If the caller has access to a Stream object, it is wasteful to
query a database for a stream by ID or name. In addition, not
having to go through stream names eliminates various classes of
possible bugs involved with re-fetching the Stream object by name.
This commit is contained in:
Eeshan Garg
2019-02-07 22:43:14 -03:30
committed by Tim Abbott
parent c240008edb
commit 3470e541c8
4 changed files with 31 additions and 17 deletions

View File

@@ -2400,10 +2400,18 @@ def internal_send_private_message(realm: Realm,
return
do_send_messages([message])
def internal_send_stream_message(realm: Realm, sender: UserProfile, stream_name: str,
topic: str, content: str) -> None:
message = internal_prep_stream_message(realm, sender, topic,
content, stream_name=stream_name)
def internal_send_stream_message(
realm: Realm, sender: UserProfile,
topic: str, content: str,
stream_name: Optional[str]=None,
stream: Optional[Stream]=None
) -> None:
message = internal_prep_stream_message(
realm, sender, topic,
content, stream_name=stream_name,
stream=stream
)
if message is None:
return
do_send_messages([message])
@@ -3443,11 +3451,11 @@ def do_rename_stream(stream: Stream,
internal_send_stream_message(
stream.realm,
sender,
new_name,
"welcome",
"_@**%s|%d** renamed stream **%s** to **%s**" % (user_profile.full_name,
user_profile.id,
old_name, new_name)
old_name, new_name),
stream=stream
)
# Even though the token doesn't change, the web client needs to update the
# email forwarding address to display the correctly-escaped new name.

View File

@@ -71,8 +71,11 @@ class EmbeddedBotHandler:
self._rate_limit.show_error_and_exit()
if message['type'] == 'stream':
internal_send_stream_message(self.user_profile.realm, self.user_profile, message['to'],
message['topic'], message['content'])
internal_send_stream_message(
self.user_profile.realm, self.user_profile,
message['topic'], message['content'],
stream_name=message['to']
)
return
assert message['type'] == 'private'

View File

@@ -21,7 +21,7 @@ from zerver.lib.utils import generate_random_token
from zerver.lib.send_email import FromAddress
from zerver.models import Stream, Recipient, \
get_user_profile_by_id, get_display_recipient, get_personal_recipient, \
Message, Realm, UserProfile, get_system_bot, get_user
Message, Realm, UserProfile, get_system_bot, get_user, get_stream_by_id_in_realm
logger = logging.getLogger(__name__)
@@ -154,22 +154,25 @@ def send_to_missed_message_address(address: str, message: message.Message) -> No
user_profile = get_user_profile_by_id(user_profile_id)
recipient = Recipient.objects.get(id=recipient_id)
display_recipient = get_display_recipient(recipient)
body = construct_zulip_body(message, user_profile.realm)
if recipient.type == Recipient.STREAM:
assert isinstance(display_recipient, str)
recipient_str = display_recipient
internal_send_stream_message(user_profile.realm, user_profile, recipient_str,
subject_b.decode('utf-8'), body)
stream = get_stream_by_id_in_realm(recipient.type_id, user_profile.realm)
internal_send_stream_message(
user_profile.realm, user_profile,
subject_b.decode('utf-8'), body,
stream=stream
)
elif recipient.type == Recipient.PERSONAL:
display_recipient = get_display_recipient(recipient)
assert not isinstance(display_recipient, str)
recipient_str = display_recipient[0]['email']
recipient_user = get_user(recipient_str, user_profile.realm)
internal_send_private_message(user_profile.realm, user_profile,
recipient_user, body)
elif recipient.type == Recipient.HUDDLE:
display_recipient = get_display_recipient(recipient)
assert not isinstance(display_recipient, str)
emails = [user_dict['email'] for user_dict in display_recipient]
recipient_str = ', '.join(emails)

View File

@@ -508,7 +508,7 @@ class InternalPrepTest(ZulipTestCase):
cordelia = self.example_user('cordelia')
hamlet = self.example_user('hamlet')
othello = self.example_user('othello')
stream_name = 'Verona'
stream = get_stream('Verona', realm)
with mock.patch('logging.exception') as m:
internal_send_private_message(
@@ -536,9 +536,9 @@ class InternalPrepTest(ZulipTestCase):
internal_send_stream_message(
realm=realm,
sender=cordelia,
stream_name=stream_name,
topic='whatever',
content=bad_content,
stream=stream
)
arg = m.call_args_list[0][0][0]
@@ -549,7 +549,7 @@ class InternalPrepTest(ZulipTestCase):
realm=realm,
sender_email=settings.ERROR_BOT,
recipient_type_name='stream',
recipients=stream_name,
recipients=stream.name,
topic_name='whatever',
content=bad_content,
)