From 0873304068389cc5a3b468ef4c9373a87f94ac55 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Fri, 12 Aug 2022 11:13:18 +0300 Subject: [PATCH] authfixes --- resolvers/profile.py | 195 ++++++++++++++++++++++--------------------- schema.graphql | 7 +- 2 files changed, 102 insertions(+), 100 deletions(-) diff --git a/resolvers/profile.py b/resolvers/profile.py index 7f992258..3cb3943c 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -1,3 +1,4 @@ +from datetime import datetime from orm.user import User, UserRole, Role, UserRating, AuthorFollower from services.auth.users import UserStorage from orm.shout import Shout @@ -15,144 +16,148 @@ from typing import List @query.field("userReactedShouts") async def get_user_reacted_shouts(_, info, slug, page, size) -> List[Shout]: - user = await UserStorage.get_user_by_slug(slug) - if not user: return {} - with local_session() as session: - shouts = session.query(Shout).\ - join(Reaction).\ - where(Reaction.createdBy == user.slug).\ - order_by(desc(Reaction.createdAt)).\ - limit(size).\ - offset(page * size).all() - return shouts + user = await UserStorage.get_user_by_slug(slug) + if not user: return {} + with local_session() as session: + shouts = session.query(Shout).\ + join(Reaction).\ + where(Reaction.createdBy == user.slug).\ + order_by(desc(Reaction.createdAt)).\ + limit(size).\ + offset(page * size).all() + return shouts @query.field("userFollowedTopics") @login_required def get_followed_topics(_, slug) -> List[Topic]: - rows = [] - with local_session() as session: - rows = session.query(Topic).\ - join(TopicFollower).\ - where(TopicFollower.follower == slug).\ - all() - return rows + rows = [] + with local_session() as session: + rows = session.query(Topic).\ + join(TopicFollower).\ + where(TopicFollower.follower == slug).\ + all() + return rows @query.field("userFollowedAuthors") def get_followed_authors(_, slug) -> List[User]: - authors = [] - with local_session() as session: - authors = session.query(User).\ - join(AuthorFollower, User.slug == AuthorFollower.author).\ - where(AuthorFollower.follower == slug).\ - all() - return authors + authors = [] + with local_session() as session: + authors = session.query(User).\ + join(AuthorFollower, User.slug == AuthorFollower.author).\ + where(AuthorFollower.follower == slug).\ + all() + return authors @query.field("userFollowers") async def user_followers(_, slug) -> List[User]: - with local_session() as session: - users = session.query(User).\ - join(AuthorFollower, User.slug == AuthorFollower.follower).\ - where(AuthorFollower.author == slug).\ - all() - return users + with local_session() as session: + users = session.query(User).\ + join(AuthorFollower, User.slug == AuthorFollower.follower).\ + where(AuthorFollower.author == slug).\ + all() + return users -# for query.field("getCurrentUser") +# for query.field("refreshSession") async def get_user_info(slug): - return { - "inbox": await get_inbox_counter(slug), - "topics": [t.slug for t in get_followed_topics(0, slug)], - "authors": [a.slug for a in get_followed_authors(0, slug)], - "reactions": [r.shout for r in get_shout_reactions(0, slug)], - "communities": [c.slug for c in get_followed_communities(0, slug)] - } + return { + "inbox": await get_inbox_counter(slug), + "topics": [t.slug for t in get_followed_topics(0, slug)], + "authors": [a.slug for a in get_followed_authors(0, slug)], + "reactions": [r.shout for r in get_shout_reactions(0, slug)], + "communities": [c.slug for c in get_followed_communities(0, slug)] + } -@query.field("getCurrentUser") +@query.field("refreshSession") @login_required async def get_current_user(_, info): - user = info.context["request"].user - return { - "user": user, - "info": await get_user_info(user.slug) - } + user = info.context["request"].user + with local_session() as session: + user.lastSeen = datetime.now() + user.save() + session.commit() + return { + "user": user, + "info": await get_user_info(user.slug) + } @query.field("getUsersBySlugs") async def get_users_by_slugs(_, info, slugs): - with local_session() as session: - users = session.query(User).\ - options(selectinload(User.ratings)).\ - filter(User.slug.in_(slugs)).all() - return users + with local_session() as session: + users = session.query(User).\ + options(selectinload(User.ratings)).\ + filter(User.slug.in_(slugs)).all() + return users @query.field("getUserRoles") async def get_user_roles(_, info, slug): - with local_session() as session: - user = session.query(User).where(User.slug == slug).first() - roles = session.query(Role).\ - options(selectinload(Role.permissions)).\ - join(UserRole).\ - where(UserRole.user_id == user.id).all() - return roles + with local_session() as session: + user = session.query(User).where(User.slug == slug).first() + roles = session.query(Role).\ + options(selectinload(Role.permissions)).\ + join(UserRole).\ + where(UserRole.user_id == user.id).all() + return roles @mutation.field("updateProfile") @login_required async def update_profile(_, info, profile): - auth = info.context["request"].auth - user_id = auth.user_id - with local_session() as session: - user = session.query(User).filter(User.id == user_id).first() - user.update(profile) - session.commit() - return {} + auth = info.context["request"].auth + user_id = auth.user_id + with local_session() as session: + user = session.query(User).filter(User.id == user_id).first() + user.update(profile) + session.commit() + return {} @mutation.field("rateUser") @login_required async def rate_user(_, info, slug, value): - user = info.context["request"].user - with local_session() as session: - rating = session.query(UserRating).\ - filter(and_(UserRating.rater == user.slug, UserRating.user == slug)).\ - first() - if rating: - rating.value = value - session.commit() - return {} - try: - UserRating.create( - rater=user.slug, - user=slug, - value=value - ) - except Exception as err: - return {"error": err} - return {} + user = info.context["request"].user + with local_session() as session: + rating = session.query(UserRating).\ + filter(and_(UserRating.rater == user.slug, UserRating.user == slug)).\ + first() + if rating: + rating.value = value + session.commit() + return {} + try: + UserRating.create( + rater=user.slug, + user=slug, + value=value + ) + except Exception as err: + return {"error": err} + return {} # for mutation.field("follow") def author_follow(user, slug): - AuthorFollower.create( - follower=user.slug, - author=slug - ) + AuthorFollower.create( + follower=user.slug, + author=slug + ) # for mutation.field("unfollow") def author_unfollow(user, slug): - with local_session() as session: - flw = session.query(AuthorFollower).\ - filter(and_(AuthorFollower.follower == user.slug, AuthorFollower.author == slug)).\ - first() - if not flw: - raise Exception("[resolvers.profile] follower not exist, cant unfollow") - else: - session.delete(flw) - session.commit() + with local_session() as session: + flw = session.query(AuthorFollower).\ + filter(and_(AuthorFollower.follower == user.slug, AuthorFollower.author == slug)).\ + first() + if not flw: + raise Exception("[resolvers.profile] follower not exist, cant unfollow") + else: + session.delete(flw) + session.commit() @query.field("authorsAll") def get_authors_all(_, info): - return UserStorage.get_all_users() + return UserStorage.get_all_users() diff --git a/schema.graphql b/schema.graphql index a4128701..30d657da 100644 --- a/schema.graphql +++ b/schema.graphql @@ -140,10 +140,10 @@ type Mutation { # auth confirmEmail(token: String!): AuthResult! + refreshSession: AuthResult! registerUser(email: String!, password: String): AuthResult! requestPasswordUpdate(email: String!): Result! updatePassword(password: String!, token: String!): Result! - # requestEmailConfirmation: User! # shout createShout(input: ShoutInput!): Result! @@ -203,10 +203,6 @@ type Query { isEmailUsed(email: String!): Boolean! signIn(email: String!, password: String): AuthResult! signOut: AuthResult! - forget(email: String!): AuthResult! - requestPasswordReset(email: String!): AuthResult! - updatePassword(password: String!, token: String!): AuthResult! - getCurrentUser: AuthResult! # profile getUsersBySlugs(slugs: [String]!): [User]! @@ -312,6 +308,7 @@ type User { id: Int! username: String! # to login, ex. email createdAt: DateTime! + lastSeen: DataTime slug: String! name: String # to display email: String