mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-27 01:34:29 +03:00
Reuse code for _get_entity_pair
Less error-prone, improved the function flow for all callers, and removed some duplicate work.
This commit is contained in:
parent
6d004601d0
commit
3a1496c205
|
@ -105,7 +105,8 @@ class CallbackQuery(EventBuilder):
|
||||||
|
|
||||||
def _set_client(self, client):
|
def _set_client(self, client):
|
||||||
super()._set_client(client)
|
super()._set_client(client)
|
||||||
self._sender, self._input_sender = self._get_entity_pair(self.sender_id)
|
self._sender, self._input_sender = utils._get_entity_pair(
|
||||||
|
self.sender_id, self._entities, client._entity_cache)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
|
|
|
@ -147,25 +147,11 @@ class EventCommon(ChatGetter, abc.ABC):
|
||||||
"""
|
"""
|
||||||
self._client = client
|
self._client = client
|
||||||
if self._chat_peer:
|
if self._chat_peer:
|
||||||
self._chat, self._input_chat = self._get_entity_pair(self.chat_id)
|
self._chat, self._input_chat = utils._get_entity_pair(
|
||||||
|
self.chat_id, self._entities, client._entity_cache)
|
||||||
else:
|
else:
|
||||||
self._chat = self._input_chat = None
|
self._chat = self._input_chat = None
|
||||||
|
|
||||||
def _get_entity_pair(self, entity_id):
|
|
||||||
"""
|
|
||||||
Returns ``(entity, input_entity)`` for the given entity ID.
|
|
||||||
"""
|
|
||||||
entity = self._entities.get(entity_id)
|
|
||||||
try:
|
|
||||||
input_entity = utils.get_input_peer(entity)
|
|
||||||
except TypeError:
|
|
||||||
try:
|
|
||||||
input_entity = self._client._entity_cache[entity_id]
|
|
||||||
except KeyError:
|
|
||||||
input_entity = None
|
|
||||||
|
|
||||||
return entity, input_entity
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def client(self):
|
def client(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -89,7 +89,8 @@ class InlineQuery(EventBuilder):
|
||||||
|
|
||||||
def _set_client(self, client):
|
def _set_client(self, client):
|
||||||
super()._set_client(client)
|
super()._set_client(client)
|
||||||
self._sender, self._input_sender = self._get_entity_pair(self.sender_id)
|
self._sender, self._input_sender = utils._get_entity_pair(
|
||||||
|
self.sender_id, self._entities, client._entity_cache)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
|
|
|
@ -206,16 +206,6 @@ class NewMessage(EventBuilder):
|
||||||
super()._set_client(client)
|
super()._set_client(client)
|
||||||
m = self.message
|
m = self.message
|
||||||
m._finish_init(client, self._entities, None)
|
m._finish_init(client, self._entities, None)
|
||||||
|
|
||||||
# TODO Duplicated work here
|
|
||||||
m._chat, m._input_chat = self._get_entity_pair(m.chat_id)
|
|
||||||
m._sender, m._input_sender = self._get_entity_pair(m.sender_id)
|
|
||||||
m._via_bot, m._via_input_bot = self._get_entity_pair(m.via_bot_id)
|
|
||||||
if m.forward:
|
|
||||||
f = m.forward
|
|
||||||
f._chat, f._input_chat = self._get_entity_pair(f.chat_id)
|
|
||||||
f._sender, f._input_sender = self._get_entity_pair(f.sender_id)
|
|
||||||
|
|
||||||
self.__dict__['_init'] = True # No new attributes can be set
|
self.__dict__['_init'] = True # No new attributes can be set
|
||||||
|
|
||||||
def __getattr__(self, item):
|
def __getattr__(self, item):
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from .common import EventBuilder, EventCommon, name_inner_event
|
from .common import EventBuilder, EventCommon, name_inner_event
|
||||||
|
from .. import utils
|
||||||
from ..tl import types
|
from ..tl import types
|
||||||
from ..tl.custom.sendergetter import SenderGetter
|
from ..tl.custom.sendergetter import SenderGetter
|
||||||
|
|
||||||
|
@ -184,7 +185,8 @@ class UserUpdate(EventBuilder):
|
||||||
self._chat_peer = types.PeerChannel(self._chat_peer)
|
self._chat_peer = types.PeerChannel(self._chat_peer)
|
||||||
|
|
||||||
super()._set_client(client)
|
super()._set_client(client)
|
||||||
self._sender, self._input_sender = self._get_entity_pair(self._sender_id)
|
self._sender, self._input_sender = utils._get_entity_pair(
|
||||||
|
self.sender_id, self._entities, client._entity_cache)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def user(self):
|
def user(self):
|
||||||
|
|
|
@ -29,25 +29,16 @@ class Forward(ChatGetter, SenderGetter):
|
||||||
self.original_fwd = original
|
self.original_fwd = original
|
||||||
|
|
||||||
self._sender_id = original.from_id
|
self._sender_id = original.from_id
|
||||||
self._sender = entities.get(original.from_id)
|
self._sender, self._input_sender = utils._get_entity_pair(
|
||||||
try:
|
original.from_id, entities, client._entity_cache)
|
||||||
self._input_sender =\
|
|
||||||
utils.get_input_peer(self._sender) if self._sender else None
|
|
||||||
except TypeError:
|
|
||||||
self._input_sender = None
|
|
||||||
|
|
||||||
self._broadcast = None
|
self._broadcast = None
|
||||||
if original.channel_id:
|
if original.channel_id:
|
||||||
self._chat_peer = types.PeerChannel(original.channel_id)
|
self._chat_peer = types.PeerChannel(original.channel_id)
|
||||||
self._chat = entities.get(utils.get_peer_id(self._chat_peer))
|
|
||||||
else:
|
|
||||||
self._chat_peer = None
|
|
||||||
self._chat = None
|
|
||||||
|
|
||||||
try:
|
self._chat, self._input_chat = utils._get_entity_pair(
|
||||||
self._input_chat = \
|
self.chat_id, entities, client._entity_cache)
|
||||||
utils.get_input_peer(self._chat) if self._chat else None
|
else:
|
||||||
except TypeError:
|
self._chat = self._input_chat = self._chat_peer = None
|
||||||
self._input_chat = None
|
|
||||||
|
|
||||||
# TODO We could reload the message
|
# TODO We could reload the message
|
||||||
|
|
|
@ -210,27 +210,20 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
|
||||||
known entities.
|
known entities.
|
||||||
"""
|
"""
|
||||||
self._client = client
|
self._client = client
|
||||||
self._sender = entities.get(self._sender_id)
|
cache = client._entity_cache
|
||||||
if self._sender:
|
|
||||||
try:
|
|
||||||
self._input_sender = utils.get_input_peer(self._sender)
|
|
||||||
except TypeError:
|
|
||||||
self._input_sender = None
|
|
||||||
|
|
||||||
self._chat = entities.get(self.chat_id)
|
self._sender, self._input_sender = utils._get_entity_pair(
|
||||||
self._input_chat = input_chat
|
self.sender_id, entities, cache)
|
||||||
if not self._input_chat and self._chat:
|
|
||||||
try:
|
|
||||||
self._input_chat = utils.get_input_peer(self._chat)
|
|
||||||
except TypeError:
|
|
||||||
self._input_chat = None
|
|
||||||
|
|
||||||
self._via_bot = entities.get(self.via_bot_id)
|
self._chat, self._input_chat = utils._get_entity_pair(
|
||||||
if self._via_bot:
|
self.chat_id, entities, cache)
|
||||||
try:
|
|
||||||
self._via_input_bot = utils.get_input_peer(self._via_bot)
|
if input_chat: # This has priority
|
||||||
except TypeError:
|
self._input_chat = input_chat
|
||||||
self._via_input_bot = None
|
|
||||||
|
if self.via_bot_id:
|
||||||
|
self._via_bot, self._via_input_bot = utils._get_entity_pair(
|
||||||
|
self.via_bot_id, entities, cache)
|
||||||
|
|
||||||
if self.fwd_from:
|
if self.fwd_from:
|
||||||
self._forward = Forward(self._client, self.fwd_from, entities)
|
self._forward = Forward(self._client, self.fwd_from, entities)
|
||||||
|
|
|
@ -469,6 +469,24 @@ def get_input_message(message):
|
||||||
_raise_cast_fail(message, 'InputMedia')
|
_raise_cast_fail(message, 'InputMedia')
|
||||||
|
|
||||||
|
|
||||||
|
def _get_entity_pair(entity_id, entities, cache,
|
||||||
|
get_input_peer=get_input_peer):
|
||||||
|
"""
|
||||||
|
Returns ``(entity, input_entity)`` for the given entity ID.
|
||||||
|
"""
|
||||||
|
entity = entities.get(entity_id)
|
||||||
|
try:
|
||||||
|
input_entity = cache[entity_id]
|
||||||
|
except KeyError:
|
||||||
|
# KeyError is unlikely, so another TypeError won't hurt
|
||||||
|
try:
|
||||||
|
input_entity = get_input_peer(entity)
|
||||||
|
except TypeError:
|
||||||
|
input_entity = None
|
||||||
|
|
||||||
|
return entity, input_entity
|
||||||
|
|
||||||
|
|
||||||
def get_message_id(message):
|
def get_message_id(message):
|
||||||
"""Similar to :meth:`get_input_peer`, but for message IDs."""
|
"""Similar to :meth:`get_input_peer`, but for message IDs."""
|
||||||
if message is None:
|
if message is None:
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
# Versions should comply with PEP440.
|
# Versions should comply with PEP440.
|
||||||
# This line is parsed in setup.py:
|
# This line is parsed in setup.py:
|
||||||
__version__ = '1.7.2'
|
__version__ = '1.7.3'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user