bot_lib: Raise exception when PM recipient list is empty.

We need to explicitly check for empty recipient lists in
send_message to ensure that internal_send_huddle_message doesn't
call Addressee.for_private with an empty recipient list.
This commit is contained in:
Eeshan Garg
2019-01-07 11:50:21 -03:30
committed by Tim Abbott
parent 020f2d0db5
commit 884b67bea7
2 changed files with 14 additions and 2 deletions

View File

@@ -15,6 +15,8 @@ from zerver.lib.bot_config import get_bot_config, ConfigError
from zerver.lib.integrations import EMBEDDED_BOTS
from zerver.lib.topic import get_topic_from_message_info
from django.utils.translation import ugettext as _
import configparser
from mypy_extensions import NoReturn
@@ -60,6 +62,9 @@ class StateHandler:
class EmbeddedBotQuitException(Exception):
pass
class EmbeddedBotEmptyRecipientsList(Exception):
pass
class EmbeddedBotHandler:
def __init__(self, user_profile: UserProfile) -> None:
# Only expose a subset of our UserProfile's functionality
@@ -84,7 +89,9 @@ class EmbeddedBotHandler:
# usual 'to' field could be either a List[str] or a str.
recipients = ','.join(message['to']).split(',')
if len(message['to']) == 1:
if len(message['to']) == 0:
raise EmbeddedBotEmptyRecipientsList(_('Message must have recipients!'))
elif len(message['to']) == 1:
recipient_user = get_active_user(recipients[0], self.user_profile.realm)
internal_send_private_message(self.user_profile.realm, self.user_profile,
recipient_user, message['content'])