Skip to content

Commit a842f0c

Browse files
authored
Merge pull request #100 from mautrix/sumner/bri-2378
batch send: use classes without unnecessary/unknown fields
2 parents 211562f + 9e76661 commit a842f0c

File tree

5 files changed

+44
-7
lines changed

5 files changed

+44
-7
lines changed

mautrix/appservice/api/intent.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from mautrix.types import (
2222
JSON,
2323
BatchID,
24+
BatchSendEvent,
2425
BatchSendResponse,
26+
BatchSendStateEvent,
2527
ContentURI,
2628
EventContent,
2729
EventID,
@@ -30,15 +32,13 @@
3032
JoinRulesStateEventContent,
3133
Member,
3234
Membership,
33-
MessageEvent,
3435
PowerLevelStateEventContent,
3536
PresenceState,
3637
RoomAvatarStateEventContent,
3738
RoomID,
3839
RoomNameStateEventContent,
3940
RoomPinnedEventsStateEventContent,
4041
RoomTopicStateEventContent,
41-
StateEvent,
4242
StateEventContent,
4343
UserID,
4444
)
@@ -435,8 +435,8 @@ async def batch_send(
435435
prev_event_id: EventID,
436436
*,
437437
batch_id: BatchID | None = None,
438-
events: Iterable[MessageEvent],
439-
state_events_at_start: Iterable[StateEvent] = None,
438+
events: Iterable[BatchSendEvent],
439+
state_events_at_start: Iterable[BatchSendStateEvent] = (),
440440
) -> BatchSendResponse:
441441
"""
442442
Send a batch of historical events into a room. See `MSC2716`_ for more info.
@@ -459,7 +459,7 @@ async def batch_send(
459459
All the event IDs generated, plus a batch ID that can be passed back to this method.
460460
"""
461461
path = Path.unstable["org.matrix.msc2716"].rooms[room_id].batch_send
462-
query = {"prev_event_id": prev_event_id}
462+
query: JSON = {"prev_event_id": prev_event_id}
463463
if batch_id:
464464
query["batch_id"] = batch_id
465465
resp = await self.api.request(

mautrix/types/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
BaseMessageEventContentFuncs,
3535
BaseRoomEvent,
3636
BaseUnsigned,
37+
BatchSendEvent,
38+
BatchSendStateEvent,
3739
CallAnswerEventContent,
3840
CallCandidate,
3941
CallCandidatesEventContent,
@@ -214,6 +216,8 @@
214216
"BaseMessageEventContentFuncs",
215217
"BaseRoomEvent",
216218
"BaseUnsigned",
219+
"BatchSendEvent",
220+
"BatchSendStateEvent",
217221
"CallAnswerEventContent",
218222
"CallCandidate",
219223
"CallCandidatesEventContent",

mautrix/types/event/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
RoomTagInfo,
1111
)
1212
from .base import BaseEvent, BaseRoomEvent, BaseUnsigned, GenericEvent
13+
from .batch import BatchSendEvent, BatchSendStateEvent
1314
from .encrypted import (
1415
EncryptedEvent,
1516
EncryptedEventContent,

mautrix/types/event/batch.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2022 Tulir Asokan, Sumner Evans
2+
#
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
from typing import Any
7+
8+
from attr import dataclass
9+
import attr
10+
11+
from ..primitive import UserID
12+
from ..util import SerializableAttrs
13+
from .base import BaseEvent
14+
15+
16+
@dataclass
17+
class BatchSendEvent(BaseEvent, SerializableAttrs):
18+
"""Base event class for events sent via a batch send request."""
19+
20+
sender: UserID
21+
timestamp: int = attr.ib(metadata={"json": "origin_server_ts"})
22+
content: Any
23+
24+
25+
@dataclass
26+
class BatchSendStateEvent(BatchSendEvent, SerializableAttrs):
27+
"""
28+
State events to be used as initial state events on batch send events. These never need to be
29+
deserialized.
30+
"""
31+
32+
state_key: str

mautrix/types/event/message.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
# This Source Code Form is subject to the terms of the Mozilla Public
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6-
from typing import Any, Dict, List, Optional, Pattern, Union
6+
from typing import Dict, List, Optional, Pattern, Union
77
from html import escape
88
import re
99

1010
from attr import dataclass
1111
import attr
1212

1313
from ..primitive import JSON, ContentURI, EventID
14-
from ..util import ExtensibleEnum, Obj, Serializable, SerializableAttrs, deserializer, field
14+
from ..util import ExtensibleEnum, Obj, SerializableAttrs, deserializer, field
1515
from .base import BaseRoomEvent, BaseUnsigned
1616

1717
# region Message types

0 commit comments

Comments
 (0)