Query devices table for last seen info.

This is a) simpler than querying user_ips directly and b) means we can
purge older entries from user_ips without losing the required info.

The storage functions now no longer return the access_token, since it
was unused.
This commit is contained in:
Erik Johnston 2019-09-23 16:00:18 +01:00
parent ed80231ade
commit 51d28272e2
2 changed files with 8 additions and 50 deletions

View File

@ -392,19 +392,14 @@ class ClientIpStore(background_updates.BackgroundUpdateStore):
keys giving the column names keys giving the column names
""" """
res = yield self.runInteraction( keyvalues = {"user_id": user_id}
"get_last_client_ip_by_device", if device_id:
self._get_last_client_ip_by_device_txn, keyvalues["device_id"] = device_id
user_id,
device_id, res = yield self._simple_select_list(
retcols=( table="devices",
"user_id", keyvalues=keyvalues,
"access_token", retcols=("user_id", "ip", "user_agent", "device_id", "last_seen"),
"ip",
"user_agent",
"device_id",
"last_seen",
),
) )
ret = {(d["user_id"], d["device_id"]): d for d in res} ret = {(d["user_id"], d["device_id"]): d for d in res}
@ -423,42 +418,6 @@ class ClientIpStore(background_updates.BackgroundUpdateStore):
} }
return ret return ret
@classmethod
def _get_last_client_ip_by_device_txn(cls, txn, user_id, device_id, retcols):
where_clauses = []
bindings = []
if device_id is None:
where_clauses.append("user_id = ?")
bindings.extend((user_id,))
else:
where_clauses.append("(user_id = ? AND device_id = ?)")
bindings.extend((user_id, device_id))
if not where_clauses:
return []
inner_select = (
"SELECT MAX(last_seen) mls, user_id, device_id FROM user_ips "
"WHERE %(where)s "
"GROUP BY user_id, device_id"
) % {"where": " OR ".join(where_clauses)}
sql = (
"SELECT %(retcols)s FROM user_ips "
"JOIN (%(inner_select)s) ips ON"
" user_ips.last_seen = ips.mls AND"
" user_ips.user_id = ips.user_id AND"
" (user_ips.device_id = ips.device_id OR"
" (user_ips.device_id IS NULL AND ips.device_id IS NULL)"
" )"
) % {
"retcols": ",".join("user_ips." + c for c in retcols),
"inner_select": inner_select,
}
txn.execute(sql, bindings)
return cls.cursor_to_dict(txn)
@defer.inlineCallbacks @defer.inlineCallbacks
def get_user_ip_and_agents(self, user): def get_user_ip_and_agents(self, user):
user_id = user.to_string() user_id = user.to_string()

View File

@ -55,7 +55,6 @@ class ClientIpStoreTestCase(unittest.HomeserverTestCase):
{ {
"user_id": user_id, "user_id": user_id,
"device_id": "device_id", "device_id": "device_id",
"access_token": "access_token",
"ip": "ip", "ip": "ip",
"user_agent": "user_agent", "user_agent": "user_agent",
"last_seen": 12345678000, "last_seen": 12345678000,