mirror of
https://github.com/element-hq/synapse.git
synced 2024-11-24 18:45:52 +03:00
SYN-2: Allow server admins to delete room aliases
This commit is contained in:
parent
1f76377a7c
commit
e06adc6d7e
4 changed files with 99 additions and 21 deletions
|
@ -57,7 +57,6 @@ class DirectoryHandler(BaseHandler):
|
|||
if not servers:
|
||||
raise SynapseError(400, "Failed to get server list")
|
||||
|
||||
|
||||
try:
|
||||
yield self.store.create_room_alias_association(
|
||||
room_alias,
|
||||
|
@ -68,25 +67,19 @@ class DirectoryHandler(BaseHandler):
|
|||
defer.returnValue("Already exists")
|
||||
|
||||
# TODO: Send the room event.
|
||||
yield self._update_room_alias_events(user_id, room_id)
|
||||
|
||||
aliases = yield self.store.get_aliases_for_room(room_id)
|
||||
@defer.inlineCallbacks
|
||||
def delete_association(self, user_id, room_alias):
|
||||
# TODO Check if server admin
|
||||
|
||||
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},
|
||||
)
|
||||
if not room_alias.is_mine:
|
||||
raise SynapseError(400, "Room alias must be local")
|
||||
|
||||
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])
|
||||
room_id = yield self.store.delete_room_alias(room_alias)
|
||||
|
||||
if room_id:
|
||||
yield self._update_room_alias_events(user_id, room_id)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def get_association(self, room_alias):
|
||||
|
@ -142,3 +135,23 @@ class DirectoryHandler(BaseHandler):
|
|||
"room_id": result.room_id,
|
||||
"servers": result.servers,
|
||||
})
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def _update_room_alias_events(self, user_id, room_id):
|
||||
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])
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
from twisted.internet import defer
|
||||
|
||||
from synapse.api.errors import SynapseError, Codes
|
||||
from synapse.api.errors import AuthError, SynapseError, Codes
|
||||
from base import RestServlet, client_path_pattern
|
||||
|
||||
import json
|
||||
|
@ -81,6 +81,24 @@ class ClientDirectoryServer(RestServlet):
|
|||
|
||||
defer.returnValue((200, {}))
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def on_DELETE(self, request, room_alias):
|
||||
user = yield self.auth.get_user_by_req(request)
|
||||
|
||||
is_admin = yield self.auth.is_server_admin(user)
|
||||
if not is_admin:
|
||||
raise AuthError(403, "You need to be a server admin")
|
||||
|
||||
dir_handler = self.handlers.directory_handler
|
||||
|
||||
room_alias = self.hs.parse_roomalias(urllib.unquote(room_alias))
|
||||
|
||||
yield dir_handler.delete_association(
|
||||
user.to_string(), room_alias
|
||||
)
|
||||
|
||||
defer.returnValue((200, {}))
|
||||
|
||||
|
||||
def _parse_json(request):
|
||||
try:
|
||||
|
|
|
@ -93,6 +93,36 @@ class DirectoryStore(SQLBaseStore):
|
|||
}
|
||||
)
|
||||
|
||||
def delete_room_alias(self, room_alias):
|
||||
return self.runInteraction(
|
||||
self._delete_room_alias_txn,
|
||||
room_alias,
|
||||
)
|
||||
|
||||
def _delete_room_alias_txn(self, txn, room_alias):
|
||||
cursor = txn.execute(
|
||||
"SELECT room_id FROM room_aliases WHERE room_alias = ?",
|
||||
(room_alias.to_string(),)
|
||||
)
|
||||
|
||||
res = cursor.fetchone()
|
||||
if res:
|
||||
room_id = res[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
txn.execute(
|
||||
"DELETE FROM room_aliases WHERE room_alias = ?",
|
||||
(room_alias.to_string(),)
|
||||
)
|
||||
|
||||
txn.execute(
|
||||
"DELETE FROM room_alias_servers WHERE room_alias = ?",
|
||||
(room_alias.to_string(),)
|
||||
)
|
||||
|
||||
return room_id
|
||||
|
||||
def get_aliases_for_room(self, room_id):
|
||||
return self._simple_select_onecol(
|
||||
"room_aliases",
|
||||
|
|
|
@ -30,7 +30,8 @@ class DirectoryStoreTestCase(unittest.TestCase):
|
|||
db_pool = SQLiteMemoryDbPool()
|
||||
yield db_pool.prepare()
|
||||
|
||||
hs = HomeServer("test",
|
||||
hs = HomeServer(
|
||||
"test",
|
||||
db_pool=db_pool,
|
||||
)
|
||||
|
||||
|
@ -60,9 +61,25 @@ class DirectoryStoreTestCase(unittest.TestCase):
|
|||
servers=["test"],
|
||||
)
|
||||
|
||||
|
||||
self.assertObjectHasAttributes(
|
||||
{"room_id": self.room.to_string(),
|
||||
"servers": ["test"]},
|
||||
{
|
||||
"room_id": self.room.to_string(),
|
||||
"servers": ["test"],
|
||||
},
|
||||
(yield self.store.get_association_from_room_alias(self.alias))
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def test_delete_alias(self):
|
||||
yield self.store.create_room_alias_association(
|
||||
room_alias=self.alias,
|
||||
room_id=self.room.to_string(),
|
||||
servers=["test"],
|
||||
)
|
||||
|
||||
room_id = yield self.store.delete_room_alias(self.alias)
|
||||
self.assertEqual(self.room.to_string(), room_id)
|
||||
|
||||
self.assertIsNone(
|
||||
(yield self.store.get_association_from_room_alias(self.alias))
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue