pyrofork: fix MESSAGE_IDS_EMPTY error on get_scheduled_messages method

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
wulan17 2025-05-15 23:01:20 +07:00
parent 0e0a8f7533
commit a08e10f5d7
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420
2 changed files with 67 additions and 54 deletions

View file

@ -78,6 +78,6 @@ class GetScheduledMessages:
r = await self.invoke(rpc, sleep_threshold=-1)
messages = await utils.parse_messages(self, r)
messages = await utils.parse_messages(self, r, is_scheduled=True)
return messages if is_iterable else messages[0] if messages else None

View file

@ -103,7 +103,8 @@ async def parse_messages(
client,
messages: "raw.types.messages.Messages",
replies: int = 1,
business_connection_id: str = None
business_connection_id: str = None,
is_scheduled: bool = False
) -> List["types.Message"]:
users = {i.id: i for i in messages.users}
chats = {i.id: i for i in messages.chats}
@ -120,64 +121,76 @@ async def parse_messages(
parsed_messages.append(await types.Message._parse(client, message, users, chats, topics, replies=0, business_connection_id=business_connection_id))
if replies:
messages_with_replies = {
i.id: i.reply_to.reply_to_msg_id
for i in messages.messages
if (
not isinstance(i, raw.types.MessageEmpty)
and i.reply_to
and isinstance(i.reply_to, raw.types.MessageReplyHeader)
and i.reply_to.reply_to_msg_id is not None
)
}
if not is_scheduled:
messages_with_replies = {
i.id: i.reply_to.reply_to_msg_id
for i in messages.messages
if (
not isinstance(i, raw.types.MessageEmpty)
and i.reply_to
and isinstance(i.reply_to, raw.types.MessageReplyHeader)
and i.reply_to.reply_to_msg_id is not None
)
}
message_reply_to_story = {
i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id}
for i in messages.messages
if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader)
}
message_reply_to_story = {
i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id}
for i in messages.messages
if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader)
}
if messages_with_replies:
# We need a chat id, but some messages might be empty (no chat attribute available)
# Scan until we find a message with a chat available (there must be one, because we are fetching replies)
for m in parsed_messages:
if m.chat:
chat_id = m.chat.id
break
else:
chat_id = 0
if messages_with_replies:
# We need a chat id, but some messages might be empty (no chat attribute available)
# Scan until we find a message with a chat available (there must be one, because we are fetching replies)
for m in parsed_messages:
if m.chat:
chat_id = m.chat.id
break
else:
chat_id = 0
reply_messages = await client.get_messages(
chat_id,
reply_to_message_ids=messages_with_replies.keys(),
replies=replies - 1
)
for message in parsed_messages:
reply_id = messages_with_replies.get(message.id, None)
for reply in reply_messages:
if reply.id == reply_id:
if not reply.forum_topic_created:
message.reply_to_message = reply
if message_reply_to_story:
for m in parsed_messages:
if m.chat:
chat_id = m.chat.id
break
else:
chat_id = 0
reply_messages = {}
for msg_id in message_reply_to_story.keys():
reply_messages[msg_id] = await client.get_stories(
message_reply_to_story[msg_id]['user_id'],
message_reply_to_story[msg_id]['story_id']
reply_messages = await client.get_messages(
chat_id,
reply_to_message_ids=messages_with_replies.keys(),
replies=replies - 1
)
for message in parsed_messages:
reply_id = messages_with_replies.get(message.id, None)
for reply in reply_messages:
if reply.id == reply_id:
if not reply.forum_topic_created:
message.reply_to_message = reply
if message_reply_to_story:
for m in parsed_messages:
if m.chat:
chat_id = m.chat.id
break
else:
chat_id = 0
reply_messages = {}
for msg_id in message_reply_to_story.keys():
reply_messages[msg_id] = await client.get_stories(
message_reply_to_story[msg_id]['user_id'],
message_reply_to_story[msg_id]['story_id']
)
for message in parsed_messages:
if message.id in reply_messages:
message.reply_to_story = reply_messages[message.id]
else:
for message in parsed_messages:
if message.id in reply_messages:
message.reply_to_story = reply_messages[message.id]
if (
message.reply_to_message_id
and not message.external_reply
):
message.reply_to_message = await client.get_messages(
message.chat.id,
message_ids=message.reply_to_message_id,
replies=replies - 1
)
return types.List(parsed_messages)