Fix get(_input)_users in ChatAction with no service msg

This commit is contained in:
Lonami Exo 2020-02-24 13:06:21 +01:00
parent 0814a20ec4
commit 9a86447b6e
5 changed files with 84 additions and 4 deletions

View File

@ -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

View File

View File

View File

@ -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]

View File