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 import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram import types from pyrogram import types
from pyrogram import utils
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -74,17 +73,9 @@ class GetForumTopicsByID:
users = {i.id: i for i in r.users} users = {i.id: i for i in r.users}
chats = {i.id: i for i in r.chats} 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() topics = types.List()
for i in r.topics: 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 return topics if is_iterable else topics[0] if topics else None

View File

@ -785,8 +785,7 @@ class Message(Object, Update):
client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message
if message.reply_to: if message.reply_to and message.reply_to.forum_topic:
if message.reply_to.forum_topic:
if message.reply_to.reply_to_top_id: if message.reply_to.reply_to_top_id:
parsed_message.message_thread_id = message.reply_to.reply_to_top_id parsed_message.message_thread_id = message.reply_to.reply_to_top_id
else: else:
@ -867,16 +866,13 @@ class Message(Object, Update):
giveaway = types.Giveaway._parse(client, media, chats) giveaway = types.Giveaway._parse(client, media, chats)
media_type = enums.MessageMediaType.GIVEAWAY media_type = enums.MessageMediaType.GIVEAWAY
elif isinstance(media, raw.types.MessageMediaStory): 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: try:
story = await client.get_stories(utils.get_peer_id(media.peer), media.id) story = await client.get_stories(utils.get_peer_id(media.peer), media.id)
except BotMethodInvalid: except BotMethodInvalid:
pass
if not story:
story = await types.Story._parse(client, media, users, chats, media.peer) 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 media_type = enums.MessageMediaType.STORY
elif isinstance(media, raw.types.MessageMediaDocument): elif isinstance(media, raw.types.MessageMediaDocument):
@ -1040,13 +1036,6 @@ class Message(Object, Update):
if topics: if topics:
parsed_message.topic = types.ForumTopic._parse(client, topics[thread_id], users=users, chats=chats) 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: else:
if message.reply_to.quote: if message.reply_to.quote:
quote_entities = [types.MessageEntity._parse(client, entity, users) for entity in message.reply_to.quote_entities] 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: else:
parsed_message.reply_to_story = reply_to_story 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 if not parsed_message.poll: # Do not cache poll messages
client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message