Stop using InputPeerSelf() on events and special case edit()

Used to fail on the chat with your own (where messages are
"incoming" instead outgoing). Now the ID of the chat and
sender are compared to achieve the same effect. Fixes #632.
This commit is contained in:
Lonami Exo 2018-02-22 21:01:18 +01:00
parent f13a7e4afd
commit 448a04a7c5

View File

@ -21,8 +21,7 @@ def _into_id_set(client, chats):
for chat in chats: for chat in chats:
chat = client.get_input_entity(chat) chat = client.get_input_entity(chat)
if isinstance(chat, types.InputPeerSelf): if isinstance(chat, types.InputPeerSelf):
chat = getattr(_into_id_set, 'me', None) or client.get_me() chat = client.get_me(input_peer=True)
_into_id_set.me = chat
result.add(utils.get_peer_id(chat)) result.add(utils.get_peer_id(chat))
return result return result
@ -43,6 +42,7 @@ class _EventBuilder(abc.ABC):
def __init__(self, chats=None, blacklist_chats=False): def __init__(self, chats=None, blacklist_chats=False):
self.chats = chats self.chats = chats
self.blacklist_chats = blacklist_chats self.blacklist_chats = blacklist_chats
self._self_id = None
@abc.abstractmethod @abc.abstractmethod
def build(self, update): def build(self, update):
@ -51,6 +51,7 @@ class _EventBuilder(abc.ABC):
def resolve(self, client): def resolve(self, client):
"""Helper method to allow event builders to be resolved before usage""" """Helper method to allow event builders to be resolved before usage"""
self.chats = _into_id_set(client, self.chats) self.chats = _into_id_set(client, self.chats)
self._self_id = client.get_me(input_peer=True).user_id
def _filter_event(self, event): def _filter_event(self, event):
""" """
@ -179,11 +180,6 @@ class NewMessage(_EventBuilder):
You can specify a regex-like string which will be matched You can specify a regex-like string which will be matched
against the message, a callable function that returns ``True`` against the message, a callable function that returns ``True``
if a message is acceptable, or a compiled regex pattern. if a message is acceptable, or a compiled regex pattern.
Notes:
The ``message.from_id`` might not only be an integer or ``None``,
but also ``InputPeerSelf()`` for short private messages (the API
would not return such thing, this is a custom modification).
""" """
def __init__(self, incoming=None, outgoing=None, def __init__(self, incoming=None, outgoing=None,
chats=None, blacklist_chats=False, pattern=None): chats=None, blacklist_chats=False, pattern=None):
@ -216,7 +212,7 @@ class NewMessage(_EventBuilder):
silent=update.silent, silent=update.silent,
id=update.id, id=update.id,
to_id=types.PeerUser(update.user_id), to_id=types.PeerUser(update.user_id),
from_id=types.InputPeerSelf() if update.out else update.user_id, from_id=self._self_id if update.out else update.user_id,
message=update.message, message=update.message,
date=update.date, date=update.date,
fwd_from=update.fwd_from, fwd_from=update.fwd_from,
@ -317,7 +313,11 @@ class NewMessage(_EventBuilder):
or the edited message otherwise. or the edited message otherwise.
""" """
if not self.message.out: if not self.message.out:
return None if not isinstance(self.message.to_id, types.PeerUser):
return None
me = self._client.get_me(input_peer=True)
if self.message.to_id.user_id != me.user_id:
return None
return self._client.edit_message(self.input_chat, return self._client.edit_message(self.input_chat,
self.message, self.message,