Automatically cast Channel to InputChannel (similar to InputPeer)

This commit is contained in:
Lonami Exo
2017-07-07 09:48:06 +02:00
parent f88efa7f49
commit 95a989be2c
2 changed files with 32 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ from mimetypes import add_type, guess_extension
from .tl.types import (
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser,
MessageMediaDocument, MessageMediaPhoto, PeerChannel,
MessageMediaDocument, MessageMediaPhoto, PeerChannel, InputChannel,
PeerChat, PeerUser, User, UserFull, UserProfilePhoto)
@@ -52,7 +52,7 @@ def get_extension(media):
def get_input_peer(entity):
"""Gets the input peer for the given "entity" (user, chat or channel).
A ValueError is raised if the given entity isn't a supported type."""
if type(entity).subclass_of_id == 0xc91c90b6: # crc32('InputUser')
if type(entity).subclass_of_id == 0xc91c90b6: # crc32(b'InputPeer')
return entity
if isinstance(entity, User):
@@ -80,6 +80,19 @@ def get_input_peer(entity):
.format(type(entity).__name__))
def get_input_channel(entity):
"""Gets the input peer for the given "entity" (user, chat or channel).
A ValueError is raised if the given entity isn't a supported type."""
if type(entity).subclass_of_id == 0x40f202fd: # crc32(b'InputChannel')
return entity
if isinstance(entity, Channel) or isinstance(entity, ChannelForbidden):
return InputChannel(entity.id, entity.access_hash)
raise ValueError('Cannot cast {} to any kind of InputChannel.'
.format(type(entity).__name__))
def find_user_or_chat(peer, users, chats):
"""Finds the corresponding user or chat given a peer.
Returns None if it was not found"""