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

Commit

Permalink
Merge pull request #3520 from matrix-org/matthew/sync_deleted_devices
Browse files Browse the repository at this point in the history
Announce deleted devices explicitly over federation.
  • Loading branch information
ara4n authored Jul 23, 2018
2 parents 3132b89 + 9b34f3e commit 354a99c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions changelog.d/3520.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correctly announce deleted devices over federation
2 changes: 1 addition & 1 deletion synapse/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def _notify_app_services(self, room_stream_id):
logger.exception("Error notifying application services of event")

def on_new_event(self, stream_key, new_token, users=[], rooms=[]):
""" Used to inform listeners that something has happend event wise.
""" Used to inform listeners that something has happened event wise.
Will wake up all listeners for the given users and rooms.
"""
Expand Down
53 changes: 35 additions & 18 deletions synapse/storage/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,31 @@ def update_remote_device_list_cache_entry(self, user_id, device_id, content,

def _update_remote_device_list_cache_entry_txn(self, txn, user_id, device_id,
content, stream_id):
self._simple_upsert_txn(
txn,
table="device_lists_remote_cache",
keyvalues={
"user_id": user_id,
"device_id": device_id,
},
values={
"content": json.dumps(content),
}
)
if content.get("deleted"):
self._simple_delete_txn(
txn,
table="device_lists_remote_cache",
keyvalues={
"user_id": user_id,
"device_id": device_id,
},
)

txn.call_after(
self.device_id_exists_cache.invalidate, (user_id, device_id,)
)
else:
self._simple_upsert_txn(
txn,
table="device_lists_remote_cache",
keyvalues={
"user_id": user_id,
"device_id": device_id,
},
values={
"content": json.dumps(content),
}
)

txn.call_after(self._get_cached_user_device.invalidate, (user_id, device_id,))
txn.call_after(self._get_cached_devices_for_user.invalidate, (user_id,))
Expand Down Expand Up @@ -366,7 +380,7 @@ def _get_devices_by_remote_txn(self, txn, destination, from_stream_id,
now_stream_id = max(stream_id for stream_id in itervalues(query_map))

devices = self._get_e2e_device_keys_txn(
txn, query_map.keys(), include_all_devices=True
txn, query_map.keys(), include_all_devices=True, include_deleted_devices=True
)

prev_sent_id_sql = """
Expand All @@ -393,12 +407,15 @@ def _get_devices_by_remote_txn(self, txn, destination, from_stream_id,

prev_id = stream_id

key_json = device.get("key_json", None)
if key_json:
result["keys"] = json.loads(key_json)
device_display_name = device.get("device_display_name", None)
if device_display_name:
result["device_display_name"] = device_display_name
if device is not None:
key_json = device.get("key_json", None)
if key_json:
result["keys"] = json.loads(key_json)
device_display_name = device.get("device_display_name", None)
if device_display_name:
result["device_display_name"] = device_display_name
else:
result["deleted"] = True

results.append(result)

Expand Down
27 changes: 24 additions & 3 deletions synapse/storage/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ def _set_e2e_device_keys_txn(txn):
)

@defer.inlineCallbacks
def get_e2e_device_keys(self, query_list, include_all_devices=False):
def get_e2e_device_keys(
self, query_list, include_all_devices=False,
include_deleted_devices=False,
):
"""Fetch a list of device keys.
Args:
query_list(list): List of pairs of user_ids and device_ids.
include_all_devices (bool): whether to include entries for devices
that don't have device keys
include_deleted_devices (bool): whether to include null entries for
devices which no longer exist (but were in the query_list).
This option only takes effect if include_all_devices is true.
Returns:
Dict mapping from user-id to dict mapping from device_id to
dict containing "key_json", "device_display_name".
Expand All @@ -79,7 +85,7 @@ def get_e2e_device_keys(self, query_list, include_all_devices=False):

results = yield self.runInteraction(
"get_e2e_device_keys", self._get_e2e_device_keys_txn,
query_list, include_all_devices,
query_list, include_all_devices, include_deleted_devices,
)

for user_id, device_keys in iteritems(results):
Expand All @@ -88,10 +94,19 @@ def get_e2e_device_keys(self, query_list, include_all_devices=False):

defer.returnValue(results)

def _get_e2e_device_keys_txn(self, txn, query_list, include_all_devices):
def _get_e2e_device_keys_txn(
self, txn, query_list, include_all_devices=False,
include_deleted_devices=False,
):
query_clauses = []
query_params = []

if include_all_devices is False:
include_deleted_devices = False

if include_deleted_devices:
deleted_devices = set(query_list)

for (user_id, device_id) in query_list:
query_clause = "user_id = ?"
query_params.append(user_id)
Expand Down Expand Up @@ -119,8 +134,14 @@ def _get_e2e_device_keys_txn(self, txn, query_list, include_all_devices):

result = {}
for row in rows:
if include_deleted_devices:
deleted_devices.remove((row["user_id"], row["device_id"]))
result.setdefault(row["user_id"], {})[row["device_id"]] = row

if include_deleted_devices:
for user_id, device_id in deleted_devices:
result.setdefault(user_id, {})[device_id] = None

return result

@defer.inlineCallbacks
Expand Down

0 comments on commit 354a99c

Please sign in to comment.