Pyrofork: Add reply_to_story field to Message

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-09-22 14:22:15 +07:00
parent fa5539a16d
commit db6cae4ae1
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
2 changed files with 53 additions and 13 deletions

View file

@ -124,6 +124,9 @@ class Message(Object, Update):
For replies, the original message. Note that the Message object in this field will not contain For replies, the original message. Note that the Message object in this field will not contain
further reply_to_message fields even if it itself is a reply. further reply_to_message fields even if it itself is a reply.
reply_to_story (:obj:`~pyrogram.types.Story`, *optional*):
For replies, the original story.
mentioned (``bool``, *optional*): mentioned (``bool``, *optional*):
The message contains a mention. The message contains a mention.
@ -378,6 +381,7 @@ class Message(Object, Update):
reply_to_story_user_id: int = None, reply_to_story_user_id: int = None,
reply_to_top_message_id: int = None, reply_to_top_message_id: int = None,
reply_to_message: "Message" = None, reply_to_message: "Message" = None,
reply_to_story: "types.Story" = None,
mentioned: bool = None, mentioned: bool = None,
empty: bool = None, empty: bool = None,
service: "enums.MessageServiceType" = None, service: "enums.MessageServiceType" = None,
@ -470,6 +474,7 @@ class Message(Object, Update):
self.reply_to_story_user_id = reply_to_story_user_id self.reply_to_story_user_id = reply_to_story_user_id
self.reply_to_top_message_id = reply_to_top_message_id self.reply_to_top_message_id = reply_to_top_message_id
self.reply_to_message = reply_to_message self.reply_to_message = reply_to_message
self.reply_to_story = reply_to_story
self.mentioned = mentioned self.mentioned = mentioned
self.empty = empty self.empty = empty
self.service = service self.service = service
@ -1003,20 +1008,31 @@ class Message(Object, Update):
parsed_message.reply_to_story_user_id = message.reply_to.user_id parsed_message.reply_to_story_user_id = message.reply_to.user_id
if replies: if replies:
try: if parsed_message.reply_to_message_id:
key = (parsed_message.chat.id, parsed_message.reply_to_message_id) try:
reply_to_message = client.message_cache[key] key = (parsed_message.chat.id, parsed_message.reply_to_message_id)
reply_to_message = client.message_cache[key]
if not reply_to_message: if not reply_to_message:
reply_to_message = await client.get_messages( reply_to_message = await client.get_messages(
parsed_message.chat.id, parsed_message.chat.id,
reply_to_message_ids=message.id, reply_to_message_ids=message.id,
replies=replies - 1 replies=replies - 1
)
if reply_to_message and not reply_to_message.forum_topic_created:
parsed_message.reply_to_message = reply_to_message
except MessageIdsEmpty:
pass
elif parsed_message.reply_to_story_id:
try:
reply_to_story = await client.get_stories(
parsed_message.reply_to_story_user_id,
parsed_message.reply_to_story_id
) )
if reply_to_message and not reply_to_message.forum_topic_created: except Exception:
parsed_message.reply_to_message = reply_to_message pass
except MessageIdsEmpty: else:
pass parsed_message.reply_to_story = reply_to_story
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

View file

@ -108,7 +108,13 @@ async def parse_messages(
messages_with_replies = { messages_with_replies = {
i.id: i.reply_to.reply_to_msg_id i.id: i.reply_to.reply_to_msg_id
for i in messages.messages for i in messages.messages
if not isinstance(i, raw.types.MessageEmpty) and i.reply_to if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyHeader)
}
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: if messages_with_replies:
@ -134,6 +140,24 @@ async def parse_messages(
if reply.id == reply_id: if reply.id == reply_id:
if not reply.forum_topic_created: if not reply.forum_topic_created:
message.reply_to_message = reply 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]
return types.List(parsed_messages) return types.List(parsed_messages)