Skip to content

Commit

Permalink
Add wrapper for beeper batch send endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 31, 2023
1 parent ea4229b commit 193acb7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
55 changes: 53 additions & 2 deletions mautrix/appservice/api/intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BatchSendEvent,
BatchSendResponse,
BatchSendStateEvent,
BeeperBatchSendResponse,
ContentURI,
EventContent,
EventID,
Expand All @@ -40,7 +41,6 @@
RoomNameStateEventContent,
RoomPinnedEventsStateEventContent,
RoomTopicStateEventContent,
SerializableAttrs,
StateEventContent,
UserID,
)
Expand Down Expand Up @@ -161,6 +161,8 @@ def user(
user_id: The Matrix ID of the user whose intent API to get.
token: The access token to use for the Matrix ID.
base_url: An optional URL to use for API requests.
as_token: Whether the provided token is actually another as_token
(meaning the ``user_id`` query parameter needs to be used).
Returns:
The IntentAPI for the given user.
Expand All @@ -187,7 +189,7 @@ async def set_presence(
Args:
presence: The online status of the user.
status: The status message.
ignore_cache: Whether or not to set presence even if the cache says the presence is
ignore_cache: Whether to set presence even if the cache says the presence is
already set to that value.
"""
await self.ensure_registered()
Expand Down Expand Up @@ -520,6 +522,9 @@ async def batch_send(
.. versionadded:: v0.12.5
.. deprecated:: v0.20.3
MSC2716 was abandoned by upstream and Beeper has forked the endpoint.
Args:
room_id: The room ID to send the events to.
prev_event_id: The anchor event. The batch will be inserted immediately after this event.
Expand Down Expand Up @@ -554,6 +559,52 @@ async def batch_send(
)
return BatchSendResponse.deserialize(resp)

async def beeper_batch_send(
self,
room_id: RoomID,
events: Iterable[BatchSendEvent],
*,
forward: bool = False,
forward_if_no_messages: bool = False,
send_notification: bool = False,
mark_read_by: UserID | None = None,
) -> BeeperBatchSendResponse:
"""
Send a batch of events into a room. Only for Beeper/hungryserv.
.. versionadded:: v0.20.3
Args:
room_id: The room ID to send the events to.
events: The events to send.
forward: Send events to the end of the room instead of the beginning
forward_if_no_messages: Send events to the end of the room, but only if there are no
messages in the room. If there are messages, send the new messages to the beginning.
send_notification: Send a push notification for the new messages.
Only applies when sending to the end of the room.
mark_read_by: Send a read receipt from the given user ID atomically.
Returns:
All the event IDs generated.
"""
body = {
"events": [evt.serialize() for evt in events],
}
if forward:
body["forward"] = forward
elif forward_if_no_messages:
body["forward_if_no_messages"] = forward_if_no_messages
if send_notification:
body["send_notification"] = send_notification
if mark_read_by:
body["mark_read_by"] = mark_read_by
resp = await self.api.request(
Method.POST,
Path.unstable["com.beeper.backfill"].rooms[room_id].batch_send,
content=body,
)
return BeeperBatchSendResponse.deserialize(resp)

async def beeper_delete_room(self, room_id: RoomID) -> None:
versions = await self.versions()
if not versions.supports("com.beeper.room_yeeting"):
Expand Down
1 change: 1 addition & 0 deletions mautrix/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
)
from .misc import (
BatchSendResponse,
BeeperBatchSendResponse,
DeviceLists,
DeviceOTKCount,
DirectoryPaginationToken,
Expand Down
5 changes: 5 additions & 0 deletions mautrix/types/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ class BatchSendResponse(SerializableAttrs):
batch_event_id: EventID
next_batch_id: BatchID
base_insertion_event_id: Optional[EventID] = None


@dataclass
class BeeperBatchSendResponse(SerializableAttrs):
event_ids: List[EventID]

0 comments on commit 193acb7

Please sign in to comment.