From 4bc5d90168389cd4223782d87813357072d7044f Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 4 Oct 2022 15:07:41 +0300 Subject: [PATCH] schema-fixes --- resolvers/auth.py | 18 +++++++++--------- resolvers/profile.py | 27 +++++++++++++++------------ schema.graphql | 4 ++-- services/main.py | 2 +- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/resolvers/auth.py b/resolvers/auth.py index 00dd91a3..73ad4f14 100644 --- a/resolvers/auth.py +++ b/resolvers/auth.py @@ -18,7 +18,7 @@ from base.exceptions import ( from base.orm import local_session from base.resolvers import mutation, query from orm import User, Role -from resolvers.profile import get_user_info +from resolvers.profile import get_user_subscriptions from settings import SESSION_TOKEN_HEADER, CONFIRM_CALLBACK_URL @@ -34,12 +34,12 @@ async def get_current_user(_, info): return { "token": token, "user": user, - "info": await get_user_info(user.slug), + "news": await get_user_subscriptions(user.slug), } @mutation.field("confirmEmail") -async def confirm_email(_, confirm_token): +async def confirm_email(_, _info, confirm_token): """confirm owning email address""" try: user_id = await TokenStorage.get(confirm_token) @@ -91,7 +91,7 @@ def generate_unique_slug(username): @mutation.field("registerUser") -async def register(_, email: str, password: str = "", username: str = ""): +async def register(_, _info, email: str, password: str = "", username: str = ""): """creates new user account""" with local_session() as session: user = session.query(User).filter(User.email == email).first() @@ -110,13 +110,13 @@ async def register(_, email: str, password: str = "", username: str = ""): user = create_user(user_dict) if not password: - user = await auth_send_link(_, email) + user = await auth_send_link(_, _info, email) return user @mutation.field("sendLink") -async def auth_send_link(_, email): +async def auth_send_link(_, _info, email): """send link with confirm code to email""" with local_session() as session: user = session.query(User).filter(User.email == email).first() @@ -129,7 +129,7 @@ async def auth_send_link(_, email): @query.field("signIn") -async def login(_, email: str, password: str = ""): +async def login(_, _info, email: str, password: str = ""): with local_session() as session: orm_user = session.query(User).filter(User.email == email).first() @@ -158,7 +158,7 @@ async def login(_, email: str, password: str = ""): return { "token": session_token, "user": user, - "info": await get_user_info(user.slug), + "news": await get_user_subscriptions(user.slug), } except InvalidPassword: print(f"[auth] {email}: invalid password") @@ -175,7 +175,7 @@ async def sign_out(_, info: GraphQLResolveInfo): @query.field("isEmailUsed") -async def is_email_used(_, email): +async def is_email_used(_, _info, email): with local_session() as session: user = session.query(User).filter(User.email == email).first() return user is not None diff --git a/resolvers/profile.py b/resolvers/profile.py index 46fab04d..57f622e9 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -1,6 +1,6 @@ from typing import List -from sqlalchemy import and_, desc +from sqlalchemy import and_, desc, func from sqlalchemy.orm import selectinload from auth.authenticate import login_required @@ -17,7 +17,7 @@ from .topics import get_topic_stat from services.auth.users import UserStorage -async def get_user_info(slug): +async def get_user_subscriptions(slug): return { "unread": await get_unread_counter(slug), # unread inbox messages counter "topics": [t.slug for t in get_followed_topics(0, slug)], # followed topics slugs @@ -29,13 +29,16 @@ async def get_user_info(slug): async def get_author_stat(slug): # TODO: implement author stat - return { - - } + with local_session() as session: + return { + "followers": session.query(AuthorFollower).where(AuthorFollower.author == slug).count(), + "rating": session.query(func.sum(UserRating.value)).where(UserRating.user == slug).first() + # TODO: debug + } @query.field("userReactedShouts") -async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]: +async def get_user_reacted_shouts(_, slug, offset, limit) -> List[Shout]: user = await UserStorage.get_user_by_slug(slug) if not user: return [] @@ -54,7 +57,7 @@ async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]: @query.field("userFollowedTopics") @login_required -async def get_followed_topics(_, slug) -> List[Topic]: +async def get_followed_topics(_, info, slug) -> List[Topic]: topics = [] with local_session() as session: topics = ( @@ -69,7 +72,7 @@ async def get_followed_topics(_, slug) -> List[Topic]: @query.field("userFollowedAuthors") -async def get_followed_authors(_, slug) -> List[User]: +async def get_followed_authors(_, _info, slug) -> List[User]: authors = [] with local_session() as session: authors = ( @@ -84,7 +87,7 @@ async def get_followed_authors(_, slug) -> List[User]: @query.field("userFollowers") -async def user_followers(_, slug) -> List[User]: +async def user_followers(_, _info, slug) -> List[User]: with local_session() as session: users = ( session.query(User) @@ -137,12 +140,12 @@ async def update_profile(_, info, profile): @mutation.field("rateUser") @login_required -async def rate_user(_, info, slug, value): +async def rate_user(_, info, rated_userslug, value): user = info.context["request"].user with local_session() as session: rating = ( session.query(UserRating) - .filter(and_(UserRating.rater == user.slug, UserRating.user == slug)) + .filter(and_(UserRating.rater == user.slug, UserRating.user == rated_userslug)) .first() ) if rating: @@ -150,7 +153,7 @@ async def rate_user(_, info, slug, value): session.commit() return {} try: - UserRating.create(rater=user.slug, user=slug, value=value) + UserRating.create(rater=user.slug, user=rated_userslug, value=value) except Exception as err: return {"error": err} return {} diff --git a/schema.graphql b/schema.graphql index baf15c84..d882b7c2 100644 --- a/schema.graphql +++ b/schema.graphql @@ -39,7 +39,7 @@ type ChatResult { title: String } -type SessionInfo { +type UserFollowings { unread: Int topics: [String] authors: [String] @@ -51,7 +51,7 @@ type AuthResult { error: String token: String user: User - info: SessionInfo + news: UserFollowings } type Result { diff --git a/services/main.py b/services/main.py index b6a2bf37..3783b55f 100644 --- a/services/main.py +++ b/services/main.py @@ -15,5 +15,5 @@ async def storages_init(): RoleStorage.init(session) UserStorage.init(session) TopicStorage.init(session) - SearchService.init(session) + await SearchService.init(session) session.commit()