Factor out setting entities to events

This commit is contained in:
Lonami Exo 2019-06-30 13:23:18 +02:00
parent 8c771a842f
commit aa2b3daccc
11 changed files with 69 additions and 100 deletions

View File

@ -538,6 +538,7 @@ class EventBuilderDict:
event = self.__dict__[builder] = builder.build(self.update) event = self.__dict__[builder] = builder.build(self.update)
if isinstance(event, EventCommon): if isinstance(event, EventCommon):
event.original_update = self.update event.original_update = self.update
event._entities = self.update._entities
event._set_client(self.client) event._set_client(self.client)
elif event: elif event:
event._client = self.client event._client = self.client

View File

@ -47,18 +47,13 @@ class CallbackQuery(EventBuilder):
@classmethod @classmethod
def build(cls, update): def build(cls, update):
if isinstance(update, types.UpdateBotCallbackQuery): if isinstance(update, types.UpdateBotCallbackQuery):
event = cls.Event(update, update.peer, update.msg_id) return cls.Event(update, update.peer, update.msg_id)
elif isinstance(update, types.UpdateInlineBotCallbackQuery): elif isinstance(update, types.UpdateInlineBotCallbackQuery):
# See https://github.com/LonamiWebs/Telethon/pull/1005 # See https://github.com/LonamiWebs/Telethon/pull/1005
# The long message ID is actually just msg_id + peer_id # The long message ID is actually just msg_id + peer_id
mid, pid = struct.unpack('<ii', struct.pack('<q', update.msg_id.id)) mid, pid = struct.unpack('<ii', struct.pack('<q', update.msg_id.id))
peer = types.PeerChannel(-pid) if pid < 0 else types.PeerUser(pid) peer = types.PeerChannel(-pid) if pid < 0 else types.PeerUser(pid)
event = cls.Event(update, peer, mid) return cls.Event(update, peer, mid)
else:
return
event._entities = update._entities
return event
def filter(self, event): def filter(self, event):
# We can't call super().filter(...) because it ignores chat_instance # We can't call super().filter(...) because it ignores chat_instance

View File

@ -14,18 +14,18 @@ class ChatAction(EventBuilder):
# Telegram does not always send # Telegram does not always send
# UpdateChannelPinnedMessage for new pins # UpdateChannelPinnedMessage for new pins
# but always for unpin, with update.id = 0 # but always for unpin, with update.id = 0
event = cls.Event(types.PeerChannel(update.channel_id), return cls.Event(types.PeerChannel(update.channel_id),
unpin=True) unpin=True)
elif isinstance(update, types.UpdateChatParticipantAdd): elif isinstance(update, types.UpdateChatParticipantAdd):
event = cls.Event(types.PeerChat(update.chat_id), return cls.Event(types.PeerChat(update.chat_id),
added_by=update.inviter_id or True, added_by=update.inviter_id or True,
users=update.user_id) users=update.user_id)
elif isinstance(update, types.UpdateChatParticipantDelete): elif isinstance(update, types.UpdateChatParticipantDelete):
event = cls.Event(types.PeerChat(update.chat_id), return cls.Event(types.PeerChat(update.chat_id),
kicked_by=True, kicked_by=True,
users=update.user_id) users=update.user_id)
elif (isinstance(update, ( elif (isinstance(update, (
types.UpdateNewMessage, types.UpdateNewChannelMessage)) types.UpdateNewMessage, types.UpdateNewChannelMessage))
@ -33,53 +33,46 @@ class ChatAction(EventBuilder):
msg = update.message msg = update.message
action = update.message.action action = update.message.action
if isinstance(action, types.MessageActionChatJoinedByLink): if isinstance(action, types.MessageActionChatJoinedByLink):
event = cls.Event(msg, return cls.Event(msg,
added_by=True, added_by=True,
users=msg.from_id) users=msg.from_id)
elif isinstance(action, types.MessageActionChatAddUser): elif isinstance(action, types.MessageActionChatAddUser):
# If a user adds itself, it means they joined # If a user adds itself, it means they joined
added_by = ([msg.from_id] == action.users) or msg.from_id added_by = ([msg.from_id] == action.users) or msg.from_id
event = cls.Event(msg, return cls.Event(msg,
added_by=added_by, added_by=added_by,
users=action.users) users=action.users)
elif isinstance(action, types.MessageActionChatDeleteUser): elif isinstance(action, types.MessageActionChatDeleteUser):
event = cls.Event(msg, return cls.Event(msg,
kicked_by=msg.from_id or True, kicked_by=msg.from_id or True,
users=action.user_id) users=action.user_id)
elif isinstance(action, types.MessageActionChatCreate): elif isinstance(action, types.MessageActionChatCreate):
event = cls.Event(msg, return cls.Event(msg,
users=action.users, users=action.users,
created=True, created=True,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChannelCreate): elif isinstance(action, types.MessageActionChannelCreate):
event = cls.Event(msg, return cls.Event(msg,
created=True, created=True,
users=msg.from_id, users=msg.from_id,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChatEditTitle): elif isinstance(action, types.MessageActionChatEditTitle):
event = cls.Event(msg, return cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_title=action.title) new_title=action.title)
elif isinstance(action, types.MessageActionChatEditPhoto): elif isinstance(action, types.MessageActionChatEditPhoto):
event = cls.Event(msg, return cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=action.photo) new_photo=action.photo)
elif isinstance(action, types.MessageActionChatDeletePhoto): elif isinstance(action, types.MessageActionChatDeletePhoto):
event = cls.Event(msg, return cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_photo=True) new_photo=True)
elif isinstance(action, types.MessageActionPinMessage): elif isinstance(action, types.MessageActionPinMessage):
# Telegram always sends this service message for new pins # Telegram always sends this service message for new pins
event = cls.Event(msg, return cls.Event(msg,
users=msg.from_id, users=msg.from_id,
new_pin=msg.reply_to_msg_id) new_pin=msg.reply_to_msg_id)
else:
return
else:
return
event._entities = update._entities
return event
class Event(EventCommon): class Event(EventCommon):
""" """

View File

@ -78,7 +78,12 @@ class EventBuilder(abc.ABC):
@classmethod @classmethod
@abc.abstractmethod @abc.abstractmethod
def build(cls, update): def build(cls, update):
"""Builds an event for the given update if possible, or returns None""" """
Builds an event for the given update if possible, or returns None.
`others` are the rest of updates that came in the same container
as the current `update`.
"""
async def resolve(self, client): async 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"""

View File

@ -48,12 +48,7 @@ class InlineQuery(EventBuilder):
@classmethod @classmethod
def build(cls, update): def build(cls, update):
if isinstance(update, types.UpdateBotInlineQuery): if isinstance(update, types.UpdateBotInlineQuery):
event = cls.Event(update) return cls.Event(update)
else:
return
event._entities = update._entities
return event
def filter(self, event): def filter(self, event):
if self.pattern: if self.pattern:

View File

@ -27,20 +27,15 @@ class MessageDeleted(EventBuilder):
@classmethod @classmethod
def build(cls, update): def build(cls, update):
if isinstance(update, types.UpdateDeleteMessages): if isinstance(update, types.UpdateDeleteMessages):
event = cls.Event( return cls.Event(
deleted_ids=update.messages, deleted_ids=update.messages,
peer=None peer=None
) )
elif isinstance(update, types.UpdateDeleteChannelMessages): elif isinstance(update, types.UpdateDeleteChannelMessages):
event = cls.Event( return cls.Event(
deleted_ids=update.messages, deleted_ids=update.messages,
peer=types.PeerChannel(update.channel_id) peer=types.PeerChannel(update.channel_id)
) )
else:
return
event._entities = update._entities
return event
class Event(EventCommon): class Event(EventCommon):
def __init__(self, deleted_ids, peer): def __init__(self, deleted_ids, peer):

View File

@ -36,12 +36,7 @@ class MessageEdited(NewMessage):
def build(cls, update): def build(cls, update):
if isinstance(update, (types.UpdateEditMessage, if isinstance(update, (types.UpdateEditMessage,
types.UpdateEditChannelMessage)): types.UpdateEditChannelMessage)):
event = cls.Event(update.message) return cls.Event(update.message)
else:
return
event._entities = update._entities
return event
class Event(NewMessage.Event): class Event(NewMessage.Event):
pass # Required if we want a different name for it pass # Required if we want a different name for it

View File

@ -22,27 +22,22 @@ class MessageRead(EventBuilder):
@classmethod @classmethod
def build(cls, update): def build(cls, update):
if isinstance(update, types.UpdateReadHistoryInbox): if isinstance(update, types.UpdateReadHistoryInbox):
event = cls.Event(update.peer, update.max_id, False) return cls.Event(update.peer, update.max_id, False)
elif isinstance(update, types.UpdateReadHistoryOutbox): elif isinstance(update, types.UpdateReadHistoryOutbox):
event = cls.Event(update.peer, update.max_id, True) return cls.Event(update.peer, update.max_id, True)
elif isinstance(update, types.UpdateReadChannelInbox): elif isinstance(update, types.UpdateReadChannelInbox):
event = cls.Event(types.PeerChannel(update.channel_id), return cls.Event(types.PeerChannel(update.channel_id),
update.max_id, False) update.max_id, False)
elif isinstance(update, types.UpdateReadChannelOutbox): elif isinstance(update, types.UpdateReadChannelOutbox):
event = cls.Event(types.PeerChannel(update.channel_id), return cls.Event(types.PeerChannel(update.channel_id),
update.max_id, True) update.max_id, True)
elif isinstance(update, types.UpdateReadMessagesContents): elif isinstance(update, types.UpdateReadMessagesContents):
event = cls.Event(message_ids=update.messages, return cls.Event(message_ids=update.messages,
contents=True) contents=True)
elif isinstance(update, types.UpdateChannelReadMessagesContents): elif isinstance(update, types.UpdateChannelReadMessagesContents):
event = cls.Event(types.PeerChannel(update.channel_id), return cls.Event(types.PeerChannel(update.channel_id),
message_ids=update.messages, message_ids=update.messages,
contents=True) contents=True)
else:
return
event._entities = update._entities
return event
def filter(self, event): def filter(self, event):
if self.inbox == event.outbox: if self.inbox == event.outbox:

View File

@ -128,7 +128,6 @@ class NewMessage(EventBuilder):
if ori.from_id == ori.to_id.user_id and not ori.fwd_from: if ori.from_id == ori.to_id.user_id and not ori.fwd_from:
event.message.out = True event.message.out = True
event._entities = update._entities
return event return event
def filter(self, event): def filter(self, event):

View File

@ -14,21 +14,16 @@ class UserUpdate(EventBuilder):
@classmethod @classmethod
def build(cls, update): def build(cls, update):
if isinstance(update, types.UpdateUserStatus): if isinstance(update, types.UpdateUserStatus):
event = cls.Event(update.user_id, return cls.Event(update.user_id,
status=update.status) status=update.status)
elif isinstance(update, types.UpdateChatUserTyping): elif isinstance(update, types.UpdateChatUserTyping):
# Unfortunately, we can't know whether `chat_id`'s type # Unfortunately, we can't know whether `chat_id`'s type
event = cls.Event(update.user_id, return cls.Event(update.user_id,
chat_id=update.chat_id, chat_id=update.chat_id,
typing=update.action) typing=update.action)
elif isinstance(update, types.UpdateUserTyping): elif isinstance(update, types.UpdateUserTyping):
event = cls.Event(update.user_id, return cls.Event(update.user_id,
typing=update.action) typing=update.action)
else:
return
event._entities = update._entities
return event
class Event(EventCommon, SenderGetter): class Event(EventCommon, SenderGetter):
""" """

View File

@ -551,6 +551,7 @@ class MTProtoSender:
self._log.debug('Handling update %s', message.obj.__class__.__name__) self._log.debug('Handling update %s', message.obj.__class__.__name__)
if self._update_callback: if self._update_callback:
print(message.obj.stringify())
self._update_callback(message.obj) self._update_callback(message.obj)
async def _handle_pong(self, message): async def _handle_pong(self, message):