mirror of
https://github.com/element-hq/synapse.git
synced 2024-11-27 03:58:06 +03:00
Generate m.room.aliases event when the HS creates a room alias
This commit is contained in:
parent
1c7bb34ffd
commit
9dd4570b68
10 changed files with 94 additions and 18 deletions
|
@ -327,6 +327,11 @@ class Auth(object):
|
||||||
event.state_key,
|
event.state_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not current_state:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
current_state = current_state[0]
|
||||||
|
|
||||||
user_level = yield self.store.get_power_level(
|
user_level = yield self.store.get_power_level(
|
||||||
event.room_id,
|
event.room_id,
|
||||||
event.user_id,
|
event.user_id,
|
||||||
|
@ -341,7 +346,7 @@ class Auth(object):
|
||||||
|
|
||||||
# FIXME (erikj)
|
# FIXME (erikj)
|
||||||
old_people = {k: v for k, v in old_list.items() if k.startswith("@")}
|
old_people = {k: v for k, v in old_list.items() if k.startswith("@")}
|
||||||
new_people = {k: v for k, v in event.content if k.startswith("@")}
|
new_people = {k: v for k, v in event.content.items() if k.startswith("@")}
|
||||||
|
|
||||||
removed = set(old_people.keys()) - set(new_people.keys())
|
removed = set(old_people.keys()) - set(new_people.keys())
|
||||||
added = set(old_people.keys()) - set(new_people.keys())
|
added = set(old_people.keys()) - set(new_people.keys())
|
||||||
|
|
|
@ -173,3 +173,10 @@ class RoomOpsPowerLevelsEvent(SynapseStateEvent):
|
||||||
|
|
||||||
def get_content_template(self):
|
def get_content_template(self):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
class RoomAliasesEvent(SynapseStateEvent):
|
||||||
|
TYPE = "m.room.aliases"
|
||||||
|
|
||||||
|
def get_content_template(self):
|
||||||
|
return {}
|
||||||
|
|
|
@ -57,7 +57,7 @@ SCHEMAS = [
|
||||||
|
|
||||||
# Remember to update this number every time an incompatible change is made to
|
# Remember to update this number every time an incompatible change is made to
|
||||||
# database schema files, so the users will be informed on server restarts.
|
# database schema files, so the users will be informed on server restarts.
|
||||||
SCHEMA_VERSION = 2
|
SCHEMA_VERSION = 3
|
||||||
|
|
||||||
|
|
||||||
class SynapseHomeServer(HomeServer):
|
class SynapseHomeServer(HomeServer):
|
||||||
|
|
|
@ -42,9 +42,6 @@ class BaseHandler(object):
|
||||||
retry_after_ms=int(1000*(time_allowed - time_now)),
|
retry_after_ms=int(1000*(time_allowed - time_now)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class BaseRoomHandler(BaseHandler):
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _on_new_room_event(self, event, snapshot, extra_destinations=[],
|
def _on_new_room_event(self, event, snapshot, extra_destinations=[],
|
||||||
extra_users=[]):
|
extra_users=[]):
|
||||||
|
|
|
@ -19,8 +19,10 @@ from ._base import BaseHandler
|
||||||
|
|
||||||
from synapse.api.errors import SynapseError
|
from synapse.api.errors import SynapseError
|
||||||
from synapse.http.client import HttpClient
|
from synapse.http.client import HttpClient
|
||||||
|
from synapse.api.events.room import RoomAliasesEvent
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -37,7 +39,8 @@ class DirectoryHandler(BaseHandler):
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def create_association(self, room_alias, room_id, servers=None):
|
def create_association(self, user_id, room_alias, room_id, servers=None):
|
||||||
|
|
||||||
# TODO(erikj): Do auth.
|
# TODO(erikj): Do auth.
|
||||||
|
|
||||||
if not room_alias.is_mine:
|
if not room_alias.is_mine:
|
||||||
|
@ -54,12 +57,37 @@ class DirectoryHandler(BaseHandler):
|
||||||
if not servers:
|
if not servers:
|
||||||
raise SynapseError(400, "Failed to get server list")
|
raise SynapseError(400, "Failed to get server list")
|
||||||
|
|
||||||
yield self.store.create_room_alias_association(
|
|
||||||
room_alias,
|
try:
|
||||||
room_id,
|
yield self.store.create_room_alias_association(
|
||||||
servers
|
room_alias,
|
||||||
|
room_id,
|
||||||
|
servers
|
||||||
|
)
|
||||||
|
except sqlite3.IntegrityError:
|
||||||
|
defer.returnValue("Already exists")
|
||||||
|
|
||||||
|
# TODO: Send the room event.
|
||||||
|
|
||||||
|
aliases = yield self.store.get_aliases_for_room(room_id)
|
||||||
|
|
||||||
|
event = self.event_factory.create_event(
|
||||||
|
etype=RoomAliasesEvent.TYPE,
|
||||||
|
state_key=self.hs.hostname,
|
||||||
|
room_id=room_id,
|
||||||
|
user_id=user_id,
|
||||||
|
content={"aliases": aliases},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
snapshot = yield self.store.snapshot_room(
|
||||||
|
room_id=room_id,
|
||||||
|
user_id=user_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
yield self.state_handler.handle_new_event(event, snapshot)
|
||||||
|
yield self._on_new_room_event(event, snapshot, extra_users=[user_id])
|
||||||
|
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_association(self, room_alias):
|
def get_association(self, room_alias):
|
||||||
room_id = None
|
room_id = None
|
||||||
|
|
|
@ -19,7 +19,7 @@ from synapse.api.constants import Membership
|
||||||
from synapse.api.events.room import RoomTopicEvent
|
from synapse.api.events.room import RoomTopicEvent
|
||||||
from synapse.api.errors import RoomError
|
from synapse.api.errors import RoomError
|
||||||
from synapse.streams.config import PaginationConfig
|
from synapse.streams.config import PaginationConfig
|
||||||
from ._base import BaseRoomHandler
|
from ._base import BaseHandler
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MessageHandler(BaseRoomHandler):
|
class MessageHandler(BaseHandler):
|
||||||
|
|
||||||
def __init__(self, hs):
|
def __init__(self, hs):
|
||||||
super(MessageHandler, self).__init__(hs)
|
super(MessageHandler, self).__init__(hs)
|
||||||
|
|
|
@ -25,14 +25,14 @@ from synapse.api.events.room import (
|
||||||
RoomSendEventLevelEvent, RoomOpsPowerLevelsEvent, RoomNameEvent,
|
RoomSendEventLevelEvent, RoomOpsPowerLevelsEvent, RoomNameEvent,
|
||||||
)
|
)
|
||||||
from synapse.util import stringutils
|
from synapse.util import stringutils
|
||||||
from ._base import BaseRoomHandler
|
from ._base import BaseHandler
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class RoomCreationHandler(BaseRoomHandler):
|
class RoomCreationHandler(BaseHandler):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def create_room(self, user_id, room_id, config):
|
def create_room(self, user_id, room_id, config):
|
||||||
|
@ -105,7 +105,9 @@ class RoomCreationHandler(BaseRoomHandler):
|
||||||
)
|
)
|
||||||
|
|
||||||
if room_alias:
|
if room_alias:
|
||||||
yield self.store.create_room_alias_association(
|
directory_handler = self.hs.get_handlers().directory_handler
|
||||||
|
yield directory_handler.create_association(
|
||||||
|
user_id=user_id,
|
||||||
room_id=room_id,
|
room_id=room_id,
|
||||||
room_alias=room_alias,
|
room_alias=room_alias,
|
||||||
servers=[self.hs.hostname],
|
servers=[self.hs.hostname],
|
||||||
|
@ -239,7 +241,7 @@ class RoomCreationHandler(BaseRoomHandler):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class RoomMemberHandler(BaseRoomHandler):
|
class RoomMemberHandler(BaseHandler):
|
||||||
# TODO(paul): This handler currently contains a messy conflation of
|
# TODO(paul): This handler currently contains a messy conflation of
|
||||||
# low-level API that works on UserID objects and so on, and REST-level
|
# low-level API that works on UserID objects and so on, and REST-level
|
||||||
# API that takes ID strings and returns pagination chunks. These concerns
|
# API that takes ID strings and returns pagination chunks. These concerns
|
||||||
|
@ -560,7 +562,7 @@ class RoomMemberHandler(BaseRoomHandler):
|
||||||
extra_users=[target_user]
|
extra_users=[target_user]
|
||||||
)
|
)
|
||||||
|
|
||||||
class RoomListHandler(BaseRoomHandler):
|
class RoomListHandler(BaseHandler):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_public_room_list(self):
|
def get_public_room_list(self):
|
||||||
|
|
|
@ -45,6 +45,8 @@ class ClientDirectoryServer(RestServlet):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def on_PUT(self, request, room_alias):
|
def on_PUT(self, request, room_alias):
|
||||||
|
user = yield self.auth.get_user_by_req(request)
|
||||||
|
|
||||||
content = _parse_json(request)
|
content = _parse_json(request)
|
||||||
if not "room_id" in content:
|
if not "room_id" in content:
|
||||||
raise SynapseError(400, "Missing room_id key",
|
raise SynapseError(400, "Missing room_id key",
|
||||||
|
@ -69,12 +71,13 @@ class ClientDirectoryServer(RestServlet):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yield dir_handler.create_association(
|
yield dir_handler.create_association(
|
||||||
room_alias, room_id, servers
|
user.to_string(), room_alias, room_id, servers
|
||||||
)
|
)
|
||||||
except SynapseError as e:
|
except SynapseError as e:
|
||||||
raise e
|
raise e
|
||||||
except:
|
except:
|
||||||
logger.exception("Failed to create association")
|
logger.exception("Failed to create association")
|
||||||
|
raise
|
||||||
|
|
||||||
defer.returnValue((200, {}))
|
defer.returnValue((200, {}))
|
||||||
|
|
||||||
|
|
|
@ -92,3 +92,10 @@ class DirectoryStore(SQLBaseStore):
|
||||||
"server": server,
|
"server": server,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def get_aliases_for_room(self, room_id):
|
||||||
|
return self._simple_select_onecol(
|
||||||
|
"room_aliases",
|
||||||
|
{"room_id": room_id},
|
||||||
|
"room_alias",
|
||||||
|
)
|
||||||
|
|
27
synapse/storage/schema/delta/v3.sql
Normal file
27
synapse/storage/schema/delta/v3.sql
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/* Copyright 2014 OpenMarket Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS room_aliases_alias ON room_aliases(room_alias);
|
||||||
|
CREATE INDEX IF NOT EXISTS room_aliases_id ON room_aliases(room_id);
|
||||||
|
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS room_alias_servers_alias ON room_alias_servers(room_alias);
|
||||||
|
|
||||||
|
DELETE FROM room_aliases WHERE rowid NOT IN (SELECT max(rowid) FROM room_aliases GROUP BY room_alias, room_id);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS room_aliases_uniq ON room_aliases(room_alias, room_id);
|
||||||
|
|
||||||
|
PRAGMA user_version = 3;
|
Loading…
Reference in a new issue