Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch send: use classes without unnecessary/unknown fields #100

Merged
merged 1 commit into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mautrix/appservice/api/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from mautrix.types import (
JSON,
BatchID,
BatchSendEvent,
BatchSendResponse,
BatchSendStateEvent,
ContentURI,
EventContent,
EventID,
Expand All @@ -30,15 +32,13 @@
JoinRulesStateEventContent,
Member,
Membership,
MessageEvent,
PowerLevelStateEventContent,
PresenceState,
RoomAvatarStateEventContent,
RoomID,
RoomNameStateEventContent,
RoomPinnedEventsStateEventContent,
RoomTopicStateEventContent,
StateEvent,
StateEventContent,
UserID,
)
Expand Down Expand Up @@ -435,8 +435,8 @@ async def batch_send(
prev_event_id: EventID,
*,
batch_id: BatchID | None = None,
events: Iterable[MessageEvent],
state_events_at_start: Iterable[StateEvent] = None,
events: Iterable[BatchSendEvent],
state_events_at_start: Iterable[BatchSendStateEvent] = (),
) -> BatchSendResponse:
"""
Send a batch of historical events into a room. See `MSC2716`_ for more info.
Expand All @@ -459,7 +459,7 @@ async def batch_send(
All the event IDs generated, plus a batch ID that can be passed back to this method.
"""
path = Path.unstable["org.matrix.msc2716"].rooms[room_id].batch_send
query = {"prev_event_id": prev_event_id}
query: JSON = {"prev_event_id": prev_event_id}
if batch_id:
query["batch_id"] = batch_id
resp = await self.api.request(
Expand Down
4 changes: 4 additions & 0 deletions mautrix/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
BaseMessageEventContentFuncs,
BaseRoomEvent,
BaseUnsigned,
BatchSendEvent,
BatchSendStateEvent,
CallAnswerEventContent,
CallCandidate,
CallCandidatesEventContent,
Expand Down Expand Up @@ -214,6 +216,8 @@
"BaseMessageEventContentFuncs",
"BaseRoomEvent",
"BaseUnsigned",
"BatchSendEvent",
"BatchSendStateEvent",
"CallAnswerEventContent",
"CallCandidate",
"CallCandidatesEventContent",
Expand Down
1 change: 1 addition & 0 deletions mautrix/types/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
RoomTagInfo,
)
from .base import BaseEvent, BaseRoomEvent, BaseUnsigned, GenericEvent
from .batch import BatchSendEvent, BatchSendStateEvent
from .encrypted import (
EncryptedEvent,
EncryptedEventContent,
Expand Down
32 changes: 32 additions & 0 deletions mautrix/types/event/batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2022 Tulir Asokan, Sumner Evans
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Any

from attr import dataclass
import attr

from ..primitive import UserID
from ..util import SerializableAttrs
from .base import BaseEvent


@dataclass
class BatchSendEvent(BaseEvent, SerializableAttrs):
"""Base event class for events sent via a batch send request."""

sender: UserID
timestamp: int = attr.ib(metadata={"json": "origin_server_ts"})
content: Any


@dataclass
class BatchSendStateEvent(BatchSendEvent, SerializableAttrs):
"""
State events to be used as initial state events on batch send events. These never need to be
deserialized.
"""

state_key: str
4 changes: 2 additions & 2 deletions mautrix/types/event/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Any, Dict, List, Optional, Pattern, Union
from typing import Dict, List, Optional, Pattern, Union
from html import escape
import re

from attr import dataclass
import attr

from ..primitive import JSON, ContentURI, EventID
from ..util import ExtensibleEnum, Obj, Serializable, SerializableAttrs, deserializer, field
from ..util import ExtensibleEnum, Obj, SerializableAttrs, deserializer, field
from .base import BaseRoomEvent, BaseUnsigned

# region Message types
Expand Down