mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-08-01 08:46:03 -04:00
Implement directory service federation by Federation Queries; avoid local_only hack; add unit tests
This commit is contained in:
parent
7fb93f2a47
commit
3a1cfe18cf
3 changed files with 141 additions and 27 deletions
|
@ -35,9 +35,11 @@ class DirectoryHandler(BaseHandler):
|
|||
|
||||
def __init__(self, hs):
|
||||
super(DirectoryHandler, self).__init__(hs)
|
||||
self.hs = hs
|
||||
self.http_client = hs.get_http_client()
|
||||
self.clock = hs.get_clock()
|
||||
|
||||
self.federation = hs.get_replication_layer()
|
||||
self.federation.register_query_handler(
|
||||
"directory", self.on_directory_query
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def create_association(self, room_alias, room_id, servers):
|
||||
|
@ -58,9 +60,7 @@ class DirectoryHandler(BaseHandler):
|
|||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def get_association(self, room_alias, local_only=False):
|
||||
# TODO(erikj): Do auth
|
||||
|
||||
def get_association(self, room_alias):
|
||||
room_id = None
|
||||
if room_alias.is_mine:
|
||||
result = yield self.store.get_association_from_room_alias(
|
||||
|
@ -70,22 +70,13 @@ class DirectoryHandler(BaseHandler):
|
|||
if result:
|
||||
room_id = result.room_id
|
||||
servers = result.servers
|
||||
elif not local_only:
|
||||
path = "%s/ds/room/%s?local_only=1" % (
|
||||
PREFIX,
|
||||
urllib.quote(room_alias.to_string())
|
||||
else:
|
||||
result = yield self.federation.make_query(
|
||||
destination=room_alias.domain,
|
||||
query_type="directory",
|
||||
args={"room_alias": room_alias.to_string()},
|
||||
)
|
||||
|
||||
result = None
|
||||
try:
|
||||
result = yield self.http_client.get_json(
|
||||
destination=room_alias.domain,
|
||||
path=path,
|
||||
)
|
||||
except:
|
||||
# TODO(erikj): Handle this better?
|
||||
logger.exception("Failed to get remote room alias")
|
||||
|
||||
if result and "room_id" in result and "servers" in result:
|
||||
room_id = result["room_id"]
|
||||
servers = result["servers"]
|
||||
|
@ -99,3 +90,20 @@ class DirectoryHandler(BaseHandler):
|
|||
"servers": servers,
|
||||
})
|
||||
return
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def on_directory_query(self, args):
|
||||
room_alias = self.hs.parse_roomalias(args["room_alias"])
|
||||
if not room_alias.is_mine:
|
||||
raise SynapseError(
|
||||
400, "Room Alias is not hosted on this Home Server"
|
||||
)
|
||||
|
||||
result = yield self.store.get_association_from_room_alias(
|
||||
room_alias
|
||||
)
|
||||
|
||||
defer.returnValue({
|
||||
"room_id": result.room_id,
|
||||
"servers": result.servers,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue