Add a HomeServer.parse_roomalias() to avoid having to RoomAlias.from_sring(..., hs=hs) - similar to parse_userid()

This commit is contained in:
Paul "LeoNerd" Evans 2014-08-13 17:43:10 +01:00
parent 08ed4ccfdf
commit 7fb93f2a47
4 changed files with 18 additions and 14 deletions

View File

@ -16,7 +16,6 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.types import RoomAlias, RoomID
from base import RestServlet, client_path_pattern from base import RestServlet, client_path_pattern
import json import json
@ -39,12 +38,11 @@ class ClientDirectoryServer(RestServlet):
# TODO(erikj): Handle request # TODO(erikj): Handle request
local_only = "local_only" in request.args local_only = "local_only" in request.args
room_alias = urllib.unquote(room_alias) room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias))
room_alias_obj = RoomAlias.from_string(room_alias, self.hs)
dir_handler = self.handlers.directory_handler dir_handler = self.handlers.directory_handler
res = yield dir_handler.get_association( res = yield dir_handler.get_association(
room_alias_obj, room_alias,
local_only=local_only local_only=local_only
) )
@ -57,10 +55,9 @@ class ClientDirectoryServer(RestServlet):
logger.debug("Got content: %s", content) logger.debug("Got content: %s", content)
room_alias = urllib.unquote(room_alias) room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias))
room_alias_obj = RoomAlias.from_string(room_alias, self.hs)
logger.debug("Got room name: %s", room_alias_obj.to_string()) logger.debug("Got room name: %s", room_alias.to_string())
room_id = content["room_id"] room_id = content["room_id"]
servers = content["servers"] servers = content["servers"]
@ -75,7 +72,7 @@ class ClientDirectoryServer(RestServlet):
try: try:
yield dir_handler.create_association( yield dir_handler.create_association(
room_alias_obj, room_id, servers room_alias, room_id, servers
) )
except: except:
logger.exception("Failed to create association") logger.exception("Failed to create association")

View File

@ -22,7 +22,6 @@ from synapse.api.events.room import (RoomTopicEvent, MessageEvent,
RoomMemberEvent, FeedbackEvent) RoomMemberEvent, FeedbackEvent)
from synapse.api.constants import Feedback, Membership from synapse.api.constants import Feedback, Membership
from synapse.api.streams import PaginationConfig from synapse.api.streams import PaginationConfig
from synapse.types import RoomAlias
import json import json
import logging import logging
@ -150,10 +149,7 @@ class JoinRoomAliasServlet(RestServlet):
logger.debug("room_alias: %s", room_alias) logger.debug("room_alias: %s", room_alias)
room_alias = RoomAlias.from_string( room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias))
urllib.unquote(room_alias),
self.hs
)
handler = self.handlers.room_member_handler handler = self.handlers.room_member_handler
ret_dict = yield handler.join_room_alias(user, room_alias) ret_dict = yield handler.join_room_alias(user, room_alias)

View File

@ -28,7 +28,7 @@ from synapse.handlers import Handlers
from synapse.rest import RestServletFactory from synapse.rest import RestServletFactory
from synapse.state import StateHandler from synapse.state import StateHandler
from synapse.storage import DataStore from synapse.storage import DataStore
from synapse.types import UserID from synapse.types import UserID, RoomAlias
from synapse.util import Clock from synapse.util import Clock
from synapse.util.distributor import Distributor from synapse.util.distributor import Distributor
from synapse.util.lockutils import LockManager from synapse.util.lockutils import LockManager
@ -120,6 +120,11 @@ class BaseHomeServer(object):
object.""" object."""
return UserID.from_string(s, hs=self) return UserID.from_string(s, hs=self)
def parse_roomalias(self, s):
"""Parse the string given by 's' as a Room Alias and return a RoomAlias
object."""
return RoomAlias.from_string(s, hs=self)
# Build magic accessors for every dependency # Build magic accessors for every dependency
for depname in BaseHomeServer.DEPENDENCIES: for depname in BaseHomeServer.DEPENDENCIES:
BaseHomeServer._make_dependency_method(depname) BaseHomeServer._make_dependency_method(depname)

View File

@ -62,3 +62,9 @@ class RoomAliasTestCase(unittest.TestCase):
room = RoomAlias("channel", "my.domain", True) room = RoomAlias("channel", "my.domain", True)
self.assertEquals(room.to_string(), "#channel:my.domain") self.assertEquals(room.to_string(), "#channel:my.domain")
def test_via_homeserver(self):
room = mock_homeserver.parse_roomalias("#elsewhere:my.domain")
self.assertEquals("elsewhere", room.localpart)
self.assertEquals("my.domain", room.domain)