mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-02-16 19:41:07 +03:00
Factor out setting entities to events
This commit is contained in:
parent
8c771a842f
commit
aa2b3daccc
|
@ -538,6 +538,7 @@ class EventBuilderDict:
|
|||
event = self.__dict__[builder] = builder.build(self.update)
|
||||
if isinstance(event, EventCommon):
|
||||
event.original_update = self.update
|
||||
event._entities = self.update._entities
|
||||
event._set_client(self.client)
|
||||
elif event:
|
||||
event._client = self.client
|
||||
|
|
|
@ -47,18 +47,13 @@ class CallbackQuery(EventBuilder):
|
|||
@classmethod
|
||||
def build(cls, update):
|
||||
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):
|
||||
# See https://github.com/LonamiWebs/Telethon/pull/1005
|
||||
# The long message ID is actually just msg_id + peer_id
|
||||
mid, pid = struct.unpack('<ii', struct.pack('<q', update.msg_id.id))
|
||||
peer = types.PeerChannel(-pid) if pid < 0 else types.PeerUser(pid)
|
||||
event = cls.Event(update, peer, mid)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(update, peer, mid)
|
||||
|
||||
def filter(self, event):
|
||||
# We can't call super().filter(...) because it ignores chat_instance
|
||||
|
|
|
@ -14,18 +14,18 @@ class ChatAction(EventBuilder):
|
|||
# Telegram does not always send
|
||||
# UpdateChannelPinnedMessage for new pins
|
||||
# but always for unpin, with update.id = 0
|
||||
event = cls.Event(types.PeerChannel(update.channel_id),
|
||||
unpin=True)
|
||||
return cls.Event(types.PeerChannel(update.channel_id),
|
||||
unpin=True)
|
||||
|
||||
elif isinstance(update, types.UpdateChatParticipantAdd):
|
||||
event = cls.Event(types.PeerChat(update.chat_id),
|
||||
added_by=update.inviter_id or True,
|
||||
users=update.user_id)
|
||||
return cls.Event(types.PeerChat(update.chat_id),
|
||||
added_by=update.inviter_id or True,
|
||||
users=update.user_id)
|
||||
|
||||
elif isinstance(update, types.UpdateChatParticipantDelete):
|
||||
event = cls.Event(types.PeerChat(update.chat_id),
|
||||
kicked_by=True,
|
||||
users=update.user_id)
|
||||
return cls.Event(types.PeerChat(update.chat_id),
|
||||
kicked_by=True,
|
||||
users=update.user_id)
|
||||
|
||||
elif (isinstance(update, (
|
||||
types.UpdateNewMessage, types.UpdateNewChannelMessage))
|
||||
|
@ -33,53 +33,46 @@ class ChatAction(EventBuilder):
|
|||
msg = update.message
|
||||
action = update.message.action
|
||||
if isinstance(action, types.MessageActionChatJoinedByLink):
|
||||
event = cls.Event(msg,
|
||||
added_by=True,
|
||||
users=msg.from_id)
|
||||
return cls.Event(msg,
|
||||
added_by=True,
|
||||
users=msg.from_id)
|
||||
elif isinstance(action, types.MessageActionChatAddUser):
|
||||
# If a user adds itself, it means they joined
|
||||
added_by = ([msg.from_id] == action.users) or msg.from_id
|
||||
event = cls.Event(msg,
|
||||
added_by=added_by,
|
||||
users=action.users)
|
||||
return cls.Event(msg,
|
||||
added_by=added_by,
|
||||
users=action.users)
|
||||
elif isinstance(action, types.MessageActionChatDeleteUser):
|
||||
event = cls.Event(msg,
|
||||
kicked_by=msg.from_id or True,
|
||||
users=action.user_id)
|
||||
return cls.Event(msg,
|
||||
kicked_by=msg.from_id or True,
|
||||
users=action.user_id)
|
||||
elif isinstance(action, types.MessageActionChatCreate):
|
||||
event = cls.Event(msg,
|
||||
users=action.users,
|
||||
created=True,
|
||||
new_title=action.title)
|
||||
return cls.Event(msg,
|
||||
users=action.users,
|
||||
created=True,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, types.MessageActionChannelCreate):
|
||||
event = cls.Event(msg,
|
||||
created=True,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
return cls.Event(msg,
|
||||
created=True,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, types.MessageActionChatEditTitle):
|
||||
event = cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_title=action.title)
|
||||
elif isinstance(action, types.MessageActionChatEditPhoto):
|
||||
event = cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=action.photo)
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=action.photo)
|
||||
elif isinstance(action, types.MessageActionChatDeletePhoto):
|
||||
event = cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=True)
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_photo=True)
|
||||
elif isinstance(action, types.MessageActionPinMessage):
|
||||
# Telegram always sends this service message for new pins
|
||||
event = cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_pin=msg.reply_to_msg_id)
|
||||
else:
|
||||
return
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(msg,
|
||||
users=msg.from_id,
|
||||
new_pin=msg.reply_to_msg_id)
|
||||
|
||||
class Event(EventCommon):
|
||||
"""
|
||||
|
|
|
@ -78,7 +78,12 @@ class EventBuilder(abc.ABC):
|
|||
@classmethod
|
||||
@abc.abstractmethod
|
||||
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):
|
||||
"""Helper method to allow event builders to be resolved before usage"""
|
||||
|
|
|
@ -48,12 +48,7 @@ class InlineQuery(EventBuilder):
|
|||
@classmethod
|
||||
def build(cls, update):
|
||||
if isinstance(update, types.UpdateBotInlineQuery):
|
||||
event = cls.Event(update)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(update)
|
||||
|
||||
def filter(self, event):
|
||||
if self.pattern:
|
||||
|
|
|
@ -27,20 +27,15 @@ class MessageDeleted(EventBuilder):
|
|||
@classmethod
|
||||
def build(cls, update):
|
||||
if isinstance(update, types.UpdateDeleteMessages):
|
||||
event = cls.Event(
|
||||
return cls.Event(
|
||||
deleted_ids=update.messages,
|
||||
peer=None
|
||||
)
|
||||
elif isinstance(update, types.UpdateDeleteChannelMessages):
|
||||
event = cls.Event(
|
||||
return cls.Event(
|
||||
deleted_ids=update.messages,
|
||||
peer=types.PeerChannel(update.channel_id)
|
||||
)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
|
||||
class Event(EventCommon):
|
||||
def __init__(self, deleted_ids, peer):
|
||||
|
|
|
@ -36,12 +36,7 @@ class MessageEdited(NewMessage):
|
|||
def build(cls, update):
|
||||
if isinstance(update, (types.UpdateEditMessage,
|
||||
types.UpdateEditChannelMessage)):
|
||||
event = cls.Event(update.message)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(update.message)
|
||||
|
||||
class Event(NewMessage.Event):
|
||||
pass # Required if we want a different name for it
|
||||
|
|
|
@ -22,27 +22,22 @@ class MessageRead(EventBuilder):
|
|||
@classmethod
|
||||
def build(cls, update):
|
||||
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):
|
||||
event = cls.Event(update.peer, update.max_id, True)
|
||||
return cls.Event(update.peer, update.max_id, True)
|
||||
elif isinstance(update, types.UpdateReadChannelInbox):
|
||||
event = cls.Event(types.PeerChannel(update.channel_id),
|
||||
update.max_id, False)
|
||||
return cls.Event(types.PeerChannel(update.channel_id),
|
||||
update.max_id, False)
|
||||
elif isinstance(update, types.UpdateReadChannelOutbox):
|
||||
event = cls.Event(types.PeerChannel(update.channel_id),
|
||||
update.max_id, True)
|
||||
return cls.Event(types.PeerChannel(update.channel_id),
|
||||
update.max_id, True)
|
||||
elif isinstance(update, types.UpdateReadMessagesContents):
|
||||
event = cls.Event(message_ids=update.messages,
|
||||
contents=True)
|
||||
return cls.Event(message_ids=update.messages,
|
||||
contents=True)
|
||||
elif isinstance(update, types.UpdateChannelReadMessagesContents):
|
||||
event = cls.Event(types.PeerChannel(update.channel_id),
|
||||
message_ids=update.messages,
|
||||
contents=True)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(types.PeerChannel(update.channel_id),
|
||||
message_ids=update.messages,
|
||||
contents=True)
|
||||
|
||||
def filter(self, event):
|
||||
if self.inbox == event.outbox:
|
||||
|
|
|
@ -128,7 +128,6 @@ class NewMessage(EventBuilder):
|
|||
if ori.from_id == ori.to_id.user_id and not ori.fwd_from:
|
||||
event.message.out = True
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
|
||||
def filter(self, event):
|
||||
|
|
|
@ -14,21 +14,16 @@ class UserUpdate(EventBuilder):
|
|||
@classmethod
|
||||
def build(cls, update):
|
||||
if isinstance(update, types.UpdateUserStatus):
|
||||
event = cls.Event(update.user_id,
|
||||
status=update.status)
|
||||
return cls.Event(update.user_id,
|
||||
status=update.status)
|
||||
elif isinstance(update, types.UpdateChatUserTyping):
|
||||
# Unfortunately, we can't know whether `chat_id`'s type
|
||||
event = cls.Event(update.user_id,
|
||||
chat_id=update.chat_id,
|
||||
typing=update.action)
|
||||
return cls.Event(update.user_id,
|
||||
chat_id=update.chat_id,
|
||||
typing=update.action)
|
||||
elif isinstance(update, types.UpdateUserTyping):
|
||||
event = cls.Event(update.user_id,
|
||||
typing=update.action)
|
||||
else:
|
||||
return
|
||||
|
||||
event._entities = update._entities
|
||||
return event
|
||||
return cls.Event(update.user_id,
|
||||
typing=update.action)
|
||||
|
||||
class Event(EventCommon, SenderGetter):
|
||||
"""
|
||||
|
|
|
@ -551,6 +551,7 @@ class MTProtoSender:
|
|||
|
||||
self._log.debug('Handling update %s', message.obj.__class__.__name__)
|
||||
if self._update_callback:
|
||||
print(message.obj.stringify())
|
||||
self._update_callback(message.obj)
|
||||
|
||||
async def _handle_pong(self, message):
|
||||
|
|
Loading…
Reference in New Issue
Block a user