diff --git a/telethon/events/callbackquery.py b/telethon/events/callbackquery.py index 6c94cc32..e33fdf8f 100644 --- a/telethon/events/callbackquery.py +++ b/telethon/events/callbackquery.py @@ -95,11 +95,9 @@ class CallbackQuery(EventBuilder): """ def __init__(self, query, peer, msg_id): super().__init__(peer, msg_id=msg_id) + SenderGetter.__init__(self, query.user_id) self.query = query self.data_match = None - self._sender_id = query.user_id - self._input_sender = None - self._sender = None self._message = None self._answered = False diff --git a/telethon/events/common.py b/telethon/events/common.py index 0a4e57f8..2f3af68e 100644 --- a/telethon/events/common.py +++ b/telethon/events/common.py @@ -134,11 +134,10 @@ class EventCommon(ChatGetter, abc.ABC): _event_name = 'Event' def __init__(self, chat_peer=None, msg_id=None, broadcast=False): + super().__init__(self, chat_peer, broadcast=broadcast) self._entities = {} self._client = None - self._chat_peer = chat_peer self._message_id = msg_id - self._broadcast = broadcast self.original_update = None def _set_client(self, client): diff --git a/telethon/events/inlinequery.py b/telethon/events/inlinequery.py index 7946ecb5..1ae3e00d 100644 --- a/telethon/events/inlinequery.py +++ b/telethon/events/inlinequery.py @@ -80,12 +80,10 @@ class InlineQuery(EventBuilder): """ def __init__(self, query): super().__init__(chat_peer=types.PeerUser(query.user_id)) + SenderGetter.__init__(self, query.user_id) self.query = query self.pattern_match = None self._answered = False - self._sender_id = query.user_id - self._input_sender = None - self._sender = None def _set_client(self, client): super()._set_client(client) diff --git a/telethon/events/userupdate.py b/telethon/events/userupdate.py index 0357f870..119af3d9 100644 --- a/telethon/events/userupdate.py +++ b/telethon/events/userupdate.py @@ -101,9 +101,7 @@ class UserUpdate(EventBuilder): # We need the client to actually figure out its type. super().__init__(chat_id) - self._sender_id = user_id - self._input_sender = None - self._sender = None + SenderGetter.__init__(self, user_id) self.online = None if status is None else \ isinstance(status, types.UserStatusOnline) diff --git a/telethon/tl/custom/chatgetter.py b/telethon/tl/custom/chatgetter.py index 791c156c..78d294db 100644 --- a/telethon/tl/custom/chatgetter.py +++ b/telethon/tl/custom/chatgetter.py @@ -9,14 +9,13 @@ class ChatGetter(abc.ABC): Helper base class that introduces the `chat`, `input_chat` and `chat_id` properties and `get_chat` and `get_input_chat` 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): - self._chat = self._input_chat = self._chat_peer = \ - self._client = self._broadcast = None + def __init__(self, chat_peer=None, *, input_chat=None, chat=None, broadcast=None): + self._chat_peer = chat_peer + self._input_chat = input_chat + self._chat = chat + self._broadcast = broadcast + self._client = None @property def chat(self): diff --git a/telethon/tl/custom/conversation.py b/telethon/tl/custom/conversation.py index 7a37efec..be11e51b 100644 --- a/telethon/tl/custom/conversation.py +++ b/telethon/tl/custom/conversation.py @@ -30,15 +30,13 @@ class Conversation(ChatGetter): def __init__(self, client, input_chat, *, timeout, total_timeout, max_messages, exclusive, replies_are_responses): + # This call resets the client + ChatGetter.__init__(self, input_chat=input_chat) + self._id = Conversation._id_counter Conversation._id_counter += 1 self._client = client - self._chat = None - self._input_chat = input_chat - self._chat_peer = None - self._broadcast = None - self._timeout = timeout self._total_timeout = total_timeout self._total_due = None diff --git a/telethon/tl/custom/forward.py b/telethon/tl/custom/forward.py index c72e85e1..ecccba98 100644 --- a/telethon/tl/custom/forward.py +++ b/telethon/tl/custom/forward.py @@ -28,17 +28,17 @@ class Forward(ChatGetter, SenderGetter): self._client = client self.original_fwd = original - self._sender_id = original.from_id - self._sender, self._input_sender = utils._get_entity_pair( + sender, input_sender = utils._get_entity_pair( original.from_id, entities, client._entity_cache) - self._broadcast = None - if original.channel_id: - self._chat_peer = types.PeerChannel(original.channel_id) - - self._chat, self._input_chat = utils._get_entity_pair( - self.chat_id, entities, client._entity_cache) + if not original.channel_id: + peer = chat = input_chat = None 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 diff --git a/telethon/tl/custom/message.py b/telethon/tl/custom/message.py index f5ce9ece..6526235c 100644 --- a/telethon/tl/custom/message.py +++ b/telethon/tl/custom/message.py @@ -184,27 +184,25 @@ class Message(ChatGetter, SenderGetter, TLObject, abc.ABC): self._buttons = None self._buttons_flat = None self._buttons_count = None - self._sender_id = from_id - self._sender = None - self._input_sender = None self._via_bot = None self._via_input_bot = None self._action_entities = None 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: self.out = not self.fwd_from # Patch out in our chat 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 - 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 def _finish_init(self, client, entities, input_chat): diff --git a/telethon/tl/custom/sendergetter.py b/telethon/tl/custom/sendergetter.py index 4195c225..f03c4f92 100644 --- a/telethon/tl/custom/sendergetter.py +++ b/telethon/tl/custom/sendergetter.py @@ -6,14 +6,12 @@ class SenderGetter(abc.ABC): Helper base class that introduces the `sender`, `input_sender` and `sender_id` properties and `get_sender` and `get_input_sender` 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): - self._sender = self._input_sender = self._sender_id = \ - self._client = None + def __init__(self, sender_id=None, *, sender=None, input_sender=None): + self._sender_id = sender_id + self._sender = sender + self._input_sender = input_sender + self._client = None @property def sender(self):