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
This commit is contained in:
PIG208 2021-04-04 06:02:12 +08:00 committed by Tim Abbott
parent 4627b07396
commit 42aecf683e
2 changed files with 35 additions and 6 deletions

View file

@ -32,15 +32,20 @@ class IncrementorHandler:
num = int(num) + 1 num = int(num) + 1
storage.put('number', num) storage.put('number', num)
if storage.get('message_id') is None: if storage.get('message_id') is not None:
result = bot_handler.send_reply(message, str(num)) result = bot_handler.update_message(dict(
if result is not None:
storage.put('message_id', result['id'])
else:
bot_handler.update_message(dict(
message_id=storage.get('message_id'), message_id=storage.get('message_id'),
content=str(num) 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 handler_class = IncrementorHandler

View file

@ -29,3 +29,27 @@ class TestIncrementorBot(BotTestCase, DefaultTests):
for item in m.call_args_list for item in m.call_args_list
] ]
self.assertEqual(content_updates, ['2', '3', '4']) 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'])