Skip to content

Commit

Permalink
Update forum topic parsing in Message object
Browse files Browse the repository at this point in the history
  • Loading branch information
KurimuzonAkuma committed Dec 15, 2023
1 parent fd3858a commit 5f43892
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
11 changes: 1 addition & 10 deletions pyrogram/methods/chats/get_forum_topics_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import pyrogram
from pyrogram import raw
from pyrogram import types
from pyrogram import utils

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,17 +73,9 @@ async def get_forum_topics_by_id(
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
36 changes: 17 additions & 19 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,11 @@ async def _parse(

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

Expand Down Expand Up @@ -867,16 +866,13 @@ async def _parse(
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):
Expand Down Expand Up @@ -1040,13 +1036,6 @@ async def _parse(

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]
Expand Down Expand Up @@ -1107,6 +1096,15 @@ async def _parse(
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

Expand Down

0 comments on commit 5f43892

Please sign in to comment.