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

Commit

Permalink
Make /sync attempt to return device updates for both joined and invit…
Browse files Browse the repository at this point in the history
…ed users (#3484)
  • Loading branch information
ara4n authored and richvdh committed May 16, 2019
1 parent fafb936 commit 4a6d5de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog.d/3484.misc
Original file line number Diff line number Diff line change
@@ -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.
44 changes: 25 additions & 19 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ def generate_sync_result(self, sync_config, since_token=None, full_state=False):
res = yield self._generate_sync_entry_for_rooms(
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

block_all_presence_data = (
Expand All @@ -943,15 +943,15 @@ def generate_sync_result(self, sync_config, since_token=None, full_state=False):
)
if self.hs_config.use_presence and not block_all_presence_data:
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)

device_lists = yield self._generate_sync_entry_for_device_list(
sync_result_builder,
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_users=newly_left_users,
)
Expand Down Expand Up @@ -1036,7 +1036,8 @@ def _generate_sync_entry_for_groups(self, sync_result_builder):
@measure_func("_generate_sync_entry_for_device_list")
@defer.inlineCallbacks
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):
user_id = sync_result_builder.sync_config.user.to_string()
since_token = sync_result_builder.since_token
Expand All @@ -1050,15 +1051,15 @@ def _generate_sync_entry_for_device_list(self, sync_result_builder,
# share a room with?
for room_id in newly_joined_rooms:
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:
left_users = yield self.state.get_current_users_in_room(room_id)
newly_left_users.update(left_users)

# TODO: Check that these users are actually new, i.e. either they
# 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:
defer.returnValue(DeviceLists(
Expand Down Expand Up @@ -1176,16 +1177,17 @@ def _generate_sync_entry_for_account_data(self, sync_result_builder):

@defer.inlineCallbacks
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
`sync_result_builder` with the result.
Args:
sync_result_builder(SyncResultBuilder)
newly_joined_rooms(list): List of rooms that the user has joined
since the last sync (or empty if an initial sync)
newly_joined_users(list): List of users that have joined rooms
since the last sync (or empty if an initial sync)
newly_joined_or_invited_users(list): List of users that have joined
or been invited to rooms since the last sync (or empty if an initial
sync)
"""
now_token = sync_result_builder.now_token
sync_config = sync_result_builder.sync_config
Expand All @@ -1211,7 +1213,7 @@ def _generate_sync_entry_for_presence(self, sync_result_builder, newly_joined_ro
"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:
users = yield self.state.get_current_users_in_room(room_id)
extra_users_ids.update(users)
Expand Down Expand Up @@ -1243,7 +1245,8 @@ def _generate_sync_entry_for_rooms(self, sync_result_builder, account_data_by_ro
Returns:
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()
block_all_room_ephemeral = (
Expand Down Expand Up @@ -1314,8 +1317,8 @@ def handle_room_entries(room_entry):

sync_result_builder.invited.extend(invited)

# Now we want to get any newly joined users
newly_joined_users = set()
# Now we want to get any newly joined or invited users
newly_joined_or_invited_users = set()
newly_left_users = set()
if since_token:
for joined_sync in sync_result_builder.joined:
Expand All @@ -1324,19 +1327,22 @@ def handle_room_entries(room_entry):
)
for event in it:
if event.type == EventTypes.Member:
if event.membership == Membership.JOIN:
newly_joined_users.add(event.state_key)
if (
event.membership == Membership.JOIN or
event.membership == Membership.INVITE
):
newly_joined_or_invited_users.add(event.state_key)
else:
prev_content = event.unsigned.get("prev_content", {})
prev_membership = prev_content.get("membership", None)
if prev_membership == Membership.JOIN:
newly_left_users.add(event.state_key)

newly_left_users -= newly_joined_users
newly_left_users -= newly_joined_or_invited_users

defer.returnValue((
newly_joined_rooms,
newly_joined_users,
newly_joined_or_invited_users,
newly_left_rooms,
newly_left_users,
))
Expand Down Expand Up @@ -1381,7 +1387,7 @@ def _get_rooms_changed(self, sync_result_builder, ignored_users):
where:
room_entries is a list [RoomSyncResultBuilder]
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
"""
user_id = sync_result_builder.sync_config.user.to_string()
Expand Down Expand Up @@ -1422,7 +1428,7 @@ def _get_rooms_changed(self, sync_result_builder, ignored_users):
if room_id in sync_result_builder.joined_room_ids and non_joins:
# Always include if the user (re)joined the room, especially
# 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
newly_joined_rooms.append(room_id)

Expand Down

0 comments on commit 4a6d5de

Please sign in to comment.