Skip to content

Commit

Permalink
Remove async-files from IncomingMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimond committed May 19, 2022
1 parent c5c29c3 commit d822e0b
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ async def echo_file_handler(message: IncomingMessage, bot: Bot) -> None:
await bot.answer_message("Attached file is required")
return

await bot.answer_message("", file=attached_file) # type: ignore
await bot.answer_message("", file=attached_file)
```


Expand Down
1 change: 0 additions & 1 deletion pybotx/client/events_api/edit_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def from_domain(
) -> "BotXAPIEditEventRequestPayload":
api_file: MissingOptional[BotXAPIAttachment] = Undefined
if file:
assert not file.is_async_file, "async_files not supported"
api_file = BotXAPIAttachment.from_file_attachment(file)
elif file is None:
api_file = None
Expand Down
1 change: 0 additions & 1 deletion pybotx/client/events_api/reply_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def from_domain(
) -> "BotXAPIReplyEventRequestPayload":
api_file: Missing[BotXAPIAttachment] = Undefined
if file:
assert not file.is_async_file, "async_files not supported"
api_file = BotXAPIAttachment.from_file_attachment(file)

body, mentions = find_and_replace_embed_mentions(body)
Expand Down
1 change: 0 additions & 1 deletion pybotx/client/notifications_api/direct_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def from_domain(
) -> "BotXAPIDirectNotificationRequestPayload":
api_file: Missing[BotXAPIAttachment] = Undefined
if file:
assert not file.is_async_file, "async_files not supported"
api_file = BotXAPIAttachment.from_file_attachment(file)

if len(body) > MAX_NOTIFICATION_BODY_LENGTH:
Expand Down
11 changes: 3 additions & 8 deletions pybotx/models/message/incoming_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pydantic import Field

from pybotx.logger import logger
from pybotx.models.async_files import APIAsyncFile, File, convert_async_file_to_domain
from pybotx.models.attachments import (
AttachmentContact,
AttachmentLink,
Expand Down Expand Up @@ -92,7 +91,7 @@ class IncomingMessage(BotCommandBase):
mentions: MentionList = field(default_factory=MentionList)
forward: Optional[Forward] = None
reply: Optional[Reply] = None
file: Optional[Union[File, IncomingFileAttachment]] = None
file: Optional[IncomingFileAttachment] = None
location: Optional[AttachmentLocation] = None
contact: Optional[AttachmentContact] = None
link: Optional[AttachmentLink] = None
Expand Down Expand Up @@ -198,7 +197,6 @@ class BotAPIIncomingMessage(BotAPIBaseCommand):

source_sync_id: Optional[UUID]
attachments: List[Union[BotAPIAttachment, Dict[str, Any]]] # noqa: WPS234
async_files: List[APIAsyncFile]
entities: List[Union[BotAPIEntity, Dict[str, Any]]] # noqa: WPS234

def to_domain(self, raw_command: Dict[str, Any]) -> IncomingMessage: # noqa: WPS231
Expand Down Expand Up @@ -241,16 +239,13 @@ def to_domain(self, raw_command: Dict[str, Any]) -> IncomingMessage: # noqa: WP
type=convert_chat_type_to_domain(self.sender.chat_type),
)

file: Optional[Union[File, IncomingFileAttachment]] = None
file: Optional[IncomingFileAttachment] = None
location: Optional[AttachmentLocation] = None
contact: Optional[AttachmentContact] = None
link: Optional[AttachmentLink] = None
sticker: Optional[Sticker] = None

if self.async_files:
# Always one async file per-message
file = convert_async_file_to_domain(self.async_files[0])
elif self.attachments:
if self.attachments:
# Always one attachment per-message
if isinstance(self.attachments[0], dict):
logger.warning("Received unknown attachment type")
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.40.0"
version = "0.40.1"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <nsidnev@ccsteam.ru>",
Expand Down
151 changes: 119 additions & 32 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
File,
HandlerCollector,
Image,
IncomingMessage,
Video,
Voice,
lifespan_wrapper,
)
from pybotx.models.system_events.smartapp_event import SmartAppEvent

pytestmark = [
pytest.mark.asyncio,
Expand Down Expand Up @@ -49,35 +49,77 @@ async def test__async_file__open(
),
)

payload = api_incoming_message_factory(
bot_id=bot_id,
async_file={
"type": "image",
"file": "https://link.to/file",
"file_mime_type": "image/png",
"file_name": "pass.png",
"file_preview": "https://link.to/preview",
"file_preview_height": 300,
"file_preview_width": 300,
"file_size": 1502345,
"file_hash": "Jd9r+OKpw5y+FSCg1xNTSUkwEo4nCW1Sn1AkotkOpH0=",
"file_encryption_algo": "stream",
"chunk_size": 2097152,
"file_id": "c3b9def2-b2c8-4732-b61f-99b9b110fa80",
payload = {
"sync_id": "a465f0f3-1354-491c-8f11-f400164295cb",
"command": {
"body": "system:smartapp_event",
"data": {
"ref": "6fafda2c-6505-57a5-a088-25ea5d1d0364",
"smartapp_id": "8dada2c8-67a6-4434-9dec-570d244e78ee",
"data": {
"type": "smartapp_rpc",
"method": "folders.get",
"params": {
"q": 1,
},
},
"opts": {"option": "test_option"},
"smartapp_api_version": 1,
},
"command_type": "system",
"metadata": {},
},
group_chat_id="054af49e-5e18-4dca-ad73-4f96b6de63fa",
host=host,
)
"async_files": [
{
"type": "image",
"file": "https://link.to/file",
"file_mime_type": "image/png",
"file_name": "pass.png",
"file_preview": "https://link.to/preview",
"file_preview_height": 300,
"file_preview_width": 300,
"file_size": 1502345,
"file_hash": "Jd9r+OKpw5y+FSCg1xNTSUkwEo4nCW1Sn1AkotkOpH0=",
"file_encryption_algo": "stream",
"chunk_size": 2097152,
"file_id": "c3b9def2-b2c8-4732-b61f-99b9b110fa80",
},
],
"attachments": [],
"entities": [],
"from": {
"user_huid": "b9197d3a-d855-5d34-ba8a-eff3a975ab20",
"group_chat_id": "054af49e-5e18-4dca-ad73-4f96b6de63fa",
"host": host,
"ad_login": None,
"ad_domain": None,
"username": None,
"chat_type": "group_chat",
"manufacturer": None,
"device": None,
"device_software": None,
"device_meta": {},
"platform": None,
"platform_package_id": None,
"is_admin": False,
"is_creator": False,
"app_version": None,
"locale": "en",
},
"bot_id": str(bot_id),
"proto_version": 4,
"source_sync_id": None,
}

collector = HandlerCollector()
read_content: Optional[bytes] = None

@collector.default_message_handler
async def default_handler(message: IncomingMessage, bot: Bot) -> None:
@collector.smartapp_event
async def smartapp_event_handler(event: SmartAppEvent, bot: Bot) -> None:
nonlocal read_content

assert message.file
async with message.file.open() as fo:
assert event.files
async with event.files[0].open() as fo:
read_content = await fo.read()

built_bot = Bot(collectors=[collector], bot_accounts=[bot_account])
Expand Down Expand Up @@ -214,17 +256,62 @@ async def test__async_execute_raw_bot_command__different_file_types(
bot_account: BotAccountWithSecret,
) -> None:
# - Arrange -
payload = api_incoming_message_factory(async_file=api_async_file)
payload = {
"sync_id": "a465f0f3-1354-491c-8f11-f400164295cb",
"command": {
"body": "system:smartapp_event",
"data": {
"ref": "6fafda2c-6505-57a5-a088-25ea5d1d0364",
"smartapp_id": "8dada2c8-67a6-4434-9dec-570d244e78ee",
"data": {
"type": "smartapp_rpc",
"method": "folders.get",
"params": {
"q": 1,
},
},
"opts": {"option": "test_option"},
"smartapp_api_version": 1,
},
"command_type": "system",
"metadata": {},
},
"async_files": [api_async_file],
"attachments": [],
"entities": [],
"from": {
"user_huid": "b9197d3a-d855-5d34-ba8a-eff3a975ab20",
"group_chat_id": "dea55ee4-7a9f-5da0-8c73-079f400ee517",
"host": "cts.example.com",
"ad_login": None,
"ad_domain": None,
"username": None,
"chat_type": "group_chat",
"manufacturer": None,
"device": None,
"device_software": None,
"device_meta": {},
"platform": None,
"platform_package_id": None,
"is_admin": False,
"is_creator": False,
"app_version": None,
"locale": "en",
},
"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46",
"proto_version": 4,
"source_sync_id": None,
}

collector = HandlerCollector()
incoming_message: Optional[IncomingMessage] = None
smartapp_event: Optional[SmartAppEvent] = None

@collector.default_message_handler
async def default_handler(message: IncomingMessage, bot: Bot) -> None:
nonlocal incoming_message
incoming_message = message
@collector.smartapp_event
async def smartapp_event_handler(event: SmartAppEvent, bot: Bot) -> None:
nonlocal smartapp_event
smartapp_event = event
# Drop `raw_command` from asserting
incoming_message.raw_command = None
smartapp_event.raw_command = None

built_bot = Bot(collectors=[collector], bot_accounts=[bot_account])

Expand All @@ -233,5 +320,5 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
bot.async_execute_raw_bot_command(payload)

# - Assert -
assert incoming_message
assert incoming_message.file == domain_async_file
assert smartapp_event
assert smartapp_event.files == [domain_async_file]
33 changes: 11 additions & 22 deletions tests/test_incoming_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ClientPlatforms,
Forward,
HandlerCollector,
Image,
IncomingMessage,
MentionBuilder,
MentionList,
Expand All @@ -23,6 +22,7 @@
UserSender,
lifespan_wrapper,
)
from pybotx.models.attachments import AttachmentImage

pytestmark = [
pytest.mark.asyncio,
Expand Down Expand Up @@ -138,21 +138,13 @@ async def test__async_execute_raw_bot_command__maximum_filled_incoming_message(
"data": {"message": "data"},
"metadata": {"message": "metadata"},
},
"attachments": [],
"async_files": [
"attachments": [
{
"data": {
"content": "data:image/jpg;base64,SGVsbG8sIHdvcmxkIQo=",
"file_name": "test_file.jpg",
},
"type": "image",
"file": "https://link.to/file",
"file_mime_type": "image/png",
"file_name": "pass.png",
"file_preview": "https://link.to/preview",
"file_preview_height": 300,
"file_preview_width": 300,
"file_size": 1502345,
"file_hash": "Jd9r+OKpw5y+FSCg1xNTSUkwEo4nCW1Sn1AkotkOpH0=",
"file_encryption_algo": "stream",
"chunk_size": 2097152,
"file_id": "8dada2c8-67a6-4434-9dec-570d244e78ee",
},
],
"entities": [
Expand Down Expand Up @@ -286,15 +278,12 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
type=ChatTypes.PERSONAL_CHAT,
),
raw_command=None,
file=Image(
file=AttachmentImage(
type=AttachmentTypes.IMAGE,
filename="pass.png",
size=1502345,
is_async_file=True,
_file_id=UUID("8dada2c8-67a6-4434-9dec-570d244e78ee"),
_file_url="https://link.to/file",
_file_mimetype="image/png",
_file_hash="Jd9r+OKpw5y+FSCg1xNTSUkwEo4nCW1Sn1AkotkOpH0=",
filename="test_file.jpg",
size=len(b"Hello, world!\n"),
is_async_file=False,
content=b"Hello, world!\n",
),
mentions=MentionList(
[
Expand Down

0 comments on commit d822e0b

Please sign in to comment.