Call Chat/Sender Getter init methods

This commit is contained in:
Lonami Exo 2019-05-12 14:00:12 +02:00
parent fefd6f0e6d
commit 9730894a07
9 changed files with 35 additions and 49 deletions

View File

@ -95,11 +95,9 @@ class CallbackQuery(EventBuilder):
""" """
def __init__(self, query, peer, msg_id): def __init__(self, query, peer, msg_id):
super().__init__(peer, msg_id=msg_id) super().__init__(peer, msg_id=msg_id)
SenderGetter.__init__(self, query.user_id)
self.query = query self.query = query
self.data_match = None self.data_match = None
self._sender_id = query.user_id
self._input_sender = None
self._sender = None
self._message = None self._message = None
self._answered = False self._answered = False

View File

@ -134,11 +134,10 @@ class EventCommon(ChatGetter, abc.ABC):
_event_name = 'Event' _event_name = 'Event'
def __init__(self, chat_peer=None, msg_id=None, broadcast=False): def __init__(self, chat_peer=None, msg_id=None, broadcast=False):
super().__init__(self, chat_peer, broadcast=broadcast)
self._entities = {} self._entities = {}
self._client = None self._client = None
self._chat_peer = chat_peer
self._message_id = msg_id self._message_id = msg_id
self._broadcast = broadcast
self.original_update = None self.original_update = None
def _set_client(self, client): def _set_client(self, client):

View File

@ -80,12 +80,10 @@ class InlineQuery(EventBuilder):
""" """
def __init__(self, query): def __init__(self, query):
super().__init__(chat_peer=types.PeerUser(query.user_id)) super().__init__(chat_peer=types.PeerUser(query.user_id))
SenderGetter.__init__(self, query.user_id)
self.query = query self.query = query
self.pattern_match = None self.pattern_match = None
self._answered = False self._answered = False
self._sender_id = query.user_id
self._input_sender = None
self._sender = None
def _set_client(self, client): def _set_client(self, client):
super()._set_client(client) super()._set_client(client)

View File

@ -101,9 +101,7 @@ class UserUpdate(EventBuilder):
# We need the client to actually figure out its type. # We need the client to actually figure out its type.
super().__init__(chat_id) super().__init__(chat_id)
self._sender_id = user_id SenderGetter.__init__(self, user_id)
self._input_sender = None
self._sender = None
self.online = None if status is None else \ self.online = None if status is None else \
isinstance(status, types.UserStatusOnline) isinstance(status, types.UserStatusOnline)

View File

@ -9,14 +9,13 @@ class ChatGetter(abc.ABC):
Helper base class that introduces the `chat`, `input_chat` Helper base class that introduces the `chat`, `input_chat`
and `chat_id` properties and `get_chat` and `get_input_chat` and `chat_id` properties and `get_chat` and `get_input_chat`
methods. methods.
Subclasses **must** have the following private members: `_chat`,
`_input_chat`, `_chat_peer`, `_broadcast` and `_client`. As an end
user, you should not worry about this.
""" """
def __init__(self): def __init__(self, chat_peer=None, *, input_chat=None, chat=None, broadcast=None):
self._chat = self._input_chat = self._chat_peer = \ self._chat_peer = chat_peer
self._client = self._broadcast = None self._input_chat = input_chat
self._chat = chat
self._broadcast = broadcast
self._client = None
@property @property
def chat(self): def chat(self):

View File

@ -30,15 +30,13 @@ class Conversation(ChatGetter):
def __init__(self, client, input_chat, def __init__(self, client, input_chat,
*, timeout, total_timeout, max_messages, *, timeout, total_timeout, max_messages,
exclusive, replies_are_responses): exclusive, replies_are_responses):
# This call resets the client
ChatGetter.__init__(self, input_chat=input_chat)
self._id = Conversation._id_counter self._id = Conversation._id_counter
Conversation._id_counter += 1 Conversation._id_counter += 1
self._client = client self._client = client
self._chat = None
self._input_chat = input_chat
self._chat_peer = None
self._broadcast = None
self._timeout = timeout self._timeout = timeout
self._total_timeout = total_timeout self._total_timeout = total_timeout
self._total_due = None self._total_due = None

View File

@ -28,17 +28,17 @@ class Forward(ChatGetter, SenderGetter):
self._client = client self._client = client
self.original_fwd = original self.original_fwd = original
self._sender_id = original.from_id sender, input_sender = utils._get_entity_pair(
self._sender, self._input_sender = utils._get_entity_pair(
original.from_id, entities, client._entity_cache) original.from_id, entities, client._entity_cache)
self._broadcast = None if not original.channel_id:
if original.channel_id: peer = chat = input_chat = None
self._chat_peer = types.PeerChannel(original.channel_id)
self._chat, self._input_chat = utils._get_entity_pair(
self.chat_id, entities, client._entity_cache)
else: else:
self._chat = self._input_chat = self._chat_peer = None peer = types.PeerChannel(original.channel_id)
chat, input_chat = utils._get_entity_pair(
utils.get_peer_id(peer), entities, client._entity_cache)
ChatGetter.__init__(self, peer, chat=chat, input_chat=input_chat)
SenderGetter.__init__(self, original.from_id, sender=sender, input_sender=input_sender)
# TODO We could reload the message # TODO We could reload the message

View File

@ -184,27 +184,25 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC):
self._buttons = None self._buttons = None
self._buttons_flat = None self._buttons_flat = None
self._buttons_count = None self._buttons_count = None
self._sender_id = from_id
self._sender = None
self._input_sender = None
self._via_bot = None self._via_bot = None
self._via_input_bot = None self._via_input_bot = None
self._action_entities = None self._action_entities = None
if not out and isinstance(to_id, types.PeerUser): if not out and isinstance(to_id, types.PeerUser):
self._chat_peer = types.PeerUser(from_id) chat_peer = types.PeerUser(from_id)
if from_id == to_id.user_id: if from_id == to_id.user_id:
self.out = not self.fwd_from # Patch out in our chat self.out = not self.fwd_from # Patch out in our chat
else: else:
self._chat_peer = to_id chat_peer = to_id
if post and not from_id and self._chat_peer: # Note that these calls would reset the client
ChatGetter.__init__(self, chat_peer, broadcast=post)
SenderGetter.__init__(self, from_id)
if post and not from_id and chat_peer:
# If the message comes from a Channel, let the sender be it # If the message comes from a Channel, let the sender be it
self._sender_id = utils.get_peer_id(self._chat_peer) self._sender_id = utils.get_peer_id(chat_peer)
self._broadcast = post
self._chat = None
self._input_chat = None
self._forward = None self._forward = None
def _finish_init(self, client, entities, input_chat): def _finish_init(self, client, entities, input_chat):

View File

@ -6,14 +6,12 @@ class SenderGetter(abc.ABC):
Helper base class that introduces the `sender`, `input_sender` Helper base class that introduces the `sender`, `input_sender`
and `sender_id` properties and `get_sender` and `get_input_sender` and `sender_id` properties and `get_sender` and `get_input_sender`
methods. methods.
Subclasses **must** have the following private members: `_sender`,
`_input_sender`, `_sender_id` and `_client`. As an end user, you
should not worry about this.
""" """
def __init__(self): def __init__(self, sender_id=None, *, sender=None, input_sender=None):
self._sender = self._input_sender = self._sender_id = \ self._sender_id = sender_id
self._client = None self._sender = sender
self._input_sender = input_sender
self._client = None
@property @property
def sender(self): def sender(self):