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.jwtcodec import JWTCodec
from auth.tokenstorage import TokenStorage
from base.exceptions import InvalidToken
from base.exceptions import ExpiredToken, InvalidToken
from services.auth.users import UserStorage
from settings import SESSION_TOKEN_HEADER
@ -33,12 +33,12 @@ class SessionToken:
except ExpiredSignatureError:
payload = JWTCodec.decode(token, verify_exp=False)
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:
raise InvalidToken("token format error") from e
else:
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
@classmethod

View File

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

View File

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

View File

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

View File

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