Update forum topic parsing in Message object

This commit is contained in:
KurimuzonAkuma 2023-12-15 16:57:12 +03:00
parent fd3858a69f
commit 5f43892a44
2 changed files with 18 additions and 29 deletions

View File

@ -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

View File

@ -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