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

Stop emitting room alias update events during room creation/upgrade.
This commit is contained in:
Patrick Cloke 2020-02-20 16:24:04 -05:00 committed by GitHub
parent a90d0dc5c2
commit 99eed85a77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 40 deletions

1
changelog.d/6941.removal Normal file
View File

@ -0,0 +1 @@
Stop sending m.room.aliases events during room creation and upgrade.

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
import collections
import logging import logging
import string import string
from typing import List from typing import List
@ -282,22 +283,6 @@ class DirectoryHandler(BaseHandler):
Codes.NOT_FOUND, 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 @defer.inlineCallbacks
def _update_canonical_alias(self, requester, user_id, room_id, room_alias): def _update_canonical_alias(self, requester, user_id, room_id, room_alias):
""" """
@ -326,7 +311,7 @@ class DirectoryHandler(BaseHandler):
alt_aliases = content.pop("alt_aliases", None) alt_aliases = content.pop("alt_aliases", None)
# If the aliases are not a list (or not found) do not attempt to modify # If the aliases are not a list (or not found) do not attempt to modify
# the list. # the list.
if isinstance(alt_aliases, list): if isinstance(alt_aliases, collections.Sequence):
send_update = True send_update = True
alt_aliases = [alias for alias in alt_aliases if alias != alias_str] alt_aliases = [alias for alias in alt_aliases if alias != alias_str]
if alt_aliases: if alt_aliases:

View File

@ -149,7 +149,9 @@ class RoomCreationHandler(BaseHandler):
return ret return ret
@defer.inlineCallbacks @defer.inlineCallbacks
def _upgrade_room(self, requester, old_room_id, new_version): def _upgrade_room(
self, requester: Requester, old_room_id: str, new_version: RoomVersion
):
user_id = requester.user.to_string() user_id = requester.user.to_string()
# start by allocating a new room id # start by allocating a new room id
@ -448,19 +450,21 @@ class RoomCreationHandler(BaseHandler):
@defer.inlineCallbacks @defer.inlineCallbacks
def _move_aliases_to_new_room( def _move_aliases_to_new_room(
self, requester, old_room_id, new_room_id, old_room_state self,
requester: Requester,
old_room_id: str,
new_room_id: str,
old_room_state: StateMap[str],
): ):
directory_handler = self.hs.get_handlers().directory_handler directory_handler = self.hs.get_handlers().directory_handler
aliases = yield self.store.get_aliases_for_room(old_room_id) aliases = yield self.store.get_aliases_for_room(old_room_id)
# check to see if we have a canonical alias. # check to see if we have a canonical alias.
canonical_alias = None canonical_alias_event = None
canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, "")) canonical_alias_event_id = old_room_state.get((EventTypes.CanonicalAlias, ""))
if canonical_alias_event_id: if canonical_alias_event_id:
canonical_alias_event = yield self.store.get_event(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", "")
# first we try to remove the aliases from the old room (we suppress sending # first we try to remove the aliases from the old room (we suppress sending
# the room_aliases event until the end). # the room_aliases event until the end).
@ -488,19 +492,6 @@ class RoomCreationHandler(BaseHandler):
if not removed_aliases: if not removed_aliases:
return 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. # we can now add any aliases we successfully removed to the new room.
for alias in removed_aliases: for alias in removed_aliases:
try: try:
@ -517,8 +508,10 @@ class RoomCreationHandler(BaseHandler):
# checking module decides it shouldn't, or similar. # checking module decides it shouldn't, or similar.
logger.error("Error adding alias %s to new room: %s", alias, e) logger.error("Error adding alias %s to new room: %s", alias, e)
# If a canonical alias event existed for the old room, fire a canonical
# alias event for the new room with a copy of the information.
try: try:
if canonical_alias and (canonical_alias in removed_aliases): if canonical_alias_event:
yield self.event_creation_handler.create_and_send_nonmember_event( yield self.event_creation_handler.create_and_send_nonmember_event(
requester, requester,
{ {
@ -526,12 +519,10 @@ class RoomCreationHandler(BaseHandler):
"state_key": "", "state_key": "",
"room_id": new_room_id, "room_id": new_room_id,
"sender": requester.user.to_string(), "sender": requester.user.to_string(),
"content": {"alias": canonical_alias}, "content": canonical_alias_event.content,
}, },
ratelimit=False, ratelimit=False,
) )
yield directory_handler.send_room_alias_update_event(requester, new_room_id)
except SynapseError as e: except SynapseError as e:
# again I'm not really expecting this to fail, but if it does, I'd rather # 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. # we returned the new room to the client at this point.
@ -757,7 +748,6 @@ class RoomCreationHandler(BaseHandler):
if room_alias: if room_alias:
result["room_alias"] = room_alias.to_string() result["room_alias"] = room_alias.to_string()
yield directory_handler.send_room_alias_update_event(requester, room_id)
return result return result