Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

async/await is_server_admin #6839

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6839.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port `is_server_admin` method to async/await.
24 changes: 12 additions & 12 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ def create_association(

yield self._create_association(room_alias, room_id, servers, creator=user_id)

@defer.inlineCallbacks
def delete_association(self, requester: Requester, room_alias: RoomAlias):
async def delete_association(
self, requester: Requester, room_alias: RoomAlias
) -> str:
"""Remove an alias from the directory

(this is only meant for human users; AS users should call
Expand All @@ -171,7 +172,7 @@ def delete_association(self, requester: Requester, room_alias: RoomAlias):
room_alias

Returns:
Deferred[unicode]: room id that the alias used to point to
Room id that the alias used to point to

Raises:
NotFoundError: if the alias doesn't exist
Expand All @@ -184,7 +185,7 @@ def delete_association(self, requester: Requester, room_alias: RoomAlias):
user_id = requester.user.to_string()

try:
can_delete = yield self._user_can_delete_alias(room_alias, user_id)
can_delete = await self._user_can_delete_alias(room_alias, user_id)
except StoreError as e:
if e.code == 404:
raise NotFoundError("Unknown room alias")
Expand All @@ -193,18 +194,18 @@ def delete_association(self, requester: Requester, room_alias: RoomAlias):
if not can_delete:
raise AuthError(403, "You don't have permission to delete the alias.")

can_delete = yield self.can_modify_alias(room_alias, user_id=user_id)
can_delete = await self.can_modify_alias(room_alias, user_id=user_id)
if not can_delete:
raise SynapseError(
400,
"This alias is reserved by an application service.",
errcode=Codes.EXCLUSIVE,
)

room_id = yield self._delete_association(room_alias)
room_id = await self._delete_association(room_alias)

try:
yield self._update_canonical_alias(requester, user_id, room_id, room_alias)
await self._update_canonical_alias(requester, user_id, room_id, room_alias)
except AuthError as e:
logger.info("Failed to update alias events: %s", e)

Expand Down Expand Up @@ -376,8 +377,7 @@ def can_modify_alias(self, alias: RoomAlias, user_id: Optional[str] = None):
# either no interested services, or no service with an exclusive lock
return defer.succeed(True)

@defer.inlineCallbacks
def _user_can_delete_alias(self, alias: RoomAlias, user_id: str):
async def _user_can_delete_alias(self, alias: RoomAlias, user_id: str) -> bool:
"""Determine whether a user can delete an alias.

One of the following must be true:
Expand All @@ -388,18 +388,18 @@ def _user_can_delete_alias(self, alias: RoomAlias, user_id: str):
for the current room.

"""
creator = yield self.store.get_room_alias_creator(alias.to_string())
creator = await self.store.get_room_alias_creator(alias.to_string())

if creator is not None and creator == user_id:
return True

# Resolve the alias to the corresponding room.
room_mapping = yield self.get_association(alias)
room_mapping = await self.get_association(alias)
room_id = room_mapping["room_id"]
if not room_id:
return False

res = yield self.auth.check_can_change_room_list(
res = await self.auth.check_can_change_room_list(
room_id, UserID.from_string(user_id)
)
return res
Expand Down
43 changes: 21 additions & 22 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import math
import string
from collections import OrderedDict
from types import Dict

from six import iteritems, string_types

Expand Down Expand Up @@ -148,17 +149,16 @@ def upgrade_room(

return ret

@defer.inlineCallbacks
def _upgrade_room(
self, requester: Requester, old_room_id: str, new_version: RoomVersion
):
async def _upgrade_room(
self, requester: Requester, old_room_id: str, new_version: RoomVersion,
) -> RoomID:
user_id = requester.user.to_string()

# start by allocating a new room id
r = yield self.store.get_room(old_room_id)
r = await self.store.get_room(old_room_id)
if r is None:
raise NotFoundError("Unknown room id %s" % (old_room_id,))
new_room_id = yield self._generate_room_id(
new_room_id = await self._generate_room_id(
creator_id=user_id, is_public=r["is_public"], room_version=new_version,
)

Expand All @@ -169,7 +169,7 @@ def _upgrade_room(
(
tombstone_event,
tombstone_context,
) = yield self.event_creation_handler.create_event(
) = await self.event_creation_handler.create_event(
requester,
{
"type": EventTypes.Tombstone,
Expand All @@ -183,12 +183,12 @@ def _upgrade_room(
},
token_id=requester.access_token_id,
)
old_room_version = yield self.store.get_room_version_id(old_room_id)
yield self.auth.check_from_context(
old_room_version = await self.store.get_room_version_id(old_room_id)
await self.auth.check_from_context(
old_room_version, tombstone_event, tombstone_context
)

yield self.clone_existing_room(
await self.clone_existing_room(
requester,
old_room_id=old_room_id,
new_room_id=new_room_id,
Expand All @@ -197,25 +197,25 @@ def _upgrade_room(
)

# now send the tombstone
yield self.event_creation_handler.send_nonmember_event(
await self.event_creation_handler.send_nonmember_event(
requester, tombstone_event, tombstone_context
)

old_room_state = yield tombstone_context.get_current_state_ids()
old_room_state = await tombstone_context.get_current_state_ids()

# update any aliases
yield self._move_aliases_to_new_room(
await self._move_aliases_to_new_room(
requester, old_room_id, new_room_id, old_room_state
)

# Copy over user push rules, tags and migrate room directory state
yield self.room_member_handler.transfer_room_state_on_room_upgrade(
await self.room_member_handler.transfer_room_state_on_room_upgrade(
old_room_id, new_room_id
)

# finally, shut down the PLs in the old room, and update them in the new
# room.
yield self._update_upgraded_room_pls(
await self._update_upgraded_room_pls(
requester, old_room_id, new_room_id, old_room_state,
)

Expand Down Expand Up @@ -438,8 +438,7 @@ def clone_existing_room(
# XXX invites/joins
# XXX 3pid invites

@defer.inlineCallbacks
def _move_aliases_to_new_room(
async def _move_aliases_to_new_room(
self,
requester: Requester,
old_room_id: str,
Expand All @@ -448,13 +447,13 @@ def _move_aliases_to_new_room(
):
directory_handler = self.hs.get_handlers().directory_handler

aliases = yield self.store.get_aliases_for_room(old_room_id)
aliases = await self.store.get_aliases_for_room(old_room_id)

# check to see if we have a canonical alias.
canonical_alias_event = None
canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, ""))
if canonical_alias_event_id:
canonical_alias_event = yield self.store.get_event(canonical_alias_event_id)
canonical_alias_event = await self.store.get_event(canonical_alias_event_id)

# first we try to remove the aliases from the old room (we suppress sending
# the room_aliases event until the end).
Expand All @@ -472,7 +471,7 @@ def _move_aliases_to_new_room(
for alias_str in aliases:
alias = RoomAlias.from_string(alias_str)
try:
yield directory_handler.delete_association(requester, alias)
await directory_handler.delete_association(requester, alias)
removed_aliases.append(alias_str)
except SynapseError as e:
logger.warning("Unable to remove alias %s from old room: %s", alias, e)
Expand All @@ -485,7 +484,7 @@ def _move_aliases_to_new_room(
# we can now add any aliases we successfully removed to the new room.
for alias in removed_aliases:
try:
yield directory_handler.create_association(
await directory_handler.create_association(
requester,
RoomAlias.from_string(alias),
new_room_id,
Expand All @@ -502,7 +501,7 @@ def _move_aliases_to_new_room(
# alias event for the new room with a copy of the information.
try:
if canonical_alias_event:
yield self.event_creation_handler.create_and_send_nonmember_event(
await self.event_creation_handler.create_and_send_nonmember_event(
requester,
{
"type": EventTypes.CanonicalAlias,
Expand Down