From 5f43892a44e33873f1898d264d2a874f8818137d Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Fri, 15 Dec 2023 16:57:12 +0300 Subject: [PATCH] Update forum topic parsing in Message object --- .../methods/chats/get_forum_topics_by_id.py | 11 +----- pyrogram/types/messages_and_media/message.py | 36 +++++++++---------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/pyrogram/methods/chats/get_forum_topics_by_id.py b/pyrogram/methods/chats/get_forum_topics_by_id.py index ee79aad3..e47c2c00 100644 --- a/pyrogram/methods/chats/get_forum_topics_by_id.py +++ b/pyrogram/methods/chats/get_forum_topics_by_id.py @@ -22,7 +22,6 @@ from typing import Union, List, Iterable import pyrogram from pyrogram import raw from pyrogram import types -from pyrogram import utils log = logging.getLogger(__name__) @@ -74,17 +73,9 @@ class GetForumTopicsByID: users = {i.id: i for i in r.users} chats = {i.id: i for i in r.chats} - messages = {} - - for message in r.messages: - if isinstance(message, raw.types.MessageEmpty): - continue - - messages[message.id] = await types.Message._parse(self, message, users, chats) - topics = types.List() for i in r.topics: - topics.append(types.ForumTopic._parse(self, i, messages, users, chats)) + topics.append(types.ForumTopic._parse(self, i, users=users, chats=chats)) return topics if is_iterable else topics[0] if topics else None diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 510d861b..376bd5ae 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -785,12 +785,11 @@ class Message(Object, Update): client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message - if message.reply_to: - if message.reply_to.forum_topic: - if message.reply_to.reply_to_top_id: - parsed_message.message_thread_id = message.reply_to.reply_to_top_id - else: - parsed_message.message_thread_id = message.reply_to.reply_to_msg_id + if message.reply_to and message.reply_to.forum_topic: + if message.reply_to.reply_to_top_id: + parsed_message.message_thread_id = message.reply_to.reply_to_top_id + else: + parsed_message.message_thread_id = message.reply_to.reply_to_msg_id return parsed_message @@ -867,16 +866,13 @@ class Message(Object, Update): giveaway = types.Giveaway._parse(client, media, chats) media_type = enums.MessageMediaType.GIVEAWAY elif isinstance(media, raw.types.MessageMediaStory): - if not media.story: + if media.story: + story = await types.Story._parse(client, media.story, users, chats, media.peer) + else: try: story = await client.get_stories(utils.get_peer_id(media.peer), media.id) except BotMethodInvalid: - pass - - if not story: story = await types.Story._parse(client, media, users, chats, media.peer) - else: - story = await types.Story._parse(client, media.story, users, chats, media.peer) media_type = enums.MessageMediaType.STORY elif isinstance(media, raw.types.MessageMediaDocument): @@ -1040,13 +1036,6 @@ class Message(Object, Update): if topics: parsed_message.topic = types.ForumTopic._parse(client, topics[thread_id], users=users, chats=chats) - else: - try: - msg = await client.get_messages(parsed_message.chat.id, message.id, replies=0) - if msg.topic: - parsed_message.topic = msg.topic - except Exception: - pass else: if message.reply_to.quote: quote_entities = [types.MessageEntity._parse(client, entity, users) for entity in message.reply_to.quote_entities] @@ -1107,6 +1096,15 @@ class Message(Object, Update): else: parsed_message.reply_to_story = reply_to_story + if parsed_message.topic is None and parsed_message.chat.is_forum: + try: + parsed_message.topic = await client.get_forum_topics_by_id( + chat_id=parsed_message.chat.id, + topic_ids=parsed_message.message_thread_id or 1 + ) + except BotMethodInvalid: + pass + if not parsed_message.poll: # Do not cache poll messages client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message