|
55 | 55 | DomainSpecificString,
|
56 | 56 | JsonDict,
|
57 | 57 | Requester,
|
| 58 | + StateMap, |
58 | 59 | UserID,
|
59 | 60 | UserInfo,
|
60 | 61 | create_requester,
|
|
89 | 90 | "PRESENCE_ALL_USERS",
|
90 | 91 | "LoginResponse",
|
91 | 92 | "JsonDict",
|
| 93 | + "EventBase", |
| 94 | + "StateMap", |
92 | 95 | ]
|
93 | 96 |
|
94 | 97 | logger = logging.getLogger(__name__)
|
@@ -964,6 +967,52 @@ async def get_user_ip_and_agents(
|
964 | 967 | else:
|
965 | 968 | return []
|
966 | 969 |
|
| 970 | + async def get_room_state( |
| 971 | + self, |
| 972 | + room_id: str, |
| 973 | + event_filter: Optional[Iterable[Tuple[str, Optional[str]]]] = None, |
| 974 | + ) -> StateMap[EventBase]: |
| 975 | + """Returns the current state of the given room. |
| 976 | +
|
| 977 | + The events are returned as a mapping, in which the key for each event is a tuple |
| 978 | + which first element is the event's type and the second one is its state key. |
| 979 | +
|
| 980 | + Added in Synapse v1.47.0 |
| 981 | +
|
| 982 | + Args: |
| 983 | + room_id: The ID of the room to get state from. |
| 984 | + event_filter: A filter to apply when retrieving events. None if no filter |
| 985 | + should be applied. If provided, must be an iterable of tuples. A tuple's |
| 986 | + first element is the event type and the second is the state key, or is |
| 987 | + None if the state key should not be filtered on. |
| 988 | + An example of a filter is: |
| 989 | + [ |
| 990 | + ("m.room.member", "@alice:example.com"), # Member event for @alice:example.com |
| 991 | + ("org.matrix.some_event", ""), # State event of type "org.matrix.some_event" |
| 992 | + # with an empty string as its state key |
| 993 | + ("org.matrix.some_other_event", None), # State events of type "org.matrix.some_other_event" |
| 994 | + # regardless of their state key |
| 995 | + ] |
| 996 | + """ |
| 997 | + if event_filter: |
| 998 | + # If a filter was provided, turn it into a StateFilter and retrieve a filtered |
| 999 | + # view of the state. |
| 1000 | + state_filter = StateFilter.from_types(event_filter) |
| 1001 | + state_ids = await self._store.get_filtered_current_state_ids( |
| 1002 | + room_id, |
| 1003 | + state_filter, |
| 1004 | + ) |
| 1005 | + else: |
| 1006 | + # If no filter was provided, get the whole state. We could also reuse the call |
| 1007 | + # to get_filtered_current_state_ids above, with `state_filter = StateFilter.all()`, |
| 1008 | + # but get_filtered_current_state_ids isn't cached and `get_current_state_ids` |
| 1009 | + # is, so using the latter when we can is better for perf. |
| 1010 | + state_ids = await self._store.get_current_state_ids(room_id) |
| 1011 | + |
| 1012 | + state_events = await self._store.get_events(state_ids.values()) |
| 1013 | + |
| 1014 | + return {key: state_events[event_id] for key, event_id in state_ids.items()} |
| 1015 | + |
967 | 1016 |
|
968 | 1017 | class PublicRoomListManager:
|
969 | 1018 | """Contains methods for adding to, removing from and querying whether a room
|
|
0 commit comments