Skip to content

Commit

Permalink
Make get_input_* methods slightly smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jul 10, 2017
1 parent bdee94e commit 88c4cdf
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions telethon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

from .tl.types import (
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser,
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
MessageMediaDocument, MessageMediaPhoto, PeerChannel, InputChannel,
UserEmpty, InputUser, InputUserEmpty, InputUserSelf,
UserEmpty, InputUser, InputUserEmpty, InputUserSelf, InputPeerSelf,
PeerChat, PeerUser, User, UserFull, UserProfilePhoto)


Expand Down Expand Up @@ -57,7 +57,10 @@ def get_input_peer(entity):
return entity

if isinstance(entity, User):
return InputPeerUser(entity.id, entity.access_hash)
if entity.is_self:
return InputPeerSelf()
else:
return InputPeerUser(entity.id, entity.access_hash)

if any(isinstance(entity, c) for c in (
Chat, ChatEmpty, ChatForbidden)):
Expand All @@ -68,8 +71,14 @@ def get_input_peer(entity):
return InputPeerChannel(entity.id, entity.access_hash)

# Less common cases
if isinstance(entity, UserEmpty):
return InputPeerEmpty()

if isinstance(entity, InputUser):
return InputPeerUser(entity.user_id, entity.access_hash)

if isinstance(entity, UserFull):
return InputPeerUser(entity.user.id, entity.user.access_hash)
return get_input_peer(entity.user)

if isinstance(entity, ChatFull):
return InputPeerChat(entity.id)
Expand Down Expand Up @@ -110,6 +119,9 @@ def get_input_user(entity):
if isinstance(entity, UserFull):
return get_input_user(entity.user)

if isinstance(entity, InputPeerUser):
return InputUser(entity.user_id, entity.access_hash)

raise ValueError('Cannot cast {} to any kind of InputUser.'
.format(type(entity).__name__))

Expand Down

0 comments on commit 88c4cdf

Please sign in to comment.