diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index 25fe9baf4f..ee31ddffd8 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -413,6 +413,36 @@ def check_stream_name(stream_name): if not valid_stream_name(stream_name): raise JsonableError("Invalid stream name") +def check_if_a_bot_is_sending_a_message_to_an_empty_stream(sender, stream, stream_name): + if sender.is_bot and sender.bot_owner is not None: + if stream: + num_subscribers = len(maybe_get_subscriber_emails(stream)) + + if stream is None or num_subscribers == 0: + # Warn a bot's owner if they are sending a message to a stream + # that does not exist, or has no subscribers + # We warn the user once every 5 minutes to avoid a flood of + # PMs on a misconfigured integration, re-using the + # UserProfile.last_reminder field, which is not used for bots. + last_reminder = sender.last_reminder_tzaware() + waitperiod = datetime.timedelta(minutes=UserProfile.BOT_OWNER_STREAM_ALERT_WAITPERIOD) + if not last_reminder or timezone.now() - last_reminder > waitperiod: + if stream is None: + error_msg = "that stream does not yet exist. To create it, " + elif num_subscribers == 0: + error_msg = "there are no subscribers to that stream. To join it, " + + content = ("Hi there! We thought you'd like to know that your bot **%s** just " + "tried to send a message to stream `%s`, but %s" + "click the gear in the left-side stream list." % + (sender.full_name, stream_name, error_msg)) + message = internal_prep_message("notification-bot@zulip.com", "private", + sender.bot_owner.email, "", content) + do_send_messages([message]) + + sender.last_reminder = timezone.now() + sender.save(update_fields=['last_reminder']) + # check_message: # Returns message ready for sending with do_send_message on success or the error message (string) on error. def check_message(sender, client, message_type_name, message_to, @@ -449,34 +479,7 @@ def check_message(sender, client, message_type_name, message_to, stream = get_stream(stream_name, realm) - if sender.is_bot and sender.bot_owner is not None: - if stream: - num_subscribers = len(maybe_get_subscriber_emails(stream)) - - if stream is None or num_subscribers == 0: - # Warn a bot's owner if they are sending a message to a stream - # that does not exist, or has no subscribers - # We warn the user once every 5 minutes to avoid a flood of - # PMs on a misconfigured integration, re-using the - # UserProfile.last_reminder field, which is not used for bots. - last_reminder = sender.last_reminder_tzaware() - waitperiod = datetime.timedelta(minutes=UserProfile.BOT_OWNER_STREAM_ALERT_WAITPERIOD) - if not last_reminder or timezone.now() - last_reminder > waitperiod: - if stream is None: - error_msg = "that stream does not yet exist. To create it, " - elif num_subscribers == 0: - error_msg = "there are no subscribers to that stream. To join it, " - - content = ("Hi there! We thought you'd like to know that your bot **%s** just " - "tried to send a message to stream `%s`, but %s" - "click the gear in the left-side stream list." % - (sender.full_name, stream_name, error_msg)) - message = internal_prep_message("notification-bot@zulip.com", "private", - sender.bot_owner.email, "", content) - do_send_messages([message]) - - sender.last_reminder = timezone.now() - sender.save(update_fields=['last_reminder']) + check_if_a_bot_is_sending_a_message_to_an_empty_stream(sender, stream, stream_name) if stream is None: raise JsonableError("Stream does not exist")