Allow auto-casting custom.Dialog into input_entity

This commit is contained in:
Lonami Exo 2018-04-06 19:11:31 +02:00
parent baa6976a0b
commit 0cd44b245c
2 changed files with 8 additions and 4 deletions

View File

@ -2463,9 +2463,10 @@ class TelegramClient(TelegramBareClient):
original_peer = peer
if not isinstance(peer, int):
try:
if peer.SUBCLASS_OF_ID != 0x2d45687: # crc32(b'Peer')
if getattr(peer, 'SUBCLASS_OF_ID', 0) != 0x2d45687:
# 0x2d45687 == crc32(b'Peer')
return utils.get_input_peer(peer)
except (AttributeError, TypeError):
except TypeError:
peer = None
if not peer:

View File

@ -91,7 +91,11 @@ def get_input_peer(entity, allow_self=True):
if entity.SUBCLASS_OF_ID == 0xc91c90b6: # crc32(b'InputPeer')
return entity
except AttributeError:
_raise_cast_fail(entity, 'InputPeer')
if hasattr(entity, 'input_entity'):
# e.g. custom.Dialog (can't cyclic import)
return entity.input_entity
else:
_raise_cast_fail(entity, 'InputPeer')
if isinstance(entity, User):
if entity.is_self and allow_self:
@ -105,7 +109,6 @@ def get_input_peer(entity, allow_self=True):
if isinstance(entity, (Channel, ChannelForbidden)):
return InputPeerChannel(entity.id, entity.access_hash or 0)
# Less common cases
if isinstance(entity, InputUser):
return InputPeerUser(entity.user_id, entity.access_hash)