From 83bfcb653540ac8667910cc3b9c8b1c6f517684d Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Fri, 18 Aug 2017 22:11:37 +0530 Subject: [PATCH] bots: Modify is_private function to take 'id' as parameter. This is done so that Embedded bot system can also make use of this function directly, as only id is needed in this function. Tweaked by tabbott to have a cleaner interface and simpler documentation. --- zulip_bots/zulip_bots/lib.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 4aec921..e1bfa20 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -175,12 +175,17 @@ def extract_query_without_mention(message, client): query_without_mention = message['content'][len(start_with_mention.group()):] return query_without_mention.lstrip() -def is_private(message, client): - # type: (Dict[str, Any], ExternalBotHandler) -> bool - # bot will not reply if the sender id is the same as the bot id - # to prevent infinite loop - if message['type'] == 'private': - return client.user_id != message['sender_id'] +def is_private_message_from_another_user(message_dict, current_user_id): + # type: (Dict[str, Any], int) -> bool + """ + Checks whether a message dict represents a PM from another user. + + This function is used by the embedded bot system in the + zulip/zulip project, so refactor with care. See the comments in + extract_query_without_mention. + """ + if message_dict['type'] == 'private': + return current_user_id != message_dict['sender_id'] return False def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): @@ -212,7 +217,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file, bot_name): # is_mentioned is true if the bot is mentioned at ANY position (not necessarily # the first @mention in the message). is_mentioned = message['is_mentioned'] - is_private_message = is_private(message, restricted_client) + is_private_message = is_private_message_from_another_user(message, restricted_client.user_id) # Strip at-mention botname from the message if is_mentioned: