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

Commit

Permalink
Query devices table for last seen info.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
erikjohnston committed Sep 23, 2019
1 parent ed80231 commit 51d2827
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 50 deletions.
57 changes: 8 additions & 49 deletions synapse/storage/client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,14 @@ def get_last_client_ip_by_device(self, user_id, device_id):
keys giving the column names
"""

res = yield self.runInteraction(
"get_last_client_ip_by_device",
self._get_last_client_ip_by_device_txn,
user_id,
device_id,
retcols=(
"user_id",
"access_token",
"ip",
"user_agent",
"device_id",
"last_seen",
),
keyvalues = {"user_id": user_id}
if device_id:
keyvalues["device_id"] = device_id

res = yield self._simple_select_list(
table="devices",
keyvalues=keyvalues,
retcols=("user_id", "ip", "user_agent", "device_id", "last_seen"),
)

ret = {(d["user_id"], d["device_id"]): d for d in res}
Expand All @@ -423,42 +418,6 @@ def get_last_client_ip_by_device(self, user_id, device_id):
}
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
def get_user_ip_and_agents(self, user):
user_id = user.to_string()
Expand Down
1 change: 0 additions & 1 deletion tests/storage/test_client_ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def test_insert_new_client_ip(self):
{
"user_id": user_id,
"device_id": "device_id",
"access_token": "access_token",
"ip": "ip",
"user_agent": "user_agent",
"last_seen": 12345678000,
Expand Down

0 comments on commit 51d2827

Please sign in to comment.