diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py
index 16ca86b7bc..0142542852 100644
--- a/synapse/module_api/__init__.py
+++ b/synapse/module_api/__init__.py
@@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Iterable, Optional, Tuple
from twisted.internet import defer
@@ -22,6 +22,7 @@ from synapse.events import EventBase
from synapse.http.client import SimpleHttpClient
from synapse.http.site import SynapseRequest
from synapse.logging.context import make_deferred_yieldable, run_in_background
+from synapse.storage.state import StateFilter
from synapse.types import JsonDict, UserID, create_requester
if TYPE_CHECKING:
@@ -37,50 +38,6 @@ __all__ = ["errors", "make_deferred_yieldable", "run_in_background", "ModuleApi"
logger = logging.getLogger(__name__)
-class PublicRoomListManager:
- """Contains methods for adding to, removing from and querying whether a room
- is in the public room list.
-
- Args:
- hs: The Homeserver object
- """
-
- def __init__(self, hs: "HomeServer"):
- self._store = hs.get_datastore()
-
- async def room_is_in_public_room_list(self, room_id: str) -> bool:
- """Checks whether a room is in the public room list.
-
- Args:
- room_id: The ID of the room.
-
- Returns:
- Whether the room is in the public room list. Returns False if the room does
- not exist.
- """
- room = await self._store.get_room(room_id)
- if not room:
- return False
-
- return room.get("is_public", False)
-
- async def add_room_to_public_room_list(self, room_id: str) -> None:
- """Publishes a room to the public room list.
-
- Args:
- room_id: The ID of the room.
- """
- await self._store.set_room_is_public(room_id, True)
-
- async def remove_room_from_public_room_list(self, room_id: str) -> None:
- """Removes a room from the public room list.
-
- Args:
- room_id: The ID of the room.
- """
- await self._store.set_room_is_public(room_id, False)
-
-
class ModuleApi:
"""A proxy object that gets passed to various plugin modules so they
can register new users etc if necessary.
@@ -93,8 +50,26 @@ class ModuleApi:
self._auth = hs.get_auth()
self._auth_handler = auth_handler
- self.http_client = hs.get_simple_http_client() # type: SimpleHttpClient
- self.public_room_list_manager = PublicRoomListManager(hs)
+ # We expose these as properties below in order to attach a helpful docstring.
+ self._http_client = hs.get_simple_http_client() # type: SimpleHttpClient
+ self._public_room_list_manager = PublicRoomListManager(hs)
+
+ @property
+ def http_client(self):
+ """Allows making outbound HTTP requests to remote resources.
+
+ An instance of synapse.http.client.SimpleHttpClient
+ """
+ return self._http_client
+
+ @property
+ def public_room_list_manager(self):
+ """Allows adding to, removing from and checking the status of rooms in the
+ public room list.
+
+ An instance of synapse.module_api.PublicRoomListManager
+ """
+ return self._public_room_list_manager
def get_user_by_req(self, req, allow_guest=False):
"""Check the access_token provided for a request
@@ -320,6 +295,32 @@ class ModuleApi:
registered_user_id, request, client_redirect_url,
)
+ @defer.inlineCallbacks
+ def get_state_events_in_room(
+ self, room_id: str, types: Iterable[Tuple[str, Optional[str]]]
+ ) -> defer.Deferred:
+ """Gets current state events for the given room.
+
+ (This is exposed for compatibility with the old SpamCheckerApi. We should
+ probably deprecate it and replace it with an async method in a subclass.)
+
+ Args:
+ room_id: The room ID to get state events in.
+ types: The event type and state key (using None
+ to represent 'any') of the room state to acquire.
+
+ Returns:
+ twisted.internet.defer.Deferred[list(synapse.events.FrozenEvent)]:
+ The filtered state events in the room.
+ """
+ state_ids = yield defer.ensureDeferred(
+ self._store.get_filtered_current_state_ids(
+ room_id=room_id, state_filter=StateFilter.from_types(types)
+ )
+ )
+ state = yield defer.ensureDeferred(self._store.get_events(state_ids.values()))
+ return state.values()
+
async def create_and_send_event_into_room(self, event_dict: JsonDict) -> EventBase:
"""Create and send an event into a room. Membership events are currently not supported.
@@ -342,7 +343,48 @@ class ModuleApi:
event,
_,
) = await self._hs.get_event_creation_handler().create_and_send_nonmember_event(
- requester, event_dict, ratelimit=False
+ requester, event_dict, ratelimit=False, ignore_shadow_ban=True,
)
return event
+
+
+class PublicRoomListManager:
+ """Contains methods for adding to, removing from and querying whether a room
+ is in the public room list.
+ """
+
+ def __init__(self, hs: "HomeServer"):
+ self._store = hs.get_datastore()
+
+ async def room_is_in_public_room_list(self, room_id: str) -> bool:
+ """Checks whether a room is in the public room list.
+
+ Args:
+ room_id: The ID of the room.
+
+ Returns:
+ Whether the room is in the public room list. Returns False if the room does
+ not exist.
+ """
+ room = await self._store.get_room(room_id)
+ if not room:
+ return False
+
+ return room.get("is_public", False)
+
+ async def add_room_to_public_room_list(self, room_id: str) -> None:
+ """Publishes a room to the public room list.
+
+ Args:
+ room_id: The ID of the room.
+ """
+ await self._store.set_room_is_public(room_id, True)
+
+ async def remove_room_from_public_room_list(self, room_id: str) -> None:
+ """Removes a room from the public room list.
+
+ Args:
+ room_id: The ID of the room.
+ """
+ await self._store.set_room_is_public(room_id, False)
|