2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Don't make use of Update objects when dispatching updates

The Update type is used nowhere, adds costly abstraction and makes the
code uglier. If I ever need it again (unlikely) I can just revert this.
This commit is contained in:
Dan 2018-10-17 20:59:33 +02:00
parent a315c30336
commit 426cdbbcb8

View File

@ -22,7 +22,6 @@ from collections import OrderedDict
from queue import Queue from queue import Queue
from threading import Thread from threading import Thread
import pyrogram
from pyrogram.api import types from pyrogram.api import types
from ..ext import utils from ..ext import utils
from ..handlers import CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler from ..handlers import CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler
@ -110,50 +109,26 @@ class Dispatcher:
continue continue
message = utils.parse_messages(self.client, update.message, users, chats) message = utils.parse_messages(self.client, update.message, users, chats)
is_edited = isinstance(update, Dispatcher.EDIT_MESSAGE_UPDATES) update = message, MessageHandler
is_channel = message.chat.type == "channel"
update = pyrogram.Update(
message=message if not is_channel and not is_edited else None,
edited_message=message if not is_channel and is_edited else None,
channel_post=message if is_channel and not is_edited else None,
edited_channel_post=message if is_channel and is_edited else None
)
elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES): elif isinstance(update, Dispatcher.DELETE_MESSAGE_UPDATES):
is_channel = hasattr(update, "channel_id") deleted_messages = utils.parse_deleted_messages(
messages = utils.parse_deleted_messages(
update.messages, update.messages,
update.channel_id if is_channel else None update.channel_id if hasattr(update, "channel_id") else None
) )
update = pyrogram.Update( update = deleted_messages, DeletedMessagesHandler
deleted_messages=messages if not is_channel else None,
deleted_channel_posts=messages if is_channel else None
)
elif isinstance(update, types.UpdateBotCallbackQuery): elif isinstance(update, types.UpdateBotCallbackQuery):
update = pyrogram.Update( update = utils.parse_callback_query(self.client, update, users), CallbackQueryHandler
callback_query=utils.parse_callback_query(
self.client, update, users
)
)
elif isinstance(update, types.UpdateInlineBotCallbackQuery): elif isinstance(update, types.UpdateInlineBotCallbackQuery):
update = pyrogram.Update( update = utils.parse_inline_callback_query(self.client, update, users), CallbackQueryHandler
callback_query=utils.parse_inline_callback_query(
self.client, update, users
)
)
elif isinstance(update, types.UpdateUserStatus): elif isinstance(update, types.UpdateUserStatus):
update = pyrogram.Update( update = utils.parse_user_status(update.status, update.user_id), UserStatusHandler
user_status=utils.parse_user_status(
update.status, update.user_id
)
)
else: else:
continue continue
self.dispatch(update) self.dispatch(*update)
except Exception as e: except Exception as e:
log.error(e, exc_info=True) log.error(e, exc_info=True)
@ -169,25 +144,10 @@ class Dispatcher:
log.error(e, exc_info=True) log.error(e, exc_info=True)
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
def dispatch(self, update): def dispatch(self, update, handler_class):
message = update.message or update.channel_post or update.edited_message or update.edited_channel_post
deleted_messages = update.deleted_channel_posts or update.deleted_messages
callback_query = update.callback_query
user_status = update.user_status
update = message or deleted_messages or callback_query or user_status
type = (
MessageHandler if message
else DeletedMessagesHandler if deleted_messages
else CallbackQueryHandler if callback_query
else UserStatusHandler if user_status
else None
)
for group in self.groups.values(): for group in self.groups.values():
for handler in group: for handler in group:
if isinstance(handler, type): if isinstance(handler, handler_class):
if handler.check(update): if handler.check(update):
try: try:
handler.callback(self.client, update) handler.callback(self.client, update)