some-fixes-chats

This commit is contained in:
tonyrewin 2022-11-23 12:57:58 +03:00
parent 5ecb0d811b
commit 2cb152bdb1
7 changed files with 36 additions and 16 deletions

View File

@ -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
)

View File

@ -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")

View File

@ -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

View File

@ -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)

View File

@ -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}")

View File

@ -33,6 +33,7 @@ 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
if user:
chats = await redis.execute("GET", f"chats_by_user/{user.slug}")
if chats:
chats = list(json.loads(chats))[offset:offset + limit]
@ -45,6 +46,11 @@ async def load_chats(_, info, limit: int, offset: int):
"chats": chats,
"error": None
}
else:
return {
"error": "please login",
"chats": []
}
@query.field("loadMessagesBy")

View File

@ -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),