From e373450236e05a4be79b42e99615af20e8b8e536 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 10 May 2023 15:28:11 +0300 Subject: auth: Add the /oauth2/user/collections/list endpoint Add an endpoint to list a user's collections. This only works for logged in users. --- gn3/auth/authorisation/users/collections.py | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 gn3/auth/authorisation/users/collections.py (limited to 'gn3/auth/authorisation/users/collections.py') diff --git a/gn3/auth/authorisation/users/collections.py b/gn3/auth/authorisation/users/collections.py new file mode 100644 index 0000000..9ddc138 --- /dev/null +++ b/gn3/auth/authorisation/users/collections.py @@ -0,0 +1,30 @@ +"""Handle user collections.""" +import json + +from redis import Redis + +from .models import User + +def user_collections(rconn: Redis, user: User) -> tuple: + """Retrieve current user collections.""" + return tuple(json.loads( + rconn.hget("collections", str(user.user_id)) or + "[]")) + +def old_user_collections(rconn: Redis, user: User) -> tuple: + """ + Retrieve any old user collections and migrate them to new account. + """ + collections = user_collections(rconn, user) + old_user_accounts = [ + acct for acct in + (json.loads(usr) for usr in rconn.hgetall("users").values()) + if acct.get("email_address", "") == user.email] + for account in old_user_accounts: + collections = collections + tuple(json.loads( + rconn.hget("collections", account["user_id"]) or "[]")) + rconn.hdel("collections", account["user_id"]) + + rconn.hset( + "collections", key=str(user.user_id), value=json.dumps(collections)) + return collections -- cgit v1.2.3