Skip to content

Commit

Permalink
pyrofork: Refactor Poll
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>
  • Loading branch information
wulan17 committed Nov 29, 2024
1 parent e9e5aa3 commit 895dcd7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
35 changes: 23 additions & 12 deletions pyrogram/types/messages_and_media/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,22 @@ async def _parse(
text=answer.text.text,
voter_count=voter_count,
data=answer.option,
entities=option_entities,
client=client
entities=option_entities
)
)

q_entities = [types.MessageEntity._parse(client, entity, {}) for entity in poll.question.entities] if poll.question.entities else []
question_entities = types.List(filter(lambda x: x is not None, q_entities))

recent_voters = []
for user in poll_results.recent_voters:
try:
voter = types.User._parse(client, users.get(user.user_id, None))
except Exception:
pass
else:
recent_voters.append(voter)

return Poll(
id=str(poll.id),
question=poll.question.text,
Expand All @@ -185,10 +193,7 @@ async def _parse(
] if poll_results.solution_entities else None,
open_period=poll.close_period,
close_date=utils.timestamp_to_datetime(poll.close_date),
recent_voters=[
await client.get_users(user.user_id)
for user in poll_results.recent_voters
] if poll_results.recent_voters else None,
recent_voters=recent_voters if len(recent_voters) > 0 else None,
client=client
)

Expand Down Expand Up @@ -217,11 +222,20 @@ async def _parse_update(
types.PollOption(
text="",
voter_count=result.voters,
data=result.option,
client=client
data=result.option
)
)

recent_voters = []
if update.results.recent_voters:
for user in update.results.recent_voters:
try:
voter = types.User._parse(client, users.get(user.user_id, None))
except Exception:
pass
else:
recent_voters.append(voter)

return Poll(
id=str(update.poll_id),
question="",
Expand All @@ -230,10 +244,7 @@ async def _parse_update(
is_closed=False,
chosen_option_id=chosen_option_id,
correct_option_id=correct_option_id,
recent_voters=[
types.User._parse(client, users.get(user.user_id, None))
for user in update.results.recent_voters
] if update.results.recent_voters else None,
recent_voters=recent_voters if len(recent_voters) > 0 else None,
client=client
)

Expand Down
6 changes: 2 additions & 4 deletions pyrogram/types/messages_and_media/poll_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ class PollOption(Object):
"""

def __init__(
self,
*,
client: "pyrogram.Client" = None,
self: "pyrogram.Client",
text: str,
voter_count: int = 0,
data: bytes = None,
entities: Optional[List["pyrogram.types.MessageEntity"]] = None,
):
super().__init__(client)
super().__init__(self)

self.text = text
self.voter_count = voter_count
Expand Down

0 comments on commit 895dcd7

Please sign in to comment.