Make /sync attempt to return device updates for both joined and invited users (#3484)

This commit is contained in:
Matthew Hodgson 2019-05-16 13:23:43 +01:00 committed by Richard van der Hoff
parent fafb936de5
commit 4a6d5de98c
2 changed files with 26 additions and 19 deletions

1
changelog.d/3484.misc Normal file
View File

@ -0,0 +1 @@
Make /sync attempt to return device updates for both joined and invited users. Note that this doesn't currently work correctly due to other bugs.

View File

@ -934,7 +934,7 @@ class SyncHandler(object):
res = yield self._generate_sync_entry_for_rooms( res = yield self._generate_sync_entry_for_rooms(
sync_result_builder, account_data_by_room sync_result_builder, account_data_by_room
) )
newly_joined_rooms, newly_joined_users, _, _ = res newly_joined_rooms, newly_joined_or_invited_users, _, _ = res
_, _, newly_left_rooms, newly_left_users = res _, _, newly_left_rooms, newly_left_users = res
block_all_presence_data = ( block_all_presence_data = (
@ -943,7 +943,7 @@ class SyncHandler(object):
) )
if self.hs_config.use_presence and not block_all_presence_data: if self.hs_config.use_presence and not block_all_presence_data:
yield self._generate_sync_entry_for_presence( yield self._generate_sync_entry_for_presence(
sync_result_builder, newly_joined_rooms, newly_joined_users sync_result_builder, newly_joined_rooms, newly_joined_or_invited_users
) )
yield self._generate_sync_entry_for_to_device(sync_result_builder) yield self._generate_sync_entry_for_to_device(sync_result_builder)
@ -951,7 +951,7 @@ class SyncHandler(object):
device_lists = yield self._generate_sync_entry_for_device_list( device_lists = yield self._generate_sync_entry_for_device_list(
sync_result_builder, sync_result_builder,
newly_joined_rooms=newly_joined_rooms, newly_joined_rooms=newly_joined_rooms,
newly_joined_users=newly_joined_users, newly_joined_or_invited_users=newly_joined_or_invited_users,
newly_left_rooms=newly_left_rooms, newly_left_rooms=newly_left_rooms,
newly_left_users=newly_left_users, newly_left_users=newly_left_users,
) )
@ -1036,7 +1036,8 @@ class SyncHandler(object):
@measure_func("_generate_sync_entry_for_device_list") @measure_func("_generate_sync_entry_for_device_list")
@defer.inlineCallbacks @defer.inlineCallbacks
def _generate_sync_entry_for_device_list(self, sync_result_builder, def _generate_sync_entry_for_device_list(self, sync_result_builder,
newly_joined_rooms, newly_joined_users, newly_joined_rooms,
newly_joined_or_invited_users,
newly_left_rooms, newly_left_users): newly_left_rooms, newly_left_users):
user_id = sync_result_builder.sync_config.user.to_string() user_id = sync_result_builder.sync_config.user.to_string()
since_token = sync_result_builder.since_token since_token = sync_result_builder.since_token
@ -1050,7 +1051,7 @@ class SyncHandler(object):
# share a room with? # share a room with?
for room_id in newly_joined_rooms: for room_id in newly_joined_rooms:
joined_users = yield self.state.get_current_users_in_room(room_id) joined_users = yield self.state.get_current_users_in_room(room_id)
newly_joined_users.update(joined_users) newly_joined_or_invited_users.update(joined_users)
for room_id in newly_left_rooms: for room_id in newly_left_rooms:
left_users = yield self.state.get_current_users_in_room(room_id) left_users = yield self.state.get_current_users_in_room(room_id)
@ -1058,7 +1059,7 @@ class SyncHandler(object):
# TODO: Check that these users are actually new, i.e. either they # TODO: Check that these users are actually new, i.e. either they
# weren't in the previous sync *or* they left and rejoined. # weren't in the previous sync *or* they left and rejoined.
changed.update(newly_joined_users) changed.update(newly_joined_or_invited_users)
if not changed and not newly_left_users: if not changed and not newly_left_users:
defer.returnValue(DeviceLists( defer.returnValue(DeviceLists(
@ -1176,7 +1177,7 @@ class SyncHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def _generate_sync_entry_for_presence(self, sync_result_builder, newly_joined_rooms, def _generate_sync_entry_for_presence(self, sync_result_builder, newly_joined_rooms,
newly_joined_users): newly_joined_or_invited_users):
"""Generates the presence portion of the sync response. Populates the """Generates the presence portion of the sync response. Populates the
`sync_result_builder` with the result. `sync_result_builder` with the result.
@ -1184,8 +1185,9 @@ class SyncHandler(object):
sync_result_builder(SyncResultBuilder) sync_result_builder(SyncResultBuilder)
newly_joined_rooms(list): List of rooms that the user has joined newly_joined_rooms(list): List of rooms that the user has joined
since the last sync (or empty if an initial sync) since the last sync (or empty if an initial sync)
newly_joined_users(list): List of users that have joined rooms newly_joined_or_invited_users(list): List of users that have joined
since the last sync (or empty if an initial sync) or been invited to rooms since the last sync (or empty if an initial
sync)
""" """
now_token = sync_result_builder.now_token now_token = sync_result_builder.now_token
sync_config = sync_result_builder.sync_config sync_config = sync_result_builder.sync_config
@ -1211,7 +1213,7 @@ class SyncHandler(object):
"presence_key", presence_key "presence_key", presence_key
) )
extra_users_ids = set(newly_joined_users) extra_users_ids = set(newly_joined_or_invited_users)
for room_id in newly_joined_rooms: for room_id in newly_joined_rooms:
users = yield self.state.get_current_users_in_room(room_id) users = yield self.state.get_current_users_in_room(room_id)
extra_users_ids.update(users) extra_users_ids.update(users)
@ -1243,7 +1245,8 @@ class SyncHandler(object):
Returns: Returns:
Deferred(tuple): Returns a 4-tuple of Deferred(tuple): Returns a 4-tuple of
`(newly_joined_rooms, newly_joined_users, newly_left_rooms, newly_left_users)` `(newly_joined_rooms, newly_joined_or_invited_users,
newly_left_rooms, newly_left_users)`
""" """
user_id = sync_result_builder.sync_config.user.to_string() user_id = sync_result_builder.sync_config.user.to_string()
block_all_room_ephemeral = ( block_all_room_ephemeral = (
@ -1314,8 +1317,8 @@ class SyncHandler(object):
sync_result_builder.invited.extend(invited) sync_result_builder.invited.extend(invited)
# Now we want to get any newly joined users # Now we want to get any newly joined or invited users
newly_joined_users = set() newly_joined_or_invited_users = set()
newly_left_users = set() newly_left_users = set()
if since_token: if since_token:
for joined_sync in sync_result_builder.joined: for joined_sync in sync_result_builder.joined:
@ -1324,19 +1327,22 @@ class SyncHandler(object):
) )
for event in it: for event in it:
if event.type == EventTypes.Member: if event.type == EventTypes.Member:
if event.membership == Membership.JOIN: if (
newly_joined_users.add(event.state_key) event.membership == Membership.JOIN or
event.membership == Membership.INVITE
):
newly_joined_or_invited_users.add(event.state_key)
else: else:
prev_content = event.unsigned.get("prev_content", {}) prev_content = event.unsigned.get("prev_content", {})
prev_membership = prev_content.get("membership", None) prev_membership = prev_content.get("membership", None)
if prev_membership == Membership.JOIN: if prev_membership == Membership.JOIN:
newly_left_users.add(event.state_key) newly_left_users.add(event.state_key)
newly_left_users -= newly_joined_users newly_left_users -= newly_joined_or_invited_users
defer.returnValue(( defer.returnValue((
newly_joined_rooms, newly_joined_rooms,
newly_joined_users, newly_joined_or_invited_users,
newly_left_rooms, newly_left_rooms,
newly_left_users, newly_left_users,
)) ))
@ -1381,7 +1387,7 @@ class SyncHandler(object):
where: where:
room_entries is a list [RoomSyncResultBuilder] room_entries is a list [RoomSyncResultBuilder]
invited_rooms is a list [InvitedSyncResult] invited_rooms is a list [InvitedSyncResult]
newly_joined rooms is a list[str] of room ids newly_joined_rooms is a list[str] of room ids
newly_left_rooms is a list[str] of room ids newly_left_rooms is a list[str] of room ids
""" """
user_id = sync_result_builder.sync_config.user.to_string() user_id = sync_result_builder.sync_config.user.to_string()
@ -1422,7 +1428,7 @@ class SyncHandler(object):
if room_id in sync_result_builder.joined_room_ids and non_joins: if room_id in sync_result_builder.joined_room_ids and non_joins:
# Always include if the user (re)joined the room, especially # Always include if the user (re)joined the room, especially
# important so that device list changes are calculated correctly. # important so that device list changes are calculated correctly.
# If there are non join member events, but we are still in the room, # If there are non-join member events, but we are still in the room,
# then the user must have left and joined # then the user must have left and joined
newly_joined_rooms.append(room_id) newly_joined_rooms.append(room_id)