From 9a86447b6eca810d5c14211b154fc2ec3236cf58 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 24 Feb 2020 13:06:21 +0100 Subject: [PATCH] Fix get(_input)_users in ChatAction with no service msg --- telethon/events/chataction.py | 21 ++++++-- tests/telethon/crypto/__init__.py | 0 tests/telethon/events/__init__.py | 0 tests/telethon/events/test_chataction.py | 67 ++++++++++++++++++++++++ tests/telethon/extensions/__init__.py | 0 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 tests/telethon/crypto/__init__.py create mode 100644 tests/telethon/events/__init__.py create mode 100644 tests/telethon/events/test_chataction.py create mode 100644 tests/telethon/extensions/__init__.py diff --git a/telethon/events/chataction.py b/telethon/events/chataction.py index 7f9acb9d..7867d7e5 100644 --- a/telethon/events/chataction.py +++ b/telethon/events/chataction.py @@ -381,7 +381,8 @@ class ChatAction(EventBuilder): if not self._user_ids: return [] - if self._users is None or len(self._users) != len(self._user_ids): + # Note: we access the property first so that it fills if needed + if (self.users is None or len(self._users) != len(self._user_ids)) and self.action_message: await self.action_message._reload_message() self._users = [ u for u in self.action_message.action_entities @@ -397,19 +398,31 @@ class ChatAction(EventBuilder): if self._input_users is None and self._user_ids: self._input_users = [] for user_id in self._user_ids: + # First try to get it from our entities + try: + self._input_users.append(utils.get_input_peer(self._entities[user_id])) + continue + except (KeyError, TypeError) as e: + pass + + # If missing, try from the entity cache try: self._input_users.append(self._client._entity_cache[user_id]) + continue except KeyError: pass + return self._input_users or [] async def get_input_users(self): """ Returns `input_users` but will make an API call if necessary. """ - self._input_users = None - if self._input_users is None: - await self.action_message._reload_message() + if not self._user_ids: + return [] + + # Note: we access the property first so that it fills if needed + if (self.input_users is None or len(self._input_users) != len(self._user_ids)) and self.action_message: self._input_users = [ utils.get_input_peer(u) for u in self.action_message.action_entities diff --git a/tests/telethon/crypto/__init__.py b/tests/telethon/crypto/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/telethon/events/__init__.py b/tests/telethon/events/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/telethon/events/test_chataction.py b/tests/telethon/events/test_chataction.py new file mode 100644 index 00000000..24f60596 --- /dev/null +++ b/tests/telethon/events/test_chataction.py @@ -0,0 +1,67 @@ +import pytest + +from telethon import TelegramClient, events, types, utils + + +def get_client(): + return TelegramClient(None, 1, '1') + + +def get_user_456(): + return types.User( + id=456, + access_hash=789, + first_name='User 123' + ) + + +@pytest.mark.asyncio +async def test_get_input_users_no_action_message_no_entities(): + event = events.ChatAction.build(types.UpdateChatParticipantDelete( + chat_id=123, + user_id=456, + version=1 + )) + event._set_client(get_client()) + + assert await event.get_input_users() == [] + + +@pytest.mark.asyncio +async def test_get_input_users_no_action_message(): + user = get_user_456() + event = events.ChatAction.build(types.UpdateChatParticipantDelete( + chat_id=123, + user_id=456, + version=1 + )) + event._set_client(get_client()) + event._entities[user.id] = user + + assert await event.get_input_users() == [utils.get_input_peer(user)] + + +@pytest.mark.asyncio +async def test_get_users_no_action_message_no_entities(): + event = events.ChatAction.build(types.UpdateChatParticipantDelete( + chat_id=123, + user_id=456, + version=1 + )) + event._set_client(get_client()) + + assert await event.get_users() == [] + + +@pytest.mark.asyncio +async def test_get_users_no_action_message(): + user = get_user_456() + event = events.ChatAction.build(types.UpdateChatParticipantDelete( + chat_id=123, + user_id=456, + version=1 + )) + event._set_client(get_client()) + event._entities[user.id] = user + + assert await event.get_users() == [user] diff --git a/tests/telethon/extensions/__init__.py b/tests/telethon/extensions/__init__.py new file mode 100644 index 00000000..e69de29b