Skip to content

Commit

Permalink
feature: add delete event
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Sokolov committed Aug 16, 2024
1 parent e5237e5 commit cb67858
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pybotx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
from pybotx.models.system_events.cts_logout import CTSLogoutEvent
from pybotx.models.system_events.deleted_from_chat import DeletedFromChatEvent
from pybotx.models.system_events.event_edit import EventEdit
from pybotx.models.system_events.event_delete import EventDelete
from pybotx.models.system_events.internal_bot_notification import (
InternalBotNotificationEvent,
)
Expand Down Expand Up @@ -193,6 +194,7 @@
"Document",
"EditMessage",
"EventEdit",
"EventDelete",
"EventNotFoundError",
"File",
"FileDeletedError",
Expand Down
2 changes: 2 additions & 0 deletions pybotx/bot/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pybotx.models.system_events.cts_logout import CTSLogoutEvent
from pybotx.models.system_events.deleted_from_chat import DeletedFromChatEvent
from pybotx.models.system_events.event_edit import EventEdit
from pybotx.models.system_events.event_delete import EventDelete
from pybotx.models.system_events.internal_bot_notification import (
InternalBotNotificationEvent,
)
Expand Down Expand Up @@ -42,6 +43,7 @@
HandlerFunc[InternalBotNotificationEvent],
HandlerFunc[SmartAppEvent],
HandlerFunc[EventEdit],
HandlerFunc[EventDelete],
]

VisibleFunc = Callable[[StatusRecipient, "Bot"], Awaitable[bool]]
Expand Down
9 changes: 9 additions & 0 deletions pybotx/bot/handler_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from pybotx.models.system_events.cts_logout import CTSLogoutEvent
from pybotx.models.system_events.deleted_from_chat import DeletedFromChatEvent
from pybotx.models.system_events.event_edit import EventEdit
from pybotx.models.system_events.event_delete import EventDelete
from pybotx.models.system_events.internal_bot_notification import (
InternalBotNotificationEvent,
)
Expand Down Expand Up @@ -301,6 +302,14 @@ def event_edit(
self._system_event(EventEdit, handler_func)
return handler_func

def event_delete(
self,
handler_func: HandlerFunc[EventDelete],
) -> HandlerFunc[EventDelete]:
"""Decorate `event delete` event handler."""
self._system_event(EventDelete, handler_func)
return handler_func

def smartapp_event(
self,
handler_func: HandlerFunc[SmartAppEvent],
Expand Down
3 changes: 3 additions & 0 deletions pybotx/models/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
DeletedFromChatEvent,
)
from pybotx.models.system_events.event_edit import BotAPIEventEdit, EventEdit
from pybotx.models.system_events.event_delete import BotAPIEventDelete, EventDelete
from pybotx.models.system_events.internal_bot_notification import (
BotAPIInternalBotNotification,
InternalBotNotificationEvent,
Expand All @@ -45,6 +46,7 @@
BotAPICTSLogin,
BotAPICTSLogout,
BotAPIEventEdit,
BotAPIEventDelete
]
BotAPICommand = Union[BotAPIIncomingMessage, BotAPISystemEvent]

Expand All @@ -60,5 +62,6 @@
CTSLoginEvent,
CTSLogoutEvent,
EventEdit,
EventDelete
]
BotCommand = Union[IncomingMessage, SystemEvent]
1 change: 1 addition & 0 deletions pybotx/models/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class BotAPISystemEventTypes(StrEnum):
LEFT_FROM_CHAT = "system:left_from_chat"
SMARTAPP_EVENT = "system:smartapp_event"
EVENT_EDIT = "system:event_edit"
EVENT_DELETE = "system:event_delete"


class BotAPIClientPlatforms(Enum):
Expand Down
46 changes: 46 additions & 0 deletions pybotx/models/system_events/event_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from dataclasses import dataclass
from typing import Dict, Any, Literal
from uuid import UUID

from pydantic import Field

from pybotx.models.bot_account import BotAccount
from pybotx.models.api_base import VerifiedPayloadBaseModel
from pybotx.models.base_command import BotCommandBase, BaseBotAPIContext, BotAPIBaseCommand, \
BotAPIBaseSystemEventPayload
from pybotx.models.enums import BotAPISystemEventTypes


@dataclass
class EventDelete(BotCommandBase):
"""Event `system:event_delete`.
Attributes:
sync_id: ID of the deleted message.
"""

sync_id: UUID


class BotAPIEventDeleteData(VerifiedPayloadBaseModel):
sync_id: UUID


class BotAPIEventDeletePayload(BotAPIBaseSystemEventPayload):
body: Literal[BotAPISystemEventTypes.EVENT_DELETE]
data: BotAPIEventDeleteData


class BotAPIEventDelete(BotAPIBaseCommand):
payload: BotAPIEventDeletePayload = Field(..., alias="command")
sender: BaseBotAPIContext = Field(..., alias="from")

def to_domain(self, raw_command: Dict[str, Any]) -> EventDelete:
return EventDelete(
bot=BotAccount(
id=self.bot_id,
host=self.sender.host,
),
sync_id=self.sync_id,
raw_command=raw_command
)
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.70.0"
version = "0.71.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <nsidnev@ccsteam.ru>",
Expand Down
87 changes: 87 additions & 0 deletions tests/system_events/test_event_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from typing import Optional
from uuid import UUID

import pytest

from pybotx import (
Bot,
BotAccountWithSecret,
HandlerCollector,
lifespan_wrapper,
)
from pybotx.models.system_events.event_delete import EventDelete
from pybotx.models.bot_account import BotAccount

pytestmark = [
pytest.mark.asyncio,
pytest.mark.mock_authorization,
pytest.mark.usefixtures("respx_mock"),
]


async def test__chat_deleted_by_user__succeed(
bot_account: BotAccountWithSecret,
) -> None:
# - Arrange -
payload = {
"sync_id": "a465f0f3-1354-491c-8f11-f400164295cb",
"command": {
"body": "system:event_delete",
"data": {
"sync_id": "6fa5f1e9-1453-0ad7-2d6d-b791467e382a",
},
"command_type": "system",
"metadata": {},
},
"async_files": [],
"attachments": [],
"entities": [],
"from": {
"user_huid": None,
"group_chat_id": None,
"ad_login": None,
"ad_domain": None,
"username": None,
"chat_type": None,
"manufacturer": None,
"device": None,
"device_software": None,
"device_meta": {},
"platform": None,
"platform_package_id": None,
"is_admin": None,
"is_creator": None,
"app_version": None,
"locale": "en",
"host": "cts.example.com",
},
"bot_id": "24348246-6791-4ac0-9d86-b948cd6a0e46",
"proto_version": 4,
"source_sync_id": None,
}

collector = HandlerCollector()
event_deleted: Optional[EventDelete] = None

@collector.event_delete
async def event_delete_handler(event: EventDelete, bot: Bot) -> None:
nonlocal event_deleted
event_deleted = event
# Drop `raw_command` from asserting
event_deleted.raw_command = None

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

# - Act -
async with lifespan_wrapper(built_bot) as bot:
bot.async_execute_raw_bot_command(payload, verify_request=False)

# - Assert -
assert event_deleted == EventDelete(
sync_id=UUID("a465f0f3-1354-491c-8f11-f400164295cb"),
bot=BotAccount(
id=UUID("24348246-6791-4ac0-9d86-b948cd6a0e46"),
host="cts.example.com",
),
raw_command=None,
)

0 comments on commit cb67858

Please sign in to comment.