Telethon/telethon/events/common.py

155 lines
5.0 KiB
Python
Raw Normal View History

2018-04-05 21:14:22 +03:00
import abc
import warnings
from .. import utils
2018-06-26 12:01:47 +03:00
from ..tl import TLObject, types
2018-07-10 16:15:22 +03:00
from ..tl.custom.chatgetter import ChatGetter
2018-04-05 21:14:22 +03:00
2018-06-13 17:20:15 +03:00
async def _into_id_set(client, chats):
2018-04-05 21:14:22 +03:00
"""Helper util to turn the input chat or chats into a set of IDs."""
if chats is None:
return None
if not utils.is_list_like(chats):
chats = (chats,)
result = set()
for chat in chats:
if isinstance(chat, int):
if chat < 0:
result.add(chat) # Explicitly marked IDs are negative
else:
result.update({ # Support all valid types of peers
utils.get_peer_id(types.PeerUser(chat)),
utils.get_peer_id(types.PeerChat(chat)),
utils.get_peer_id(types.PeerChannel(chat)),
})
elif isinstance(chat, TLObject) and chat.SUBCLASS_OF_ID == 0x2d45687:
# 0x2d45687 == crc32(b'Peer')
result.add(utils.get_peer_id(chat))
else:
2018-06-13 17:20:15 +03:00
chat = await client.get_input_entity(chat)
2018-04-05 21:14:22 +03:00
if isinstance(chat, types.InputPeerSelf):
2018-06-13 17:20:15 +03:00
chat = await client.get_me(input_peer=True)
2018-04-05 21:14:22 +03:00
result.add(utils.get_peer_id(chat))
return result
class EventBuilder(abc.ABC):
"""
The common event builder, with builtin support to filter per chat.
Args:
chats (`entity`, optional):
2018-07-15 12:31:14 +03:00
May be one or more entities (username/peer/etc.), preferably IDs.
By default, only matching chats will be handled.
2018-04-05 21:14:22 +03:00
blacklist_chats (`bool`, optional):
Whether to treat the chats as a blacklist instead of
as a whitelist (default). This means that every chat
will be handled *except* those specified in ``chats``
which will be ignored if ``blacklist_chats=True``.
"""
2018-07-11 12:22:43 +03:00
self_id = None
2018-04-05 21:14:22 +03:00
def __init__(self, chats=None, blacklist_chats=False):
self.chats = chats
self.blacklist_chats = blacklist_chats
self._self_id = None
2018-07-19 02:47:32 +03:00
@classmethod
2018-04-05 21:14:22 +03:00
@abc.abstractmethod
2018-07-19 02:47:32 +03:00
def build(cls, update):
2018-04-05 21:14:22 +03:00
"""Builds an event for the given update if possible, or returns None"""
2018-06-13 17:20:15 +03:00
async def resolve(self, client):
2018-04-05 21:14:22 +03:00
"""Helper method to allow event builders to be resolved before usage"""
2018-06-13 17:20:15 +03:00
self.chats = await _into_id_set(client, self.chats)
2018-07-11 12:22:43 +03:00
if not EventBuilder.self_id:
EventBuilder.self_id = await client.get_peer_id('me')
2018-04-05 21:14:22 +03:00
2018-07-11 12:22:43 +03:00
def filter(self, event):
2018-04-05 21:14:22 +03:00
"""
If the ID of ``event._chat_peer`` isn't in the chats set (or it is
but the set is a blacklist) returns ``None``, otherwise the event.
"""
if self.chats is not None:
inside = utils.get_peer_id(event._chat_peer) in self.chats
if inside == self.blacklist_chats:
# If this chat matches but it's a blacklist ignore.
# If it doesn't match but it's a whitelist ignore.
return None
return event
2018-07-10 16:15:22 +03:00
class EventCommon(ChatGetter, abc.ABC):
"""
Intermediate class with common things to all events.
2018-07-10 16:15:22 +03:00
Remember that this class implements `ChatGetter
<telethon.tl.custom.chatgetter.ChatGetter>` which
means you have access to all chat properties and methods.
In addition, you can access the `original_update`
field which contains the original :tl:`Update`.
"""
2018-04-05 21:14:22 +03:00
_event_name = 'Event'
def __init__(self, chat_peer=None, msg_id=None, broadcast=False):
self._entities = {}
self._client = None
self._chat_peer = chat_peer
self._message_id = msg_id
self._input_chat = None
self._chat = None
2018-07-10 16:15:22 +03:00
self._broadcast = broadcast
2018-04-28 13:58:41 +03:00
self.original_update = None
2018-04-05 21:14:22 +03:00
2018-05-31 14:30:22 +03:00
def _set_client(self, client):
"""
Setter so subclasses can act accordingly when the client is set.
"""
self._client = client
2018-07-10 16:15:22 +03:00
self._chat = self._entities.get(self.chat_id)
if not self._chat:
return
2018-05-31 14:30:22 +03:00
2018-07-10 16:15:22 +03:00
self._input_chat = utils.get_input_peer(self._chat)
if not getattr(self._input_chat, 'access_hash', True):
# getattr with True to handle the InputPeerSelf() case
2018-04-05 21:14:22 +03:00
try:
2018-07-10 16:15:22 +03:00
self._input_chat = self._client.session.get_input_entity(
self._chat_peer
)
except ValueError:
2018-07-10 16:15:22 +03:00
self._input_chat = None
2018-04-05 21:14:22 +03:00
@property
def client(self):
"""
The `telethon.TelegramClient` that created this event.
"""
2018-04-05 21:14:22 +03:00
return self._client
def __str__(self):
return TLObject.pretty_format(self.to_dict())
def stringify(self):
return TLObject.pretty_format(self.to_dict(), indent=0)
def to_dict(self):
d = {k: v for k, v in self.__dict__.items() if k[0] != '_'}
d['_'] = self._event_name
return d
def name_inner_event(cls):
"""Decorator to rename cls.Event 'Event' as 'cls.Event'"""
if hasattr(cls, 'Event'):
cls.Event._event_name = '{}.Event'.format(cls.__name__)
else:
warnings.warn('Class {} does not have a inner Event'.format(cls))
return cls