From 42aecf683e4e7628f19796b440b0be4ed96a406d Mon Sep 17 00:00:00 2001 From: PIG208 <359101898@qq.com> Date: Sun, 4 Apr 2021 06:02:12 +0800 Subject: [PATCH] zulip-bots: Fix incrementor to handle edit time limit error. When the incrementor attempts to edit a message that was sent long ago, it will fail and the message will not be updated, nor will a new message be sent. Fixes: #673 --- .../bots/incrementor/incrementor.py | 17 ++++++++----- .../bots/incrementor/test_incrementor.py | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py b/zulip_bots/zulip_bots/bots/incrementor/incrementor.py index 585ed2e..2b860c2 100644 --- a/zulip_bots/zulip_bots/bots/incrementor/incrementor.py +++ b/zulip_bots/zulip_bots/bots/incrementor/incrementor.py @@ -32,15 +32,20 @@ class IncrementorHandler: num = int(num) + 1 storage.put('number', num) - if storage.get('message_id') is None: - result = bot_handler.send_reply(message, str(num)) - if result is not None: - storage.put('message_id', result['id']) - else: - bot_handler.update_message(dict( + if storage.get('message_id') is not None: + result = bot_handler.update_message(dict( message_id=storage.get('message_id'), content=str(num) )) + # When there isn't an error while updating the message, we won't + # attempt to send the it again. + if result is None or result.get('result') != 'error': + return + + message_info = bot_handler.send_reply(message, str(num)) + if message_info is not None: + storage.put('message_id', message_info['id']) + handler_class = IncrementorHandler diff --git a/zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py b/zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py index 20ceb31..9eee297 100644 --- a/zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py +++ b/zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py @@ -29,3 +29,27 @@ class TestIncrementorBot(BotTestCase, DefaultTests): for item in m.call_args_list ] self.assertEqual(content_updates, ['2', '3', '4']) + + def test_bot_edit_timeout(self) -> None: + bot = get_bot_message_handler(self.bot_name) + bot_handler = StubBotHandler() + + message = dict(type='stream') + + bot.initialize(bot_handler) + bot.handle_message(message, bot_handler) + + error_msg = dict(msg='The time limit for editing this message has passed', result='error') + with patch('zulip_bots.test_lib.StubBotHandler.update_message', return_value=error_msg): + with patch('zulip_bots.simple_lib.SimpleMessageServer.send') as m: + bot.handle_message(message, bot_handler) + bot.handle_message(message, bot_handler) + + # When there is an error, the bot should resend the message with the new value. + self.assertEqual(m.call_count, 2) + + content_updates = [ + item[0][0]['content'] + for item in m.call_args_list + ] + self.assertEqual(content_updates, ['2', '3'])