Merge pull request #316 from aiogram/dev-3.x-fix-text-decorations-2

Fix phone_number and bot_command entity types generation
This commit is contained in:
Alex Root Junior 2020-05-02 02:55:59 +03:00 committed by GitHub
commit 2553f5f19e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 13 deletions

View file

@ -26,7 +26,10 @@ class TextDecoration(ABC):
:param text:
:return:
"""
if entity.type in ("bold", "italic", "code", "underline", "strikethrough"):
if entity.type in {"bot_command", "url", "mention", "phone_number"}:
# This entities should not be changed
return text
if entity.type in {"bold", "italic", "code", "underline", "strikethrough"}:
return cast(str, getattr(self, entity.type)(value=text))
if entity.type == "pre":
return (
@ -34,17 +37,14 @@ class TextDecoration(ABC):
if entity.language
else self.pre(value=text)
)
elif entity.type == "text_mention":
if entity.type == "text_mention":
from aiogram.api.types import User
user = cast(User, entity.user)
return self.link(value=text, link=f"tg://user?id={user.id}")
elif entity.type == "mention":
return text
elif entity.type == "text_link":
if entity.type == "text_link":
return self.link(value=text, link=cast(str, entity.url))
elif entity.type == "url":
return text
return self.quote(text)
def unparse(self, text: str, entities: Optional[List[MessageEntity]] = None) -> str:

View file

@ -184,15 +184,29 @@ class TestTextDecoration:
html_decoration,
"@username",
[
MessageEntity(
type="mention", offset=0, length=9, url=None, user=None, language=None
),
MessageEntity(
type="bold", offset=0, length=9, url=None, user=None, language=None
),
MessageEntity(type="mention", offset=0, length=9),
MessageEntity(type="bold", offset=0, length=9),
],
"<b>@username</b>",
],
[
html_decoration,
"/command",
[
MessageEntity(type="bot_command", offset=0, length=8),
MessageEntity(type="bold", offset=0, length=8),
],
"<b>/command</b>",
],
[
html_decoration,
"+1-212-555-0123",
[
MessageEntity(type="phone_number", offset=0, length=15),
MessageEntity(type="bold", offset=0, length=15),
],
"<b>+1-212-555-0123</b>",
],
[
html_decoration,
"test te👍🏿st test",