|
45 | 45 | from synapse.storage.database import DatabasePool, LoggingTransaction
|
46 | 46 | from synapse.storage.databases.main.roommember import ProfileInfo
|
47 | 47 | from synapse.storage.state import StateFilter
|
48 |
| -from synapse.types import JsonDict, Requester, UserID, UserInfo, create_requester |
| 48 | +from synapse.types import ( |
| 49 | + JsonDict, |
| 50 | + Requester, |
| 51 | + StateMap, |
| 52 | + UserID, |
| 53 | + UserInfo, |
| 54 | + create_requester, |
| 55 | +) |
49 | 56 | from synapse.util import Clock
|
50 | 57 | from synapse.util.caches.descriptors import cached
|
51 | 58 |
|
|
70 | 77 | "DirectServeHtmlResource",
|
71 | 78 | "DirectServeJsonResource",
|
72 | 79 | "ModuleApi",
|
| 80 | + "EventBase", |
| 81 | + "StateMap", |
73 | 82 | ]
|
74 | 83 |
|
75 | 84 | logger = logging.getLogger(__name__)
|
@@ -690,6 +699,52 @@ def read_templates(
|
690 | 699 | (td for td in (self.custom_template_dir, custom_template_directory) if td),
|
691 | 700 | )
|
692 | 701 |
|
| 702 | + async def get_room_state( |
| 703 | + self, |
| 704 | + room_id: str, |
| 705 | + event_filter: Optional[Iterable[Tuple[str, Optional[str]]]] = None, |
| 706 | + ) -> StateMap[EventBase]: |
| 707 | + """Returns the current state of the given room. |
| 708 | +
|
| 709 | + The events are returned as a mapping, in which the key for each event is a tuple |
| 710 | + which first element is the event's type and the second one is its state key. |
| 711 | +
|
| 712 | + Added in Synapse v1.47.0 |
| 713 | +
|
| 714 | + Args: |
| 715 | + room_id: The ID of the room to get state from. |
| 716 | + event_filter: A filter to apply when retrieving events. None if no filter |
| 717 | + should be applied. If provided, must be an iterable of tuples. A tuple's |
| 718 | + first element is the event type and the second is the state key, or is |
| 719 | + None if the state key should not be filtered on. |
| 720 | + An example of a filter is: |
| 721 | + [ |
| 722 | + ("m.room.member", "@alice:example.com"), # Member event for @alice:example.com |
| 723 | + ("org.matrix.some_event", ""), # State event of type "org.matrix.some_event" |
| 724 | + # with an empty string as its state key |
| 725 | + ("org.matrix.some_other_event", None), # State events of type "org.matrix.some_other_event" |
| 726 | + # regardless of their state key |
| 727 | + ] |
| 728 | + """ |
| 729 | + if event_filter: |
| 730 | + # If a filter was provided, turn it into a StateFilter and retrieve a filtered |
| 731 | + # view of the state. |
| 732 | + state_filter = StateFilter.from_types(event_filter) |
| 733 | + state_ids = await self._store.get_filtered_current_state_ids( |
| 734 | + room_id, |
| 735 | + state_filter, |
| 736 | + ) |
| 737 | + else: |
| 738 | + # If no filter was provided, get the whole state. We could also reuse the call |
| 739 | + # to get_filtered_current_state_ids above, with `state_filter = StateFilter.all()`, |
| 740 | + # but get_filtered_current_state_ids isn't cached and `get_current_state_ids` |
| 741 | + # is, so using the latter when we can is better for perf. |
| 742 | + state_ids = await self._store.get_current_state_ids(room_id) |
| 743 | + |
| 744 | + state_events = await self._store.get_events(state_ids.values()) |
| 745 | + |
| 746 | + return {key: state_events[event_id] for key, event_id in state_ids.items()} |
| 747 | + |
693 | 748 |
|
694 | 749 | class PublicRoomListManager:
|
695 | 750 | """Contains methods for adding to, removing from and querying whether a room
|
|
0 commit comments