From 75942af1dbe1a4aa65fc986e5662e12c1337b7de Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 17 Jan 2019 14:11:24 +0000 Subject: [PATCH 01/20] Fix typo --- synapse/handlers/room.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index cb8c5f77dd..8898602eeb 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -125,7 +125,7 @@ class RoomCreationHandler(BaseHandler): ) yield self.auth.check_from_context(tombstone_event, tombstone_context) - yield self.clone_exiting_room( + yield self.clone_existing_room( requester, old_room_id=old_room_id, new_room_id=new_room_id, @@ -230,7 +230,7 @@ class RoomCreationHandler(BaseHandler): ) @defer.inlineCallbacks - def clone_exiting_room( + def clone_existing_room( self, requester, old_room_id, new_room_id, new_room_version, tombstone_event_id, ): From 4ff6d2224543146723e3109681636de73a738f25 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 17 Jan 2019 15:22:03 +0000 Subject: [PATCH 02/20] Preserve DM status of a room on upgrade Signed-off-by: Andrew Morgan --- synapse/handlers/room.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 8898602eeb..3bfba6c744 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -260,8 +260,27 @@ class RoomCreationHandler(BaseHandler): } } + # Copy over whether this room is considered a direct message by this + # user or not + user_account_data = yield self.store.get_account_data_for_user( + user_id, + ) + + if user_account_data: + direct_rooms = user_account_data[0]["m.direct"] + # Check if this room was a DM + if old_room_id in direct_rooms[user_id]: + # Add this room ID to the list of direct rooms + direct_rooms[user_id].append(new_room_id) + + # Add this room ID to the list of direct rooms for this user + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + initial_state = dict() + # Replicate relevant room events types_to_copy = ( (EventTypes.JoinRules, ""), (EventTypes.Name, ""), From 1f18c7cfc9e07cb8e6b2f9818783b2d97bd43c3c Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 17 Jan 2019 15:30:49 +0000 Subject: [PATCH 03/20] Add changelog --- changelog.d/4412.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/4412.bugfix diff --git a/changelog.d/4412.bugfix b/changelog.d/4412.bugfix new file mode 100644 index 0000000000..2388ed5b59 --- /dev/null +++ b/changelog.d/4412.bugfix @@ -0,0 +1 @@ +Copy over whether a room is a direct message or not on room upgrade. \ No newline at end of file From 887ca93a1b3b01d1da2c80549d5d2b1166e0608b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 17 Jan 2019 15:46:39 +0000 Subject: [PATCH 04/20] Prevent crash on user who doesn't have any direct rooms --- synapse/handlers/room.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 3bfba6c744..626c9f9166 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -266,7 +266,7 @@ class RoomCreationHandler(BaseHandler): user_id, ) - if user_account_data: + if user_account_data and "m.direct" in user_account_data[0]: direct_rooms = user_account_data[0]["m.direct"] # Check if this room was a DM if old_room_id in direct_rooms[user_id]: From ea8903fcc904c88027bcec62dc6646377941db41 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 18 Jan 2019 15:12:47 +0000 Subject: [PATCH 05/20] Migrating dm and room tags work for migrator --- synapse/handlers/room.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 626c9f9166..07cf5f3f18 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -260,12 +260,16 @@ class RoomCreationHandler(BaseHandler): } } - # Copy over whether this room is considered a direct message by this - # user or not + # Copy over room account data for this user user_account_data = yield self.store.get_account_data_for_user( user_id, ) + room_tags = yield self.store.get_tags_for_room( + user_id, old_room_id, + ) + + # Copy direct message state if applicable if user_account_data and "m.direct" in user_account_data[0]: direct_rooms = user_account_data[0]["m.direct"] # Check if this room was a DM @@ -278,6 +282,13 @@ class RoomCreationHandler(BaseHandler): user_id, "m.direct", direct_rooms, ) + # Copy room tags if applicable + if room_tags: + # Copy each room tag to the new room + for tag in room_tags.keys(): + tag_content = room_tags[tag] + yield self.store.add_tag_to_room(user_id, new_room_id, tag, tag_content) + initial_state = dict() # Replicate relevant room events From 25d64a846ab4974da0d8a51b3a1ff014a10d319d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 18 Jan 2019 15:27:11 +0000 Subject: [PATCH 06/20] Fix typos --- changelog.d/4412.bugfix | 2 +- synapse/handlers/room_member.py | 16 ++++++++-------- synapse/rest/client/v1/room.py | 8 ++++---- synapse/storage/background_updates.py | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/changelog.d/4412.bugfix b/changelog.d/4412.bugfix index 2388ed5b59..007be1b7db 100644 --- a/changelog.d/4412.bugfix +++ b/changelog.d/4412.bugfix @@ -1 +1 @@ -Copy over whether a room is a direct message or not on room upgrade. \ No newline at end of file +Copy over whether a room is a direct message and any associated room tags on room upgrade. \ No newline at end of file diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 07fd3e82fc..1d337ad95c 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -63,7 +63,7 @@ class RoomMemberHandler(object): self.directory_handler = hs.get_handlers().directory_handler self.registration_handler = hs.get_handlers().registration_handler self.profile_handler = hs.get_profile_handler() - self.event_creation_hander = hs.get_event_creation_handler() + self.event_creation_handler = hs.get_event_creation_handler() self.member_linearizer = Linearizer(name="member") @@ -168,7 +168,7 @@ class RoomMemberHandler(object): if requester.is_guest: content["kind"] = "guest" - event, context = yield self.event_creation_hander.create_event( + event, context = yield self.event_creation_handler.create_event( requester, { "type": EventTypes.Member, @@ -186,14 +186,14 @@ class RoomMemberHandler(object): ) # Check if this event matches the previous membership event for the user. - duplicate = yield self.event_creation_hander.deduplicate_state_event( + duplicate = yield self.event_creation_handler.deduplicate_state_event( event, context, ) if duplicate is not None: # Discard the new event since this membership change is a no-op. defer.returnValue(duplicate) - yield self.event_creation_hander.handle_new_client_event( + yield self.event_creation_handler.handle_new_client_event( requester, event, context, @@ -493,7 +493,7 @@ class RoomMemberHandler(object): else: requester = synapse.types.create_requester(target_user) - prev_event = yield self.event_creation_hander.deduplicate_state_event( + prev_event = yield self.event_creation_handler.deduplicate_state_event( event, context, ) if prev_event is not None: @@ -513,7 +513,7 @@ class RoomMemberHandler(object): if is_blocked: raise SynapseError(403, "This room has been blocked on this server") - yield self.event_creation_hander.handle_new_client_event( + yield self.event_creation_handler.handle_new_client_event( requester, event, context, @@ -527,7 +527,7 @@ class RoomMemberHandler(object): ) if event.membership == Membership.JOIN: - # Only fire user_joined_room if the user has acutally joined the + # Only fire user_joined_room if the user has actually joined the # room. Don't bother if the user is just changing their profile # info. newly_joined = True @@ -755,7 +755,7 @@ class RoomMemberHandler(object): ) ) - yield self.event_creation_hander.create_and_send_nonmember_event( + yield self.event_creation_handler.create_and_send_nonmember_event( requester, { "type": EventTypes.ThirdPartyInvite, diff --git a/synapse/rest/client/v1/room.py b/synapse/rest/client/v1/room.py index fcfe7857f6..48da4d557f 100644 --- a/synapse/rest/client/v1/room.py +++ b/synapse/rest/client/v1/room.py @@ -89,7 +89,7 @@ class RoomStateEventRestServlet(ClientV1RestServlet): def __init__(self, hs): super(RoomStateEventRestServlet, self).__init__(hs) self.handlers = hs.get_handlers() - self.event_creation_hander = hs.get_event_creation_handler() + self.event_creation_handler = hs.get_event_creation_handler() self.room_member_handler = hs.get_room_member_handler() self.message_handler = hs.get_message_handler() @@ -172,7 +172,7 @@ class RoomStateEventRestServlet(ClientV1RestServlet): content=content, ) else: - event = yield self.event_creation_hander.create_and_send_nonmember_event( + event = yield self.event_creation_handler.create_and_send_nonmember_event( requester, event_dict, txn_id=txn_id, @@ -189,7 +189,7 @@ class RoomSendEventRestServlet(ClientV1RestServlet): def __init__(self, hs): super(RoomSendEventRestServlet, self).__init__(hs) - self.event_creation_hander = hs.get_event_creation_handler() + self.event_creation_handler = hs.get_event_creation_handler() def register(self, http_server): # /rooms/$roomid/send/$event_type[/$txn_id] @@ -211,7 +211,7 @@ class RoomSendEventRestServlet(ClientV1RestServlet): if b'ts' in request.args and requester.app_service: event_dict['origin_server_ts'] = parse_integer(request, "ts", 0) - event = yield self.event_creation_hander.create_and_send_nonmember_event( + event = yield self.event_creation_handler.create_and_send_nonmember_event( requester, event_dict, txn_id=txn_id, diff --git a/synapse/storage/background_updates.py b/synapse/storage/background_updates.py index 5fe1ca2de7..60cdc884e6 100644 --- a/synapse/storage/background_updates.py +++ b/synapse/storage/background_updates.py @@ -240,7 +240,7 @@ class BackgroundUpdateStore(SQLBaseStore): * An integer count of the number of items to update in this batch. The handler should return a deferred integer count of items updated. - The hander is responsible for updating the progress of the update. + The handler is responsible for updating the progress of the update. Args: update_name(str): The name of the update that this code handles. From 8c85f0833d568f90d397dad30bc0ba28cf5d538b Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 18 Jan 2019 17:03:09 +0000 Subject: [PATCH 07/20] tags, m.direct copying over correctly --- synapse/handlers/room.py | 20 +++++++------ synapse/handlers/room_member.py | 51 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 07cf5f3f18..9ac04eda50 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -272,15 +272,19 @@ class RoomCreationHandler(BaseHandler): # Copy direct message state if applicable if user_account_data and "m.direct" in user_account_data[0]: direct_rooms = user_account_data[0]["m.direct"] - # Check if this room was a DM - if old_room_id in direct_rooms[user_id]: - # Add this room ID to the list of direct rooms - direct_rooms[user_id].append(new_room_id) - # Add this room ID to the list of direct rooms for this user - yield self.store.add_account_data_for_user( - user_id, "m.direct", direct_rooms, - ) + # Check which key this room is under + for key, room_id_list in direct_rooms.items(): + for room_id in room_id_list: + if room_id == old_room_id: + # Add new room_id to this key + direct_rooms[key].append(new_room_id) + + # Save back to user's m.direct account data + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + break # Copy room tags if applicable if room_tags: diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 1d337ad95c..35dd448c02 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -161,6 +161,8 @@ class RoomMemberHandler(object): ratelimit=True, content=None, ): + user_id = target.to_string() + if content is None: content = {} @@ -175,7 +177,7 @@ class RoomMemberHandler(object): "content": content, "room_id": room_id, "sender": requester.user.to_string(), - "state_key": target.to_string(), + "state_key": user_id, # For backwards compatibility: "membership": membership, @@ -204,7 +206,7 @@ class RoomMemberHandler(object): prev_state_ids = yield context.get_prev_state_ids(self.store) prev_member_event_id = prev_state_ids.get( - (EventTypes.Member, target.to_string()), + (EventTypes.Member, user_id), None ) @@ -218,6 +220,51 @@ class RoomMemberHandler(object): newly_joined = prev_member_event.membership != Membership.JOIN if newly_joined: yield self._user_joined_room(target, room_id) + + # Copy over direct message status and room tags if this is a join + # on an upgraded room + + # Check if this is an upgraded room + state_ids = yield self.store.get_current_state_ids(room_id) + create_id = state_ids.get((EventTypes.Create, "")) + if not create_id: + return + create_event = yield self.store.get_event(create_id) + + if "predecessor" in create_event["content"]: + old_room_id = create_event["content"]["predecessor"]["room_id"] + + # Copy over room account data from predecessor room to upgraded room + user_account_data = yield self.store.get_account_data_for_user( + user_id, + ) + room_tags = yield self.store.get_tags_for_room( + user_id, old_room_id, + ) + + # Copy direct message state if applicable + if user_account_data and "m.direct" in user_account_data[0]: + direct_rooms = user_account_data[0]["m.direct"] + + # Check which key this room is under + for key, room_id_list in direct_rooms.items(): + for rid in room_id_list: + if rid == old_room_id: + # Add new room_id to this key + direct_rooms[key].append(room_id) + + # Save back to user's m.direct account data + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + break + + # Copy room tags if applicable + if room_tags: + # Copy each room tag to the new room + for tag in room_tags.keys(): + tag_content = room_tags[tag] + yield self.store.add_tag_to_room(user_id, room_id, tag, tag_content) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = yield self.store.get_event(prev_member_event_id) From 48951f437fb792d5d4da70c95fa8d20467428604 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 22 Jan 2019 10:52:56 +0000 Subject: [PATCH 08/20] Join logic covers both room creator and arbitrary users --- synapse/handlers/room.py | 33 --------------------------------- 1 file changed, 33 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 9ac04eda50..388302de09 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -260,39 +260,6 @@ class RoomCreationHandler(BaseHandler): } } - # Copy over room account data for this user - user_account_data = yield self.store.get_account_data_for_user( - user_id, - ) - - room_tags = yield self.store.get_tags_for_room( - user_id, old_room_id, - ) - - # Copy direct message state if applicable - if user_account_data and "m.direct" in user_account_data[0]: - direct_rooms = user_account_data[0]["m.direct"] - - # Check which key this room is under - for key, room_id_list in direct_rooms.items(): - for room_id in room_id_list: - if room_id == old_room_id: - # Add new room_id to this key - direct_rooms[key].append(new_room_id) - - # Save back to user's m.direct account data - yield self.store.add_account_data_for_user( - user_id, "m.direct", direct_rooms, - ) - break - - # Copy room tags if applicable - if room_tags: - # Copy each room tag to the new room - for tag in room_tags.keys(): - tag_content = room_tags[tag] - yield self.store.add_tag_to_room(user_id, new_room_id, tag, tag_content) - initial_state = dict() # Replicate relevant room events From 8086a5c05e5ddf7a24662882cbb090ec826110b4 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 22 Jan 2019 10:56:35 +0000 Subject: [PATCH 09/20] Fix comments --- synapse/handlers/room_member.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 35dd448c02..d14da4bc7f 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -211,7 +211,7 @@ class RoomMemberHandler(object): ) if event.membership == Membership.JOIN: - # Only fire user_joined_room if the user has acutally joined the + # Only fire user_joined_room if the user has actually joined the # room. Don't bother if the user is just changing their profile # info. newly_joined = True @@ -234,7 +234,7 @@ class RoomMemberHandler(object): if "predecessor" in create_event["content"]: old_room_id = create_event["content"]["predecessor"]["room_id"] - # Copy over room account data from predecessor room to upgraded room + # Retrieve room account data for predecessor room user_account_data = yield self.store.get_account_data_for_user( user_id, ) From c4875d8c767035036fa1e5c1ee58e9c01ac1e08d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 22 Jan 2019 12:13:46 +0000 Subject: [PATCH 10/20] Prevent duplicate room IDs in m.direct --- synapse/handlers/room_member.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index d14da4bc7f..cdaf65cdef 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -248,16 +248,15 @@ class RoomMemberHandler(object): # Check which key this room is under for key, room_id_list in direct_rooms.items(): - for rid in room_id_list: - if rid == old_room_id: - # Add new room_id to this key - direct_rooms[key].append(room_id) + if old_room_id in room_id_list and room_id not in room_id_list: + # Add new room_id to this key + direct_rooms[key].append(room_id) - # Save back to user's m.direct account data - yield self.store.add_account_data_for_user( - user_id, "m.direct", direct_rooms, - ) - break + # Save back to user's m.direct account data + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + break # Copy room tags if applicable if room_tags: From 766a172b9911ff04ae3b314155462dfd40c76fba Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Tue, 22 Jan 2019 13:23:26 +0000 Subject: [PATCH 11/20] lint --- synapse/handlers/room_member.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index cdaf65cdef..eb46f5f4fa 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -263,7 +263,9 @@ class RoomMemberHandler(object): # Copy each room tag to the new room for tag in room_tags.keys(): tag_content = room_tags[tag] - yield self.store.add_tag_to_room(user_id, room_id, tag, tag_content) + yield self.store.add_tag_to_room( + user_id, room_id, tag, tag_content + ) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = yield self.store.get_event(prev_member_event_id) From 0862d35b8e23151174dac3740c37ddb18e78a74a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:09:34 +0000 Subject: [PATCH 12/20] Move tag and direct state copying into separate function --- synapse/handlers/room_member.py | 106 +++++++++++++++++++------------- 1 file changed, 63 insertions(+), 43 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index eb46f5f4fa..a340566bb0 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -223,49 +223,7 @@ class RoomMemberHandler(object): # Copy over direct message status and room tags if this is a join # on an upgraded room - - # Check if this is an upgraded room - state_ids = yield self.store.get_current_state_ids(room_id) - create_id = state_ids.get((EventTypes.Create, "")) - if not create_id: - return - create_event = yield self.store.get_event(create_id) - - if "predecessor" in create_event["content"]: - old_room_id = create_event["content"]["predecessor"]["room_id"] - - # Retrieve room account data for predecessor room - user_account_data = yield self.store.get_account_data_for_user( - user_id, - ) - room_tags = yield self.store.get_tags_for_room( - user_id, old_room_id, - ) - - # Copy direct message state if applicable - if user_account_data and "m.direct" in user_account_data[0]: - direct_rooms = user_account_data[0]["m.direct"] - - # Check which key this room is under - for key, room_id_list in direct_rooms.items(): - if old_room_id in room_id_list and room_id not in room_id_list: - # Add new room_id to this key - direct_rooms[key].append(room_id) - - # Save back to user's m.direct account data - yield self.store.add_account_data_for_user( - user_id, "m.direct", direct_rooms, - ) - break - - # Copy room tags if applicable - if room_tags: - # Copy each room tag to the new room - for tag in room_tags.keys(): - tag_content = room_tags[tag] - yield self.store.add_tag_to_room( - user_id, room_id, tag, tag_content - ) + self.copy_room_tags_and_direct_to_room(old_room_id, room_id, user_id) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = yield self.store.get_event(prev_member_event_id) @@ -274,6 +232,68 @@ class RoomMemberHandler(object): defer.returnValue(event) + @defer.inlineCallbacks + def copy_room_tags_and_direct_to_room( + self, + old_room_id, + new_room_id, + user_id, + ): + """Copies the tags and direct room state from one room to another. + + Args: + old_room_id (str) + new_room_id (str) + user_id (str) + + Returns: + Deferred|None + """ + # Check if this is an upgraded room + state_ids = yield self.store.get_filtered_current_state_ids( + new_room_id, StateFilter.from_types(((EventTypes.Create, ""))), + ) + create_id = state_ids.get((EventTypes.Create, "")) + if not create_id: + return + create_event = yield self.store.get_event(create_id) + + if "predecessor" in create_event["content"]: + old_room_id = create_event["content"]["predecessor"]["room_id"] + + # Retrieve room account data for predecessor room + user_account_data = yield self.store.get_account_data_for_user( + user_id, + ) + room_tags = yield self.store.get_tags_for_room( + user_id, old_room_id, + ) + + # Copy direct message state if applicable + if user_account_data and "m.direct" in user_account_data[0]: + direct_rooms = user_account_data[0]["m.direct"] + + # Check which key this room is under + for key, room_id_list in direct_rooms.items(): + if old_room_id in room_id_list and new_room_id not in room_id_list: + # Add new room_id to this key + direct_rooms[key].append(new_room_id) + + # Save back to user's m.direct account data + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + break + + # Copy room tags if applicable + if room_tags: + # Copy each room tag to the new room + for tag in room_tags.keys(): + tag_content = room_tags[tag] + yield self.store.add_tag_to_room( + user_id, room_id, tag, tag_content + ) + @defer.inlineCallbacks def update_membership( self, From 6f3fda79ce50059c314b9e1566007062045624b4 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:21:25 +0000 Subject: [PATCH 13/20] Move room_tag declaration to be closer to its use --- synapse/handlers/room_member.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index a340566bb0..67c601b696 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -261,13 +261,10 @@ class RoomMemberHandler(object): if "predecessor" in create_event["content"]: old_room_id = create_event["content"]["predecessor"]["room_id"] - # Retrieve room account data for predecessor room + # Retrieve user account data for predecessor room user_account_data = yield self.store.get_account_data_for_user( user_id, ) - room_tags = yield self.store.get_tags_for_room( - user_id, old_room_id, - ) # Copy direct message state if applicable if user_account_data and "m.direct" in user_account_data[0]: @@ -286,6 +283,10 @@ class RoomMemberHandler(object): break # Copy room tags if applicable + room_tags = yield self.store.get_tags_for_room( + user_id, old_room_id, + ) + if room_tags: # Copy each room tag to the new room for tag in room_tags.keys(): From 516456b763c7652b1401ad502bb6b621f822b15a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:22:14 +0000 Subject: [PATCH 14/20] Remove unnecessary null check --- synapse/handlers/room_member.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 67c601b696..3d4500fb18 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -287,13 +287,12 @@ class RoomMemberHandler(object): user_id, old_room_id, ) - if room_tags: - # Copy each room tag to the new room - for tag in room_tags.keys(): - tag_content = room_tags[tag] - yield self.store.add_tag_to_room( - user_id, room_id, tag, tag_content - ) + # Copy each room tag to the new room + for tag in room_tags.keys(): + tag_content = room_tags[tag] + yield self.store.add_tag_to_room( + user_id, room_id, tag, tag_content + ) @defer.inlineCallbacks def update_membership( From c4cdafa81fdf167961257f16118735b304535f62 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:24:28 +0000 Subject: [PATCH 15/20] Destructure account data tuple before use --- synapse/handlers/room_member.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 3d4500fb18..bde4798d0c 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -262,13 +262,13 @@ class RoomMemberHandler(object): old_room_id = create_event["content"]["predecessor"]["room_id"] # Retrieve user account data for predecessor room - user_account_data = yield self.store.get_account_data_for_user( + user_account_data, _ = yield self.store.get_account_data_for_user( user_id, ) # Copy direct message state if applicable - if user_account_data and "m.direct" in user_account_data[0]: - direct_rooms = user_account_data[0]["m.direct"] + if user_account_data and "m.direct" in user_account_data: + direct_rooms = user_account_data["m.direct"] # Check which key this room is under for key, room_id_list in direct_rooms.items(): From 8265995498e375cefd728b77930ea3e65306c69a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:26:06 +0000 Subject: [PATCH 16/20] Use python magic --- synapse/handlers/room_member.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index bde4798d0c..ee12428fe7 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -288,8 +288,7 @@ class RoomMemberHandler(object): ) # Copy each room tag to the new room - for tag in room_tags.keys(): - tag_content = room_tags[tag] + for tag, tag_content in room_tags.items(): yield self.store.add_tag_to_room( user_id, room_id, tag, tag_content ) From da0d2219d2bdde68165b16d51483357dd234ad1d Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:37:12 +0000 Subject: [PATCH 17/20] Clean up direct_rooms access --- synapse/handlers/room_member.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index ee12428fe7..b9de146913 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -267,10 +267,10 @@ class RoomMemberHandler(object): ) # Copy direct message state if applicable - if user_account_data and "m.direct" in user_account_data: - direct_rooms = user_account_data["m.direct"] + direct_rooms = user_account_data.get("m.direct", {}) - # Check which key this room is under + # Check which key this room is under + if isinstance(direct_rooms, dict): for key, room_id_list in direct_rooms.items(): if old_room_id in room_id_list and new_room_id not in room_id_list: # Add new room_id to this key From 9244a3089eae772c0fca69372a8230d1b5ab9947 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Fri, 25 Jan 2019 11:48:38 +0000 Subject: [PATCH 18/20] Fixes --- synapse/handlers/room_member.py | 80 ++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index b9de146913..aabc488753 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -29,6 +29,7 @@ import synapse.server import synapse.types from synapse.api.constants import EventTypes, Membership from synapse.api.errors import AuthError, Codes, SynapseError +from synapse.storage.state import StateFilter from synapse.types import RoomID, UserID from synapse.util.async_helpers import Linearizer from synapse.util.distributor import user_joined_room, user_left_room @@ -223,7 +224,18 @@ class RoomMemberHandler(object): # Copy over direct message status and room tags if this is a join # on an upgraded room - self.copy_room_tags_and_direct_to_room(old_room_id, room_id, user_id) + # Check if this is an upgraded room + state_ids = yield self.store.get_filtered_current_state_ids( + room_id, StateFilter.from_types([(EventTypes.Create, "")]), + ) + create_id = state_ids.get((EventTypes.Create, "")) + if not create_id: + return + create_event = yield self.store.get_event(create_id) + + if "predecessor" in create_event["content"]: + old_room_id = create_event["content"]["predecessor"]["room_id"] + self.copy_room_tags_and_direct_to_room(old_room_id, room_id, user_id) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = yield self.store.get_event(prev_member_event_id) @@ -249,50 +261,38 @@ class RoomMemberHandler(object): Returns: Deferred|None """ - # Check if this is an upgraded room - state_ids = yield self.store.get_filtered_current_state_ids( - new_room_id, StateFilter.from_types(((EventTypes.Create, ""))), + # Retrieve user account data for predecessor room + user_account_data, _ = yield self.store.get_account_data_for_user( + user_id, ) - create_id = state_ids.get((EventTypes.Create, "")) - if not create_id: - return - create_event = yield self.store.get_event(create_id) - if "predecessor" in create_event["content"]: - old_room_id = create_event["content"]["predecessor"]["room_id"] + # Copy direct message state if applicable + direct_rooms = user_account_data.get("m.direct", {}) - # Retrieve user account data for predecessor room - user_account_data, _ = yield self.store.get_account_data_for_user( - user_id, + # Check which key this room is under + if isinstance(direct_rooms, dict): + for key, room_id_list in direct_rooms.items(): + if old_room_id in room_id_list and new_room_id not in room_id_list: + # Add new room_id to this key + direct_rooms[key].append(new_room_id) + + # Save back to user's m.direct account data + yield self.store.add_account_data_for_user( + user_id, "m.direct", direct_rooms, + ) + break + + # Copy room tags if applicable + room_tags = yield self.store.get_tags_for_room( + user_id, old_room_id, + ) + + # Copy each room tag to the new room + for tag, tag_content in room_tags.items(): + yield self.store.add_tag_to_room( + user_id, new_room_id, tag, tag_content ) - # Copy direct message state if applicable - direct_rooms = user_account_data.get("m.direct", {}) - - # Check which key this room is under - if isinstance(direct_rooms, dict): - for key, room_id_list in direct_rooms.items(): - if old_room_id in room_id_list and new_room_id not in room_id_list: - # Add new room_id to this key - direct_rooms[key].append(new_room_id) - - # Save back to user's m.direct account data - yield self.store.add_account_data_for_user( - user_id, "m.direct", direct_rooms, - ) - break - - # Copy room tags if applicable - room_tags = yield self.store.get_tags_for_room( - user_id, old_room_id, - ) - - # Copy each room tag to the new room - for tag, tag_content in room_tags.items(): - yield self.store.add_tag_to_room( - user_id, room_id, tag, tag_content - ) - @defer.inlineCallbacks def update_membership( self, From 1ce463963db06158d8e740b1986392ef968df0c0 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 28 Jan 2019 14:08:18 +0000 Subject: [PATCH 19/20] Reuse predecessor method --- synapse/handlers/room_member.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index aabc488753..6163c6bdee 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -29,7 +29,6 @@ import synapse.server import synapse.types from synapse.api.constants import EventTypes, Membership from synapse.api.errors import AuthError, Codes, SynapseError -from synapse.storage.state import StateFilter from synapse.types import RoomID, UserID from synapse.util.async_helpers import Linearizer from synapse.util.distributor import user_joined_room, user_left_room @@ -224,18 +223,15 @@ class RoomMemberHandler(object): # Copy over direct message status and room tags if this is a join # on an upgraded room - # Check if this is an upgraded room - state_ids = yield self.store.get_filtered_current_state_ids( - room_id, StateFilter.from_types([(EventTypes.Create, "")]), - ) - create_id = state_ids.get((EventTypes.Create, "")) - if not create_id: - return - create_event = yield self.store.get_event(create_id) - if "predecessor" in create_event["content"]: - old_room_id = create_event["content"]["predecessor"]["room_id"] - self.copy_room_tags_and_direct_to_room(old_room_id, room_id, user_id) + # Check if this is an upgraded room + predecessor = yield self.store.get_room_predecessor(room_id) + + if predecessor: + # It is an upgraded room. Copy over old tags + self.copy_room_tags_and_direct_to_room( + predecessor["room_id"], room_id, user_id, + ) elif event.membership == Membership.LEAVE: if prev_member_event_id: prev_member_event = yield self.store.get_event(prev_member_event_id) From f0e96ab66a09db8ffec34fddf67d088fe03f8ea6 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 28 Jan 2019 14:09:45 +0000 Subject: [PATCH 20/20] Change return syntax in doc string --- synapse/handlers/room_member.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 6163c6bdee..9ed5a05cca 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -255,7 +255,7 @@ class RoomMemberHandler(object): user_id (str) Returns: - Deferred|None + Deferred[None] """ # Retrieve user account data for predecessor room user_account_data, _ = yield self.store.get_account_data_for_user(