less topicstat

This commit is contained in:
tonyrewin 2022-11-23 16:34:34 +03:00
parent cbd34bbe4e
commit a41af344a7
5 changed files with 15 additions and 15 deletions

View File

@ -9,7 +9,7 @@ from starlette.requests import HTTPConnection
from auth.credentials import AuthCredentials, AuthUser from auth.credentials import AuthCredentials, AuthUser
from auth.jwtcodec import JWTCodec from auth.jwtcodec import JWTCodec
from auth.tokenstorage import TokenStorage from auth.tokenstorage import TokenStorage
from base.exceptions import InvalidToken from base.exceptions import ExpiredToken, InvalidToken
from services.auth.users import UserStorage from services.auth.users import UserStorage
from settings import SESSION_TOKEN_HEADER from settings import SESSION_TOKEN_HEADER
@ -33,12 +33,12 @@ class SessionToken:
except ExpiredSignatureError: except ExpiredSignatureError:
payload = JWTCodec.decode(token, verify_exp=False) payload = JWTCodec.decode(token, verify_exp=False)
if not await cls.get(payload.user_id, token): if not await cls.get(payload.user_id, token):
raise InvalidToken("Session token has expired, please try again") raise ExpiredToken("Token signature has expired, please try again")
except DecodeError as e: except DecodeError as e:
raise InvalidToken("token format error") from e raise InvalidToken("token format error") from e
else: else:
if not await cls.get(payload.user_id, token): if not await cls.get(payload.user_id, token):
raise InvalidToken("Session token has expired, please login again") raise ExpiredToken("Session token has expired, please login again")
return payload return payload
@classmethod @classmethod

View File

@ -8,10 +8,8 @@ from settings import JWT_ALGORITHM, JWT_SECRET_KEY
class JWTCodec: class JWTCodec:
@staticmethod @staticmethod
def encode(user: AuthInput, exp: datetime) -> str: def encode(user: AuthInput, exp: datetime) -> str:
issued = int(datetime.now().timestamp()) expires = int(exp.timestamp() * 1000)
print('[auth.jwtcodec] issued at %r' % issued) issued = int(datetime.now().timestamp() * 1000)
expires = int(exp.timestamp())
print('[auth.jwtcodec] expires at %r' % expires)
payload = { payload = {
"user_id": user.id, "user_id": user.id,
"username": user.email or user.phone, "username": user.email or user.phone,
@ -42,8 +40,10 @@ class JWTCodec:
print('[auth.jwtcodec] debug payload %r' % r) print('[auth.jwtcodec] debug payload %r' % r)
return r return r
except jwt.InvalidIssuedAtError: except jwt.InvalidIssuedAtError:
print('[auth.jwtcodec] invalid issued at: %r' % r)
raise ExpiredToken('check token issued time') raise ExpiredToken('check token issued time')
except jwt.ExpiredSignatureError: except jwt.ExpiredSignatureError:
print('[auth.jwtcodec] expired signature %r' % r)
raise ExpiredToken('check token lifetime') raise ExpiredToken('check token lifetime')
except jwt.InvalidTokenError: except jwt.InvalidTokenError:
raise InvalidToken('token is not valid') raise InvalidToken('token is not valid')

View File

@ -42,7 +42,7 @@ class TokenStorage:
payload = JWTCodec.decode(token) payload = JWTCodec.decode(token)
except: # noqa except: # noqa
pass pass
finally: else:
await redis.execute("DEL", f"{payload.user_id}-{token}") await redis.execute("DEL", f"{payload.user_id}-{token}")
return True return True

View File

@ -7,7 +7,7 @@ from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage
from services.stat.reacted import ReactedStorage # from services.stat.reacted import ReactedStorage
from services.stat.topicstat import TopicStat from services.stat.topicstat import TopicStat
# from services.stat.viewed import ViewedStorage # from services.stat.viewed import ViewedStorage
@ -18,9 +18,9 @@ async def get_topic_stat(slug):
"authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()), "authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()),
"followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()), "followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()),
# "viewed": await ViewedStorage.get_topic(slug), # "viewed": await ViewedStorage.get_topic(slug),
"reacted": len(await ReactedStorage.get_topic(slug)), # "reacted": len(await ReactedStorage.get_topic(slug)),
"commented": len(await ReactedStorage.get_topic_comments(slug)), # "commented": len(await ReactedStorage.get_topic_comments(slug)),
"rating": await ReactedStorage.get_topic_rating(slug) # "rating": await ReactedStorage.get_topic_rating(slug)
} }

View File

@ -477,9 +477,9 @@ type TopicStat {
followers: Int! followers: Int!
authors: Int! authors: Int!
# viewed: Int # viewed: Int
reacted: Int! # reacted: Int!
commented: Int #commented: Int
rating: Int # rating: Int
} }
type Topic { type Topic {