diff --git a/resolvers/auth.py b/resolvers/auth.py index fca657fc..bd4f0b35 100644 --- a/resolvers/auth.py +++ b/resolvers/auth.py @@ -22,7 +22,7 @@ async def confirm(*_, confirm_token): user.save() return { "token": auth_token, "user" : user} else: - return { "error": "Email not confirmed"} + return { "error": "email not confirmed"} @mutation.field("registerUser") @@ -84,8 +84,8 @@ async def login(_, info: GraphQLResolveInfo, email: str, password: str = ""): with local_session() as session: orm_user = session.query(User).filter(User.email == email).first() if orm_user is None: - print(f"signIn {email}: invalid email") - return {"error" : "invalid email"} + print(f"signIn {email}: email not found") + return {"error" : "email not found"} if not password: print(f"signIn {email}: send auth email") diff --git a/resolvers/reactions.py b/resolvers/reactions.py index 5d708685..383f61a7 100644 --- a/resolvers/reactions.py +++ b/resolvers/reactions.py @@ -1,4 +1,4 @@ -from sqlalchemy import and_ +from sqlalchemy import and_, desc, func, select from sqlalchemy.orm import selectinload, joinedload from orm.reaction import Reaction from orm.base import local_session @@ -8,6 +8,7 @@ from resolvers.base import mutation, query from auth.authenticate import login_required from datetime import datetime from storages.reactions import ReactionsStorage +from storages.shoutscache import ShoutsCache from storages.viewed import ViewedStorage from typing import List @@ -120,7 +121,17 @@ def get_shout_reactions(_, info, slug) -> List[Shout]: def get_all_reactions(_, info, page=1, size=10) -> List[Reaction]: reactions = [] with local_session() as session: - q = session.query(Reaction).\ + + # reactions = session.execute(select(Reaction, func.max(Reaction.createdAt).label("reactionCreatedAt")).\ + # join(Shout, Reaction.shout == Shout.slug).\ + # group_by(Reaction.id).\ + # order_by(desc("reactionCreatedAt")).\ + # join( User, Reaction.createdBy == User.slug ).\ + # join( Shout, Reaction.shout == Shout.slug ).\ + # filter( Reaction.deletedAt == None ).\ + # limit(size).offset(page * size).all()) + + reactions = session.query(Reaction).\ options( joinedload(User), joinedload(Shout) diff --git a/storages/reactions.py b/storages/reactions.py index 458500a1..00ec6f36 100644 --- a/storages/reactions.py +++ b/storages/reactions.py @@ -81,7 +81,7 @@ class ReactionsStorage: rating_by_shout[shout] = 0 shout_reactions_by_kinds = session.query(Reaction).\ where(and_(Reaction.deletedAt == None, Reaction.shout == shout)).\ - group_by(Reaction.kind) + group_by(Reaction.kind, Reaction.id).all() for kind, reactions in shout_reactions_by_kinds: rating_by_shout[shout] += len(reactions) * kind_to_rate(kind) async with ReactionsStorage.lock: @@ -144,10 +144,13 @@ class ReactionsStorage: try: with local_session() as session: await ReactionsStorage.prepare_all(session) + print("[storage.reactions] all reactions prepared") await ReactionsStorage.prepare_by_shout(session) + print("[storage.reactions] reactions by shouts prepared") await ReactionsStorage.calc_ratings(session) + print("[storage.reactions] reactions ratings prepared") await ReactionsStorage.prepare_by_topic(session) - print("[storage.reactions] updated") + print("[storage.reactions] reactions topics prepared") except Exception as err: print("[storage.reactions] errror: %s" % (err)) await asyncio.sleep(ReactionsStorage.period) diff --git a/storages/shoutscache.py b/storages/shoutscache.py index 45ec0fc4..d034d2e9 100644 --- a/storages/shoutscache.py +++ b/storages/shoutscache.py @@ -52,8 +52,11 @@ class ShoutsCache: async def prepare_recent_reacted(): with local_session() as session: stmt = select(Shout, func.max(Reaction.createdAt).label("reactionCreatedAt")).\ - options(selectinload(Shout.authors), selectinload(Shout.topics)).\ - join(Reaction).\ + options( + selectinload(Shout.authors), + selectinload(Shout.topics), + ).\ + join(Reaction, Reaction.shout == Shout.slug).\ where(and_(Shout.publishedAt != None, Reaction.deletedAt == None)).\ group_by(Shout.slug).\ order_by(desc("reactionCreatedAt")).\