mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
Extract check_if_a_bot_is_sending_a_message_to_an_empty_stream().
(imported from commit 3312d70f6b94f4a13a42bbe0320f4bb1062262e7)
This commit is contained in:
@@ -413,6 +413,36 @@ def check_stream_name(stream_name):
|
|||||||
if not valid_stream_name(stream_name):
|
if not valid_stream_name(stream_name):
|
||||||
raise JsonableError("Invalid 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:
|
# check_message:
|
||||||
# Returns message ready for sending with do_send_message on success or the error message (string) on error.
|
# 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,
|
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)
|
stream = get_stream(stream_name, realm)
|
||||||
|
|
||||||
if sender.is_bot and sender.bot_owner is not None:
|
check_if_a_bot_is_sending_a_message_to_an_empty_stream(sender, stream, stream_name)
|
||||||
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'])
|
|
||||||
|
|
||||||
if stream is None:
|
if stream is None:
|
||||||
raise JsonableError("Stream does not exist")
|
raise JsonableError("Stream does not exist")
|
||||||
|
|||||||
Reference in New Issue
Block a user