Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add a module API method to retrieve state from a room #11204

Merged
merged 9 commits into from
Oct 29, 2021
1 change: 1 addition & 0 deletions changelog.d/11204.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a module API method to retrieve the current state of a room.
14 changes: 14 additions & 0 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
DomainSpecificString,
JsonDict,
Requester,
StateMap,
UserID,
UserInfo,
create_requester,
Expand Down Expand Up @@ -866,6 +867,19 @@ async def get_user_ip_and_agents(
else:
return []

async def get_room_state(self, room_id: str) -> StateMap[EventBase]:
babolivier marked this conversation as resolved.
Show resolved Hide resolved
"""Returns the current state of the given room.

The events are returned as a mapping, in which the key for each event is a tuple
which first element is the event's type and the second one is its state key.

Added in Synapse v1.47.0
"""
state_ids = await self._store.get_current_state_ids(room_id)
state_events = await self._store.get_events(state_ids.values())

return {key: state_events[event_id] for key, event_id in state_ids.items()}


class PublicRoomListManager:
"""Contains methods for adding to, removing from and querying whether a room
Expand Down
23 changes: 22 additions & 1 deletion tests/module_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from twisted.internet import defer

from synapse.api.constants import EduTypes
from synapse.api.constants import EduTypes, EventTypes
from synapse.events import EventBase
from synapse.federation.units import Transaction
from synapse.handlers.presence import UserPresenceState
Expand Down Expand Up @@ -385,6 +385,27 @@ def test_send_local_online_presence_to_federation(self):

self.assertTrue(found_update)

def test_get_room_state(self):
"""Tests that a module can retrieve the state of a room through the module API."""
user_id = self.register_user("peter", "hackme")
tok = self.login("peter", "hackme")

# Create a room and send some custom state in it.
room_id = self.helper.create_room_as(tok=tok)
self.helper.send_state(room_id, "org.matrix.test", {}, tok=tok)

# Check that the module API can successfully fetch state for the room.
state = self.get_success(
defer.ensureDeferred(self.module_api.get_room_state(room_id))
)

# Check that a few standard events are in the returned state.
self.assertIn((EventTypes.Create, ""), state)
self.assertIn((EventTypes.Member, user_id), state)
babolivier marked this conversation as resolved.
Show resolved Hide resolved

# Check that our custom state event is in the returned state.
self.assertIn(("org.matrix.test", ""), state)
babolivier marked this conversation as resolved.
Show resolved Hide resolved


class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
"""For testing ModuleApi functionality in a multi-worker setup"""
Expand Down