mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Automatically cast Channel to InputChannel (similar to InputPeer)
This commit is contained in:
parent
f88efa7f49
commit
95a989be2c
|
@ -7,7 +7,7 @@ from mimetypes import add_type, guess_extension
|
||||||
from .tl.types import (
|
from .tl.types import (
|
||||||
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
||||||
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser,
|
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser,
|
||||||
MessageMediaDocument, MessageMediaPhoto, PeerChannel,
|
MessageMediaDocument, MessageMediaPhoto, PeerChannel, InputChannel,
|
||||||
PeerChat, PeerUser, User, UserFull, UserProfilePhoto)
|
PeerChat, PeerUser, User, UserFull, UserProfilePhoto)
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ def get_extension(media):
|
||||||
def get_input_peer(entity):
|
def get_input_peer(entity):
|
||||||
"""Gets the input peer for the given "entity" (user, chat or channel).
|
"""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."""
|
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
|
return entity
|
||||||
|
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, User):
|
||||||
|
@ -80,6 +80,19 @@ def get_input_peer(entity):
|
||||||
.format(type(entity).__name__))
|
.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):
|
def find_user_or_chat(peer, users, chats):
|
||||||
"""Finds the corresponding user or chat given a peer.
|
"""Finds the corresponding user or chat given a peer.
|
||||||
Returns None if it was not found"""
|
Returns None if it was not found"""
|
||||||
|
|
|
@ -183,13 +183,18 @@ class TLGenerator:
|
||||||
builder.writeln('from {}.tl.mtproto_request import MTProtoRequest'
|
builder.writeln('from {}.tl.mtproto_request import MTProtoRequest'
|
||||||
.format('.' * depth))
|
.format('.' * depth))
|
||||||
|
|
||||||
if tlobject.is_function and \
|
if tlobject.is_function:
|
||||||
any(a for a in tlobject.args if a.type == 'InputPeer'):
|
if any(a for a in tlobject.args if a.type == 'InputPeer'):
|
||||||
# We can automatically convert a normal peer to an InputPeer,
|
# We can automatically convert a normal peer to an InputPeer,
|
||||||
# it will make invoking a lot of requests a lot simpler.
|
# it will make invoking a lot of requests a lot simpler.
|
||||||
builder.writeln('from {}.utils import get_input_peer'
|
builder.writeln('from {}.utils import get_input_peer'
|
||||||
.format('.' * depth))
|
.format('.' * depth))
|
||||||
|
|
||||||
|
if any(a for a in tlobject.args if a.type == 'InputChannel'):
|
||||||
|
# Same applies to channels
|
||||||
|
builder.writeln('from {}.utils import get_input_channel'
|
||||||
|
.format('.' * depth))
|
||||||
|
|
||||||
if any(a for a in tlobject.args if a.can_be_inferred):
|
if any(a for a in tlobject.args if a.can_be_inferred):
|
||||||
# Currently only 'random_id' needs 'os' to be imported
|
# Currently only 'random_id' needs 'os' to be imported
|
||||||
builder.writeln('import os')
|
builder.writeln('import os')
|
||||||
|
@ -316,7 +321,12 @@ class TLGenerator:
|
||||||
elif arg.type == 'InputPeer' and tlobject.is_function:
|
elif arg.type == 'InputPeer' and tlobject.is_function:
|
||||||
# Well-known case, auto-cast it to the right type
|
# Well-known case, auto-cast it to the right type
|
||||||
builder.writeln(
|
builder.writeln(
|
||||||
'self.{0} = get_input_peer({0})'.format(arg.name))
|
'self.{0} = get_input_peer({0})'.format(arg.name)
|
||||||
|
)
|
||||||
|
elif arg.type == 'InputChannel' and tlobject.is_function:
|
||||||
|
builder.writeln(
|
||||||
|
'self.{0} = get_input_channel({0})'.format(arg.name)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
builder.writeln('self.{0} = {0}'.format(arg.name))
|
builder.writeln('self.{0} = {0}'.format(arg.name))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user