From 04ab21ca5f11168dc051a629a6fdddb981b76291 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 14 Sep 2020 14:36:21 +0100 Subject: [PATCH] Create a PublicRoomListManager as part of ModuleApi Additionally correct all mentions of a 'Public Room(s) Directory' -> 'Public Room List'. --- synapse/events/third_party_rules.py | 4 +- synapse/module_api/__init__.py | 81 +++++++++++++++++------------ 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/synapse/events/third_party_rules.py b/synapse/events/third_party_rules.py index 503e6b1936..21e433ea79 100644 --- a/synapse/events/third_party_rules.py +++ b/synapse/events/third_party_rules.py @@ -118,8 +118,8 @@ class ThirdPartyEventRules: async def check_visibility_can_be_modified( self, room_id: str, new_visibility: str ) -> bool: - """Check if a room is allowed to be published to, or removed from, the public rooms - directory. + """Check if a room is allowed to be published to, or removed from, the public room + list. Args: room_id: The ID of the room. diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py index 3f2708bfe1..d12bf11ac7 100644 --- a/synapse/module_api/__init__.py +++ b/synapse/module_api/__init__.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import logging +from typing import TYPE_CHECKING from twisted.internet import defer @@ -22,6 +23,9 @@ from synapse.http.site import SynapseRequest from synapse.logging.context import make_deferred_yieldable, run_in_background from synapse.types import UserID +if TYPE_CHECKING: + from synapse.server import HomeServer + """ This package defines the 'stable' API which can be used by extension modules which are loaded into Synapse. @@ -32,6 +36,50 @@ __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. @@ -45,6 +93,7 @@ class ModuleApi: self._auth_handler = auth_handler self.http_client = hs.get_simple_http_client() # type: SimpleHttpClient + self.public_room_list_manager = PublicRoomListManager(hs) def get_user_by_req(self, req, allow_guest=False): """Check the access_token provided for a request @@ -269,35 +318,3 @@ class ModuleApi: await self._auth_handler.complete_sso_login( registered_user_id, request, client_redirect_url, ) - - async def room_is_in_public_directory(self, room_id: str) -> bool: - """Checks whether a room is in the public rooms directory. - - Args: - room_id: The ID of the room. - - Returns: - Whether the room is in the public rooms directory. 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_directory(self, room_id: str) -> None: - """Publishes a room to the public rooms directory. - - Args: - room_id: The ID of the room. - """ - await self._store.set_room_is_public(room_id, True) - - async def remove_room_from_public_directory(self, room_id: str) -> None: - """Removes a room from the public rooms directory. - - Args: - room_id: The ID of the room. - """ - await self._store.set_room_is_public(room_id, False)