Skip to content

Commit

Permalink
fix: Types mentions. (#341)
Browse files Browse the repository at this point in the history
* fix: Types mentions.

* chore: Bump version to 0.34.0
  • Loading branch information
kutuzov13 committed Apr 11, 2022
1 parent b7aef2b commit b4cf390
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 129 deletions.
3 changes: 2 additions & 1 deletion pybotx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
UserSender,
)
from pybotx.models.message.markup import BubbleMarkup, Button, KeyboardMarkup
from pybotx.models.message.mentions import Mention, MentionList
from pybotx.models.message.mentions import Mention, MentionBuilder, MentionList
from pybotx.models.message.message_status import MessageStatus
from pybotx.models.message.outgoing_message import OutgoingMessage
from pybotx.models.message.reply import Reply
Expand Down Expand Up @@ -147,6 +147,7 @@
"KeyboardMarkup",
"LeftFromChatEvent",
"Mention",
"MentionBuilder",
"MentionList",
"MentionTypes",
"Middleware",
Expand Down
16 changes: 0 additions & 16 deletions pybotx/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,6 @@ def convert_client_platform_to_domain(
return converted_type


def convert_mention_type_to_domain(mention_type: BotAPIMentionTypes) -> MentionTypes:
mention_types_mapping = {
BotAPIMentionTypes.CONTACT: MentionTypes.CONTACT,
BotAPIMentionTypes.CHAT: MentionTypes.CHAT,
BotAPIMentionTypes.CHANNEL: MentionTypes.CHANNEL,
BotAPIMentionTypes.USER: MentionTypes.USER,
BotAPIMentionTypes.ALL: MentionTypes.ALL,
}

converted_type = mention_types_mapping.get(mention_type)
if converted_type is None:
raise NotImplementedError(f"Unsupported mention type: {mention_type}")

return converted_type


def convert_mention_type_from_domain(
mention_type: MentionTypes,
) -> BotAPIMentionTypes:
Expand Down
50 changes: 36 additions & 14 deletions pybotx/models/message/incoming_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
ClientPlatforms,
convert_chat_type_to_domain,
convert_client_platform_to_domain,
convert_mention_type_to_domain,
)
from pybotx.models.message.forward import BotAPIForward, Forward
from pybotx.models.message.mentions import (
BotAPIMention,
BotAPIMentionData,
BotAPINestedMentionData,
Mention,
MentionBuilder,
MentionList,
)
from pybotx.models.message.reply import BotAPIReply, Reply
Expand Down Expand Up @@ -117,18 +117,37 @@ def arguments(self) -> Tuple[str, ...]:


def _convert_bot_api_mention_to_domain(api_mention_data: BotAPIMentionData) -> Mention:
entity_id: Optional[UUID] = None
name: Optional[str] = None

if api_mention_data.mention_type != BotAPIMentionTypes.ALL:
mention_data = cast(BotAPINestedMentionData, api_mention_data.mention_data)
entity_id = mention_data.entity_id
name = mention_data.name

return Mention(
type=convert_mention_type_to_domain(api_mention_data.mention_type),
entity_id=entity_id,
name=name,
mention_data = cast(BotAPINestedMentionData, api_mention_data.mention_data)

if api_mention_data.mention_type == BotAPIMentionTypes.USER:
return MentionBuilder.user(
entity_id=mention_data.entity_id,
name=mention_data.name,
)

if api_mention_data.mention_type == BotAPIMentionTypes.CHAT:
return MentionBuilder.chat(
entity_id=mention_data.entity_id,
name=mention_data.name,
)

if api_mention_data.mention_type == BotAPIMentionTypes.CONTACT:
return MentionBuilder.contact(
entity_id=mention_data.entity_id,
name=mention_data.name,
)

if api_mention_data.mention_type == BotAPIMentionTypes.CHANNEL:
return MentionBuilder.channel(
entity_id=mention_data.entity_id,
name=mention_data.name,
)

if api_mention_data.mention_type == BotAPIMentionTypes.ALL:
return MentionBuilder.all()

raise NotImplementedError(
f"Unsupported mention type: {api_mention_data.mention_type}",
)


Expand Down Expand Up @@ -255,7 +274,10 @@ def to_domain(self, raw_command: Dict[str, Any]) -> IncomingMessage: # noqa: WP
logger.warning("Received unknown entity type")
else:
entity_domain = convert_bot_api_entity_to_domain(entity)
if isinstance(entity_domain, Mention):
if isinstance(
entity_domain,
Mention.__args__, # type: ignore [attr-defined] # noqa: WPS609
):
mentions.append(entity_domain)
elif isinstance(entity_domain, Forward):
# Max one forward per message
Expand Down
108 changes: 86 additions & 22 deletions pybotx/models/message/mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,117 @@
)


def build_embed_mention(
mention_type: MentionTypes,
entity_id: Optional[UUID] = None,
name: Optional[str] = None,
) -> str:
name = name or ""
entity_id_str = "" if entity_id is None else str(entity_id)
return f"<embed_mention>{mention_type.value}:{entity_id_str}:{name}</embed_mention>"


@dataclass
class BaseTargetMention:
entity_id: Optional[UUID]
name: Optional[str]


@dataclass
class MentionUser(BaseTargetMention):
type: Literal[MentionTypes.USER]

def __str__(self) -> str:
return build_embed_mention(self.type, self.entity_id, self.name)


@dataclass
class MentionContact(BaseTargetMention):
type: Literal[MentionTypes.CONTACT]

def __str__(self) -> str:
return build_embed_mention(self.type, self.entity_id, self.name)


@dataclass
class MentionChat(BaseTargetMention):
type: Literal[MentionTypes.CHAT]

def __str__(self) -> str:
return build_embed_mention(self.type, self.entity_id, self.name)


@dataclass
class MentionChannel(BaseTargetMention):
type: Literal[MentionTypes.CHANNEL]

def __str__(self) -> str:
return build_embed_mention(self.type, self.entity_id, self.name)


@dataclass
class Mention:
type: MentionTypes
entity_id: Optional[UUID] = None
name: Optional[str] = None
class MentionAll:
type: Literal[MentionTypes.ALL]

def __str__(self) -> str:
name = self.name or ""
entity_id = self.entity_id or ""
mention_type = self.type.value
return f"<embed_mention>{mention_type}:{entity_id}:{name}</embed_mention>"
return build_embed_mention(self.type)


Mention = Union[
MentionUser,
MentionContact,
MentionChat,
MentionChannel,
MentionAll,
]


class MentionBuilder:
@classmethod
def user(cls, huid: UUID, name: Optional[str] = None) -> "Mention":
return cls(
def user(cls, entity_id: Optional[UUID], name: Optional[str] = None) -> MentionUser:
return MentionUser(
type=MentionTypes.USER,
entity_id=huid,
entity_id=entity_id,
name=name,
)

@classmethod
def contact(cls, huid: UUID, name: Optional[str] = None) -> "Mention":
return cls(
def contact(
cls,
entity_id: Optional[UUID],
name: Optional[str] = None,
) -> MentionContact:
return MentionContact(
type=MentionTypes.CONTACT,
entity_id=huid,
entity_id=entity_id,
name=name,
)

@classmethod
def chat(cls, chat_id: UUID, name: Optional[str] = None) -> "Mention":
return cls(
def chat(cls, entity_id: Optional[UUID], name: Optional[str] = None) -> MentionChat:
return MentionChat(
type=MentionTypes.CHAT,
entity_id=chat_id,
entity_id=entity_id,
name=name,
)

@classmethod
def channel(cls, chat_id: UUID, name: Optional[str] = None) -> "Mention":
return cls(
def channel(
cls,
entity_id: Optional[UUID],
name: Optional[str] = None,
) -> MentionChannel:
return MentionChannel(
type=MentionTypes.CHANNEL,
entity_id=chat_id,
entity_id=entity_id,
name=name,
)

@classmethod
def all(cls) -> "Mention":
return cls(type=MentionTypes.ALL)
def all(cls) -> MentionAll:
return MentionAll(
type=MentionTypes.ALL,
)


class MentionList(List[Mention]):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybotx"
version = "0.33.0"
version = "0.34.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
6 changes: 3 additions & 3 deletions tests/client/events_api/test_edit_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
EditMessage,
HandlerCollector,
KeyboardMarkup,
Mention,
MentionBuilder,
OutgoingAttachment,
lifespan_wrapper,
)
Expand Down Expand Up @@ -146,7 +146,7 @@ async def test__edit_message__maximum_edit_succeed(
await bot.edit_message(
bot_id=bot_id,
sync_id=UUID("8ba66c5b-40bf-5c77-911d-519cb4e382e9"),
body=f"{Mention.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
body=f"{MentionBuilder.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
metadata={"message": "metadata"},
bubbles=bubbles,
keyboard=keyboard,
Expand Down Expand Up @@ -289,7 +289,7 @@ async def test__edit__succeed(
message = EditMessage(
bot_id=bot_id,
sync_id=UUID("8ba66c5b-40bf-5c77-911d-519cb4e382e9"),
body=f"{Mention.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
body=f"{MentionBuilder.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
metadata={"message": "metadata"},
bubbles=bubbles,
keyboard=keyboard,
Expand Down
6 changes: 3 additions & 3 deletions tests/client/events_api/test_reply_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
BubbleMarkup,
HandlerCollector,
KeyboardMarkup,
Mention,
MentionBuilder,
OutgoingAttachment,
ReplyMessage,
lifespan_wrapper,
Expand Down Expand Up @@ -162,7 +162,7 @@ async def test__reply_message__maximum_filled_reply_succeed(
await bot.reply_message(
bot_id=bot_id,
sync_id=UUID("8ba66c5b-40bf-5c77-911d-519cb4e382e9"),
body=f"{Mention.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
body=f"{MentionBuilder.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
metadata={"message": "metadata"},
bubbles=bubbles,
keyboard=keyboard,
Expand Down Expand Up @@ -270,7 +270,7 @@ async def test__reply__succeed(
message = ReplyMessage(
bot_id=bot_id,
sync_id=UUID("8ba66c5b-40bf-5c77-911d-519cb4e382e9"),
body=f"{Mention.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
body=f"{MentionBuilder.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!",
metadata={"message": "metadata"},
bubbles=bubbles,
keyboard=keyboard,
Expand Down
14 changes: 7 additions & 7 deletions tests/client/notifications_api/test_direct_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
HandlerCollector,
IncomingMessage,
KeyboardMarkup,
Mention,
MentionBuilder,
OutgoingAttachment,
OutgoingMessage,
StealthModeDisabledError,
Expand Down Expand Up @@ -622,7 +622,7 @@ async def test__send_message__maximum_filled_succeed(
lambda: UUID("f3e176d5-ff46-4b18-b260-25008338c06e"),
)

body = f"Hi, {Mention.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!"
body = f"Hi, {MentionBuilder.user(UUID('8f3abcc8-ba00-4c89-88e0-b786beb8ec24'))}!"
formatted_body = "Hi, @{mention:f3e176d5-ff46-4b18-b260-25008338c06e}!"

endpoint = respx_mock.post(
Expand Down Expand Up @@ -781,14 +781,14 @@ async def test__send_message__all_mentions_types_succeed(
)

mentioned_user_huid = UUID("8f3abcc8-ba00-4c89-88e0-b786beb8ec24")
user_mention = Mention.user(mentioned_user_huid)
user_mention = MentionBuilder.user(mentioned_user_huid)
mentioned_contact_huid = UUID("1e0529fd-f091-4be9-93cc-6704a8957432")
contact_mention = Mention.contact(mentioned_contact_huid)
contact_mention = MentionBuilder.contact(mentioned_contact_huid)
mentioned_chat_huid = UUID("454d73ad-1d32-4939-a708-e14b77414e86")
chat_mention = Mention.chat(mentioned_chat_huid, "Our chat")
chat_mention = MentionBuilder.chat(mentioned_chat_huid, "Our chat")
mentioned_channel_huid = UUID("78198bec-3285-48d0-9fe2-c0eb3afaffd7")
channel_mention = Mention.channel(mentioned_channel_huid)
all_mention = Mention.all()
channel_mention = MentionBuilder.channel(mentioned_channel_huid)
all_mention = MentionBuilder.all()

body = (
f"Hi, {user_mention}, want you to know, "
Expand Down
Loading

3 comments on commit b4cf390

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://6254243b0d4145663c1f07ee--pybotx.netlify.app

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.