From 193acb7660e7650487906ec95fb1fb1ed44218da Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 1 Nov 2023 01:02:50 +0200 Subject: [PATCH] Add wrapper for beeper batch send endpoint --- mautrix/appservice/api/intent.py | 55 ++++++++++++++++++++++++++++++-- mautrix/types/__init__.py | 1 + mautrix/types/misc.py | 5 +++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mautrix/appservice/api/intent.py b/mautrix/appservice/api/intent.py index 1892743e..00990671 100644 --- a/mautrix/appservice/api/intent.py +++ b/mautrix/appservice/api/intent.py @@ -25,6 +25,7 @@ BatchSendEvent, BatchSendResponse, BatchSendStateEvent, + BeeperBatchSendResponse, ContentURI, EventContent, EventID, @@ -40,7 +41,6 @@ RoomNameStateEventContent, RoomPinnedEventsStateEventContent, RoomTopicStateEventContent, - SerializableAttrs, StateEventContent, UserID, ) @@ -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. @@ -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() @@ -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. @@ -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"): diff --git a/mautrix/types/__init__.py b/mautrix/types/__init__.py index 6d2ffa7c..42b9068c 100644 --- a/mautrix/types/__init__.py +++ b/mautrix/types/__init__.py @@ -149,6 +149,7 @@ ) from .misc import ( BatchSendResponse, + BeeperBatchSendResponse, DeviceLists, DeviceOTKCount, DirectoryPaginationToken, diff --git a/mautrix/types/misc.py b/mautrix/types/misc.py index 7a978bd1..699fc67e 100644 --- a/mautrix/types/misc.py +++ b/mautrix/types/misc.py @@ -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]