Skip to content

Commit

Permalink
fix MessageOrigin parser
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Sep 25, 2024
1 parent 9396526 commit 3f32729
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
15 changes: 2 additions & 13 deletions tgram/types/_external_reply_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .type_ import Type_

from typing import List, Optional
from tgram.utils import message_origin_parse


class ExternalReplyInfo(Type_):
Expand Down Expand Up @@ -147,19 +148,7 @@ def _parse(
ExternalReplyInfo(
me=me,
json=d,
origin=None
if not d.get("origin")
else tgram.types.MessageOriginUser._parse(me=me, d=d.get("origin"))
if d["origin"].get("sender_user")
else tgram.types.MessageOriginHiddenUser._parse(
me=me, d=d.get("origin")
)
if d["origin"].get("sender_user_name")
else tgram.types.MessageOriginChat._parse(me=me, d=d.get("origin"))
if d["origin"].get("sender_chat")
else tgram.types.MessageOriginChannel._parse(me=me, d=d.get("origin"))
if d["origin"].get("author_signature")
else None,
origin=message_origin_parse(d.get("origin"), me),
chat=tgram.types.Chat._parse(me=me, d=d.get("chat")),
message_id=d.get("message_id"),
link_preview_options=tgram.types.LinkPreviewOptions._parse(
Expand Down
22 changes: 2 additions & 20 deletions tgram/types/_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import List, Optional
from tgram import bound
from tgram.utils import String
from tgram.utils import String, message_origin_parse


class Message(Type_, bound.MessageB):
Expand Down Expand Up @@ -493,25 +493,7 @@ def _parse(
me=me, d=d.get("sender_business_bot")
),
business_connection_id=d.get("business_connection_id"),
forward_origin=None
if not d.get("forward_origin")
else tgram.types.MessageOriginUser._parse(
me=me, d=d.get("forward_origin")
)
if d["forward_origin"].get("sender_user")
else tgram.types.MessageOriginHiddenUser._parse(
me=me, d=d.get("forward_origin")
)
if d["forward_origin"].get("sender_user_name")
else tgram.types.MessageOriginChat._parse(
me=me, d=d.get("forward_origin")
)
if d["forward_origin"].get("sender_chat")
else tgram.types.MessageOriginChannel._parse(
me=me, d=d.get("forward_origin")
)
if d["forward_origin"].get("author_signature")
else None,
forward_origin=message_origin_parse(d.get("forward_origin"), me),
is_topic_message=d.get("is_topic_message"),
is_automatic_forward=d.get("is_automatic_forward"),
reply_to_message=tgram.types.Message._parse(
Expand Down
21 changes: 20 additions & 1 deletion tgram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import inspect

from pathlib import Path
from typing import List, Union, TypedDict
from typing import List, Union, TypedDict, Optional
from struct import unpack
from io import BytesIO
from json import dumps
Expand Down Expand Up @@ -376,6 +376,25 @@ async def compose(bots: List["tgram.TgBot"]):
return await asyncio.wait(tasks)


def message_origin_parse(
d: Optional[dict] = None, me: Optional["tgram.TgBot"] = None
) -> Optional["tgram.types.MessageOrigin"]:
if d is None:
return None

origin_type = d["type"]

return (
tgram.types.MessageOriginUser._parse(me=me, d=d)
if origin_type == "user"
else tgram.types.MessageOriginHiddenUser._parse(me=me, d=d)
if origin_type == "hidden_user"
else tgram.types.MessageOriginChat._parse(me=me, d=d)
if origin_type == "chat"
else tgram.types.MessageOriginChannel._parse(me=me, d=d)
)


def b64_decode(s: str) -> bytes:
return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))

Expand Down

0 comments on commit 3f32729

Please sign in to comment.