Skip to content

Commit 42aecf6

Browse files
PIG208timabbott
authored andcommitted
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
1 parent 4627b07 commit 42aecf6

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

zulip_bots/zulip_bots/bots/incrementor/incrementor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ def handle_message(self, message: Dict[str, str], bot_handler: BotHandler) -> No
3232
num = int(num) + 1
3333

3434
storage.put('number', num)
35-
if storage.get('message_id') is None:
36-
result = bot_handler.send_reply(message, str(num))
37-
if result is not None:
38-
storage.put('message_id', result['id'])
39-
else:
40-
bot_handler.update_message(dict(
35+
if storage.get('message_id') is not None:
36+
result = bot_handler.update_message(dict(
4137
message_id=storage.get('message_id'),
4238
content=str(num)
4339
))
4440

41+
# When there isn't an error while updating the message, we won't
42+
# attempt to send the it again.
43+
if result is None or result.get('result') != 'error':
44+
return
45+
46+
message_info = bot_handler.send_reply(message, str(num))
47+
if message_info is not None:
48+
storage.put('message_id', message_info['id'])
49+
4550

4651
handler_class = IncrementorHandler

zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ def test_bot(self) -> None:
2929
for item in m.call_args_list
3030
]
3131
self.assertEqual(content_updates, ['2', '3', '4'])
32+
33+
def test_bot_edit_timeout(self) -> None:
34+
bot = get_bot_message_handler(self.bot_name)
35+
bot_handler = StubBotHandler()
36+
37+
message = dict(type='stream')
38+
39+
bot.initialize(bot_handler)
40+
bot.handle_message(message, bot_handler)
41+
42+
error_msg = dict(msg='The time limit for editing this message has passed', result='error')
43+
with patch('zulip_bots.test_lib.StubBotHandler.update_message', return_value=error_msg):
44+
with patch('zulip_bots.simple_lib.SimpleMessageServer.send') as m:
45+
bot.handle_message(message, bot_handler)
46+
bot.handle_message(message, bot_handler)
47+
48+
# When there is an error, the bot should resend the message with the new value.
49+
self.assertEqual(m.call_count, 2)
50+
51+
content_updates = [
52+
item[0][0]['content']
53+
for item in m.call_args_list
54+
]
55+
self.assertEqual(content_updates, ['2', '3'])

0 commit comments

Comments
 (0)