Merge branch 'cross-signing_hidden' into cross-signing_keys

pull/5769/head
Hubert Chathi 2019-07-30 23:21:26 -04:00
commit bc95890822
4 changed files with 18 additions and 21 deletions

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

@ -0,0 +1 @@
Allow devices to be marked as hidden, for use by features such as cross-signing.

View File

@ -37,9 +37,9 @@ DROP_DEVICE_LIST_STREAMS_NON_UNIQUE_INDEXES = (
class DeviceWorkerStore(SQLBaseStore): class DeviceWorkerStore(SQLBaseStore):
@defer.inlineCallbacks
def get_device(self, user_id, device_id): def get_device(self, user_id, device_id):
"""Retrieve a device. """Retrieve a device. Only returns devices that are not marked as
hidden.
Args: Args:
user_id (str): The ID of the user which owns the device user_id (str): The ID of the user which owns the device
@ -49,19 +49,17 @@ class DeviceWorkerStore(SQLBaseStore):
Raises: Raises:
StoreError: if the device is not found StoreError: if the device is not found
""" """
ret = yield self._simple_select_one( return self._simple_select_one(
table="devices", table="devices",
keyvalues={"user_id": user_id, "device_id": device_id}, keyvalues={"user_id": user_id, "device_id": device_id, "hidden": False},
retcols=("user_id", "device_id", "display_name", "hidden"), retcols=("user_id", "device_id", "display_name", "hidden"),
desc="get_device", desc="get_device",
) )
if ret["hidden"]:
raise StoreError(404, "No row found (devices)")
return ret
@defer.inlineCallbacks @defer.inlineCallbacks
def get_devices_by_user(self, user_id): def get_devices_by_user(self, user_id):
"""Retrieve all of a user's registered devices. """Retrieve all of a user's registered devices. Only returns devices
that are not marked as hidden.
Args: Args:
user_id (str): user_id (str):
@ -72,12 +70,12 @@ class DeviceWorkerStore(SQLBaseStore):
""" """
devices = yield self._simple_select_list( devices = yield self._simple_select_list(
table="devices", table="devices",
keyvalues={"user_id": user_id}, keyvalues={"user_id": user_id, "hidden": False},
retcols=("user_id", "device_id", "display_name", "hidden"), retcols=("user_id", "device_id", "display_name"),
desc="get_devices_by_user", desc="get_devices_by_user",
) )
defer.returnValue({d["device_id"]: d for d in devices if not d["hidden"]}) defer.returnValue({d["device_id"]: d for d in devices})
@defer.inlineCallbacks @defer.inlineCallbacks
def get_devices_by_remote(self, destination, from_stream_id, limit): def get_devices_by_remote(self, destination, from_stream_id, limit):
@ -662,9 +660,9 @@ class DeviceStore(DeviceWorkerStore, BackgroundUpdateStore):
""" """
sql = """ sql = """
DELETE FROM devices DELETE FROM devices
WHERE user_id = ? AND device_id = ? AND NOT COALESCE(hidden, ?) WHERE user_id = ? AND device_id = ? AND NOT hidden
""" """
yield self._execute("delete_device", None, sql, user_id, device_id, False) yield self._execute("delete_device", None, sql, user_id, device_id)
self.device_id_exists_cache.invalidate((user_id, device_id)) self.device_id_exists_cache.invalidate((user_id, device_id))
@ -683,7 +681,7 @@ class DeviceStore(DeviceWorkerStore, BackgroundUpdateStore):
return return
sql = """ sql = """
DELETE FROM devices DELETE FROM devices
WHERE user_id = ? AND device_id IN (%s) AND NOT COALESCE(hidden, ?) WHERE user_id = ? AND device_id IN (%s) AND NOT hidden
""" % ( """ % (
",".join("?" for _ in device_ids) ",".join("?" for _ in device_ids)
) )
@ -697,7 +695,8 @@ class DeviceStore(DeviceWorkerStore, BackgroundUpdateStore):
self.device_id_exists_cache.invalidate((user_id, device_id)) self.device_id_exists_cache.invalidate((user_id, device_id))
def update_device(self, user_id, device_id, new_display_name=None): def update_device(self, user_id, device_id, new_display_name=None):
"""Update a device. """Update a device. Only updates the device if it is not marked as
hidden.
Args: Args:
user_id (str): The ID of the user which owns the device user_id (str): The ID of the user which owns the device
@ -714,11 +713,9 @@ class DeviceStore(DeviceWorkerStore, BackgroundUpdateStore):
updates["display_name"] = new_display_name updates["display_name"] = new_display_name
if not updates: if not updates:
return defer.succeed(None) return defer.succeed(None)
# FIXME: should only update if hidden is not True. But updating the
# display name of a hidden device should be harmless
return self._simple_update_one( return self._simple_update_one(
table="devices", table="devices",
keyvalues={"user_id": user_id, "device_id": device_id}, keyvalues={"user_id": user_id, "device_id": device_id, "hidden": False},
updatevalues=updates, updatevalues=updates,
desc="update_device", desc="update_device",
) )

View File

@ -89,12 +89,11 @@ class EndToEndKeyWorkerStore(SQLBaseStore):
" k.key_json" " k.key_json"
" FROM devices d" " FROM devices d"
" %s JOIN e2e_device_keys_json k USING (user_id, device_id)" " %s JOIN e2e_device_keys_json k USING (user_id, device_id)"
" WHERE (%s) AND NOT COALESCE(d.hidden, ?)" " WHERE %s AND NOT d.hidden"
) % ( ) % (
"LEFT" if include_all_devices else "INNER", "LEFT" if include_all_devices else "INNER",
" OR ".join("(" + q + ")" for q in query_clauses), " OR ".join("(" + q + ")" for q in query_clauses),
) )
query_params.append(False)
txn.execute(sql, query_params) txn.execute(sql, query_params)
rows = self.cursor_to_dict(txn) rows = self.cursor_to_dict(txn)

View File

@ -56,4 +56,4 @@ CREATE UNIQUE INDEX user_signature_stream_idx ON user_signature_stream(stream_id
-- device list needs to know which ones are "real" devices, and which ones are -- device list needs to know which ones are "real" devices, and which ones are
-- just used to avoid collisions -- just used to avoid collisions
ALTER TABLE devices ADD COLUMN hidden BOOLEAN NULLABLE; ALTER TABLE devices ADD COLUMN hidden BOOLEAN DEFAULT FALSE;