Create a PublicRoomListManager as part of ModuleApi

Additionally correct all mentions of a 'Public Room(s) Directory' -> 'Public Room List'.
pull/8292/head
Andrew Morgan 2020-09-14 14:36:21 +01:00
parent e768fae0c0
commit 04ab21ca5f
2 changed files with 51 additions and 34 deletions

View File

@ -118,8 +118,8 @@ class ThirdPartyEventRules:
async def check_visibility_can_be_modified( async def check_visibility_can_be_modified(
self, room_id: str, new_visibility: str self, room_id: str, new_visibility: str
) -> bool: ) -> bool:
"""Check if a room is allowed to be published to, or removed from, the public rooms """Check if a room is allowed to be published to, or removed from, the public room
directory. list.
Args: Args:
room_id: The ID of the room. room_id: The ID of the room.

View File

@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging import logging
from typing import TYPE_CHECKING
from twisted.internet import defer 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.logging.context import make_deferred_yieldable, run_in_background
from synapse.types import UserID 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 This package defines the 'stable' API which can be used by extension modules which
are loaded into Synapse. are loaded into Synapse.
@ -32,6 +36,50 @@ __all__ = ["errors", "make_deferred_yieldable", "run_in_background", "ModuleApi"
logger = logging.getLogger(__name__) 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: class ModuleApi:
"""A proxy object that gets passed to various plugin modules so they """A proxy object that gets passed to various plugin modules so they
can register new users etc if necessary. can register new users etc if necessary.
@ -45,6 +93,7 @@ class ModuleApi:
self._auth_handler = auth_handler self._auth_handler = auth_handler
self.http_client = hs.get_simple_http_client() # type: SimpleHttpClient 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): def get_user_by_req(self, req, allow_guest=False):
"""Check the access_token provided for a request """Check the access_token provided for a request
@ -269,35 +318,3 @@ class ModuleApi:
await self._auth_handler.complete_sso_login( await self._auth_handler.complete_sso_login(
registered_user_id, request, client_redirect_url, 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)