Skip to content

Commit

Permalink
fix: Add attachments and entities in system event edit (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
depostnykh committed Jan 18, 2024
1 parent c5e44c9 commit 8543ea1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
31 changes: 29 additions & 2 deletions pybotx/models/system_events/event_edit.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from dataclasses import dataclass
from typing import Any, Dict, Literal, Optional
from typing import Any, Dict, List, Literal, Optional

from pydantic import Field

from pybotx.models.api_base import VerifiedPayloadBaseModel
from pybotx.models.attachments import (
BotAPIAttachment,
IncomingAttachment,
convert_api_attachment_to_domain,
)
from pybotx.models.base_command import (
BaseBotAPIContext,
BotAPIBaseCommand,
Expand All @@ -12,17 +17,26 @@
)
from pybotx.models.bot_account import BotAccount
from pybotx.models.enums import BotAPISystemEventTypes
from pybotx.models.message.incoming_message import (
BotAPIEntity,
Entity,
convert_bot_api_entity_to_domain,
)


@dataclass
class EventEdit(BotCommandBase):
"""Event `system:event_edit`.
Attributes:
body: updated message body.
body: Updated message body.
attachments: Attachments from updated message.
entities: Entities from updated message.
"""

body: Optional[str]
attachments: List[IncomingAttachment]
entities: List[Entity]


class BotAPIEventEditData(VerifiedPayloadBaseModel):
Expand All @@ -37,6 +51,8 @@ class BotAPIEventEditPayload(BotAPIBaseSystemEventPayload):
class BotAPIEventEdit(BotAPIBaseCommand):
payload: BotAPIEventEditPayload = Field(..., alias="command")
sender: BaseBotAPIContext = Field(..., alias="from")
attachments: List[BotAPIAttachment]
entities: List[BotAPIEntity]

def to_domain(self, raw_command: Dict[str, Any]) -> EventEdit:
return EventEdit(
Expand All @@ -46,4 +62,15 @@ def to_domain(self, raw_command: Dict[str, Any]) -> EventEdit:
),
raw_command=raw_command,
body=self.payload.data.body,
attachments=[
convert_api_attachment_to_domain(
api_attachment=attachment,
message_body=self.payload.body,
)
for attachment in self.attachments
],
entities=[
convert_bot_api_entity_to_domain(api_entity=entity)
for entity in self.entities
],
)
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.61.3"
version = "0.61.4"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <nsidnev@ccsteam.ru>",
Expand Down
47 changes: 44 additions & 3 deletions tests/system_events/test_event_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import pytest

from pybotx import (
AttachmentTypes,
Bot,
BotAccount,
BotAccountWithSecret,
EventEdit,
HandlerCollector,
MentionContact,
MentionTypes,
lifespan_wrapper,
)
from pybotx.models.attachments import AttachmentImage

pytestmark = [
pytest.mark.asyncio,
Expand All @@ -32,8 +36,29 @@ async def test__event_edit__succeed(
"metadata": {},
},
"async_files": [],
"attachments": [],
"entities": [],
"attachments": [
{
"data": {
"content": "data:image/jpg;base64,SGVsbG8sIHdvcmxkIQo=",
"file_name": "test_file.jpg",
},
"type": "image",
},
],
"entities": [
{
"type": "mention",
"data": {
"mention_type": "contact",
"mention_id": "c06a96fa-7881-0bb6-0e0b-0af72fe3683f",
"mention_data": {
"user_huid": "ab103983-6001-44e9-889e-d55feb295494",
"name": "Вася Иванов",
"conn_type": "cts",
},
},
},
],
"from": {
"user_huid": None,
"group_chat_id": None,
Expand Down Expand Up @@ -62,7 +87,7 @@ async def test__event_edit__succeed(
event_edit: Optional[EventEdit] = None

@collector.event_edit
async def event_edit_handler(event: EventEdit, bot: Bot) -> None:
async def event_edit_handler(event: EventEdit, _: Bot) -> None:
nonlocal event_edit
event_edit = event
# Drop `raw_command` from asserting
Expand All @@ -82,4 +107,20 @@ async def event_edit_handler(event: EventEdit, bot: Bot) -> None:
),
raw_command=None,
body="Edited",
attachments=[
AttachmentImage(
type=AttachmentTypes.IMAGE,
filename="test_file.jpg",
size=14,
is_async_file=False,
content=b"Hello, world!\n",
),
],
entities=[
MentionContact(
type=MentionTypes.CONTACT,
entity_id=UUID("ab103983-6001-44e9-889e-d55feb295494"),
name="Вася Иванов",
),
],
)

0 comments on commit 8543ea1

Please sign in to comment.