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

Do not send alias events when creating / upgrading a room #6941

Merged
merged 8 commits into from
Feb 20, 2020
1 change: 1 addition & 0 deletions changelog.d/6941.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop sending m.room.aliases events during room creation and upgrade.
18 changes: 1 addition & 17 deletions synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,22 +282,6 @@ def on_directory_query(self, args):
Codes.NOT_FOUND,
)

@defer.inlineCallbacks
def send_room_alias_update_event(self, requester, room_id):
aliases = yield self.store.get_aliases_for_room(room_id)

yield self.event_creation_handler.create_and_send_nonmember_event(
requester,
{
"type": EventTypes.Aliases,
"state_key": self.hs.hostname,
"room_id": room_id,
"sender": requester.user.to_string(),
"content": {"aliases": aliases},
},
ratelimit=False,
)

@defer.inlineCallbacks
def _update_canonical_alias(self, requester, user_id, room_id, room_alias):
"""
Expand Down Expand Up @@ -326,7 +310,7 @@ def _update_canonical_alias(self, requester, user_id, room_id, room_alias):
alt_aliases = content.pop("alt_aliases", None)
# If the aliases are not a list (or not found) do not attempt to modify
# the list.
if isinstance(alt_aliases, list):
if isinstance(alt_aliases, (list, tuple)):
clokep marked this conversation as resolved.
Show resolved Hide resolved
send_update = True
alt_aliases = [alias for alias in alt_aliases if alias != alias_str]
if alt_aliases:
Expand Down
39 changes: 21 additions & 18 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,19 @@ def _move_aliases_to_new_room(

# check to see if we have a canonical alias.
canonical_alias = None
canonical_alt_aliases = []
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)
if canonical_alias_event:
canonical_alias = canonical_alias_event.content.get("alias", "")
canonical_alt_aliases = canonical_alias_event.content.get(
"alt_aliases", []
)
# If alt_aliases is not a list, overwrite it as whatever is there
# cannot be handled.
if not isinstance(canonical_alt_aliases, (list, tuple)):
clokep marked this conversation as resolved.
Show resolved Hide resolved
canonical_alt_aliases = []

# first we try to remove the aliases from the old room (we suppress sending
# the room_aliases event until the end).
Expand Down Expand Up @@ -488,19 +496,6 @@ def _move_aliases_to_new_room(
if not removed_aliases:
return

try:
# this can fail if, for some reason, our user doesn't have perms to send
# m.room.aliases events in the old room (note that we've already checked that
# they have perms to send a tombstone event, so that's not terribly likely).
#
# If that happens, it's regrettable, but we should carry on: it's the same
# as when you remove an alias from the directory normally - it just means that
# the aliases event gets out of sync with the directory
# (cf https://github.com/vector-im/riot-web/issues/2369)
yield directory_handler.send_room_alias_update_event(requester, old_room_id)
except AuthError as e:
logger.warning("Failed to send updated alias event on old room: %s", e)

# we can now add any aliases we successfully removed to the new room.
for alias in removed_aliases:
try:
Expand All @@ -517,21 +512,30 @@ def _move_aliases_to_new_room(
# checking module decides it shouldn't, or similar.
logger.error("Error adding alias %s to new room: %s", alias, e)

# The canonical alias and alternative aliases can only be propagated to
# the new room if they were removed from the old room.
content = {}
if canonical_alias and (canonical_alias in removed_aliases):
content["alias"] = canonical_alias
canonical_alt_aliases = [
alias for alias in canonical_alt_aliases if alias in removed_aliases
]
if canonical_alt_aliases:
content["alt_aliases"] = canonical_alt_aliases

clokep marked this conversation as resolved.
Show resolved Hide resolved
try:
if canonical_alias and (canonical_alias in removed_aliases):
if content:
yield self.event_creation_handler.create_and_send_nonmember_event(
requester,
{
"type": EventTypes.CanonicalAlias,
"state_key": "",
"room_id": new_room_id,
"sender": requester.user.to_string(),
"content": {"alias": canonical_alias},
"content": content,
},
ratelimit=False,
)

yield directory_handler.send_room_alias_update_event(requester, new_room_id)
except SynapseError as e:
# again I'm not really expecting this to fail, but if it does, I'd rather
# we returned the new room to the client at this point.
Expand Down Expand Up @@ -757,7 +761,6 @@ def create_room(self, requester, config, ratelimit=True, creator_join_profile=No

if room_alias:
result["room_alias"] = room_alias.to_string()
yield directory_handler.send_room_alias_update_event(requester, room_id)

return result

Expand Down