|
16 | 16 | ContentURI, |
17 | 17 | Event, |
18 | 18 | EventContent, |
| 19 | + EventContext, |
19 | 20 | EventID, |
20 | 21 | EventType, |
21 | 22 | FilterID, |
@@ -125,6 +126,48 @@ async def get_event(self, room_id: RoomID, event_id: EventID) -> Event: |
125 | 126 | except SerializerError as e: |
126 | 127 | raise MatrixResponseError("Invalid event in response") from e |
127 | 128 |
|
| 129 | + async def get_event_context( |
| 130 | + self, |
| 131 | + room_id: RoomID, |
| 132 | + event_id: EventID, |
| 133 | + limit: int | None = 10, |
| 134 | + filter: RoomEventFilter | None = None, |
| 135 | + ) -> EventContext: |
| 136 | + """ |
| 137 | + Get a number of events that happened just before and after the specified event. |
| 138 | + This allows clients to get the context surrounding an event, as well as get the state at |
| 139 | + an event and paginate in either direction. |
| 140 | +
|
| 141 | + Args: |
| 142 | + room_id: The room to get events from. |
| 143 | + event_id: The event to get context around. |
| 144 | + limit: The maximum number of events to return. The limit applies to the total number of |
| 145 | + events before and after the requested event. A limit of 0 means no other events |
| 146 | + are returned, while 2 means one event before and one after are returned. |
| 147 | + filter: A JSON RoomEventFilter_ to filter returned events with. |
| 148 | +
|
| 149 | + Returns: |
| 150 | + The event itself, up to ``limit/2`` events before and after the event, the room state |
| 151 | + at the event, and pagination tokens to scroll up and down. |
| 152 | +
|
| 153 | + .. _RoomEventFilter: |
| 154 | + https://spec.matrix.org/v1.1/client-server-api/#filtering |
| 155 | + """ |
| 156 | + query_params = {} |
| 157 | + if limit is not None: |
| 158 | + query_params["limit"] = str(limit) |
| 159 | + if filter is not None: |
| 160 | + query_params["filter"] = ( |
| 161 | + filter.serialize() if isinstance(filter, Serializable) else filter |
| 162 | + ) |
| 163 | + resp = await self.api.request( |
| 164 | + Method.GET, |
| 165 | + Path.v3.rooms[room_id].context[event_id], |
| 166 | + query_params=query_params, |
| 167 | + metrics_method="get_event_context", |
| 168 | + ) |
| 169 | + return EventContext.deserialize(resp) |
| 170 | + |
128 | 171 | async def get_state_event( |
129 | 172 | self, |
130 | 173 | room_id: RoomID, |
|
0 commit comments