From 2cb152bdb1842bd7df6568ae4165965c445bb3f5 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Wed, 23 Nov 2022 12:57:58 +0300 Subject: [PATCH] some-fixes-chats --- auth/authenticate.py | 3 +++ auth/identity.py | 1 + auth/tokenstorage.py | 4 +++- base/redis.py | 9 +++++++-- resolvers/auth.py | 1 + resolvers/inbox/load.py | 30 ++++++++++++++++++------------ services/stat/reacted.py | 4 +++- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/auth/authenticate.py b/auth/authenticate.py index 4e8248c7..958560cb 100644 --- a/auth/authenticate.py +++ b/auth/authenticate.py @@ -28,6 +28,7 @@ class SessionToken: token is of specified type """ try: + print('[auth.authenticate] session token verify') payload = JWTCodec.decode(token) except ExpiredSignatureError: payload = JWTCodec.decode(token, verify_exp=False) @@ -58,6 +59,8 @@ class JWTAuthenticate(AuthenticationBackend): try: payload = await SessionToken.verify(token) except Exception as exc: + print("[auth.authenticate] session token verify error") + print(exc) return AuthCredentials(scopes=[], error_message=str(exc)), AuthUser( user_id=None ) diff --git a/auth/identity.py b/auth/identity.py index 197f9fd7..efcec0ec 100644 --- a/auth/identity.py +++ b/auth/identity.py @@ -81,6 +81,7 @@ class Identity: @staticmethod async def onetime(token: str) -> User: try: + print('[auth.identity] using one time token') payload = JWTCodec.decode(token) if not await TokenStorage.exist(f"{payload.user_id}-{token}"): raise InvalidToken("Login token has expired, please login again") diff --git a/auth/tokenstorage.py b/auth/tokenstorage.py index 2c84ca67..c27c7d97 100644 --- a/auth/tokenstorage.py +++ b/auth/tokenstorage.py @@ -36,11 +36,13 @@ class TokenStorage: @staticmethod async def revoke(token: str) -> bool: + payload = None try: + print("[auth.tokenstorage] revoke token") payload = JWTCodec.decode(token) except: # noqa pass - else: + finally: await redis.execute("DEL", f"{payload.user_id}-{token}") return True diff --git a/base/redis.py b/base/redis.py index 39f5d47a..7468af0a 100644 --- a/base/redis.py +++ b/base/redis.py @@ -1,5 +1,5 @@ from aioredis import from_url - +from asyncio import sleep from settings import REDIS_URL @@ -21,7 +21,12 @@ class RedisCache: self._instance = None async def execute(self, command, *args, **kwargs): - return await self._instance.execute_command(command, *args, **kwargs) + while not self._instance: + await sleep(1) + try: + await self._instance.execute_command(command, *args, **kwargs) + except Exception: + pass async def lrange(self, key, start, stop): return await self._instance.lrange(key, start, stop) diff --git a/resolvers/auth.py b/resolvers/auth.py index b2923ce3..d15793f5 100644 --- a/resolvers/auth.py +++ b/resolvers/auth.py @@ -42,6 +42,7 @@ async def get_current_user(_, info): async def confirm_email(_, info, token): """confirm owning email address""" try: + print('[resolvers.auth] confirm email by token') payload = JWTCodec.decode(token) user_id = payload.user_id await TokenStorage.get(f"{user_id}-{token}") diff --git a/resolvers/inbox/load.py b/resolvers/inbox/load.py index 264e5c24..d3ca0ff1 100644 --- a/resolvers/inbox/load.py +++ b/resolvers/inbox/load.py @@ -33,18 +33,24 @@ async def load_messages(chatId: str, limit: int, offset: int): async def load_chats(_, info, limit: int, offset: int): """ load :limit chats of current user with :offset """ user = info.context["request"].user - chats = await redis.execute("GET", f"chats_by_user/{user.slug}") - if chats: - chats = list(json.loads(chats))[offset:offset + limit] - if not chats: - chats = [] - for c in chats: - c['messages'] = await load_messages(c['id'], limit, offset) - c['unread'] = await get_unread_counter(c['id'], user.slug) - return { - "chats": chats, - "error": None - } + if user: + chats = await redis.execute("GET", f"chats_by_user/{user.slug}") + if chats: + chats = list(json.loads(chats))[offset:offset + limit] + if not chats: + chats = [] + for c in chats: + c['messages'] = await load_messages(c['id'], limit, offset) + c['unread'] = await get_unread_counter(c['id'], user.slug) + return { + "chats": chats, + "error": None + } + else: + return { + "error": "please login", + "chats": [] + } @query.field("loadMessagesBy") diff --git a/services/stat/reacted.py b/services/stat/reacted.py index 00f4fb66..159122b9 100644 --- a/services/stat/reacted.py +++ b/services/stat/reacted.py @@ -35,8 +35,10 @@ class ReactedStorage: @staticmethod async def get_shout_stat(slug): + viewed = await ViewedStorage.get_shout(slug) + print(viewed) return { - "viewed": await ViewedStorage.get_shout(slug), + "viewed": viewed, "reacted": len(await ReactedStorage.get_shout(slug)), "commented": len(await ReactedStorage.get_comments(slug)), "rating": await ReactedStorage.get_rating(slug),