authfixes

This commit is contained in:
tonyrewin 2022-08-12 11:13:18 +03:00
parent f13f40c89a
commit 0873304068
2 changed files with 102 additions and 100 deletions

View File

@ -1,3 +1,4 @@
from datetime import datetime
from orm.user import User, UserRole, Role, UserRating, AuthorFollower from orm.user import User, UserRole, Role, UserRating, AuthorFollower
from services.auth.users import UserStorage from services.auth.users import UserStorage
from orm.shout import Shout from orm.shout import Shout
@ -15,144 +16,148 @@ from typing import List
@query.field("userReactedShouts") @query.field("userReactedShouts")
async def get_user_reacted_shouts(_, info, slug, page, size) -> List[Shout]: async def get_user_reacted_shouts(_, info, slug, page, size) -> List[Shout]:
user = await UserStorage.get_user_by_slug(slug) user = await UserStorage.get_user_by_slug(slug)
if not user: return {} if not user: return {}
with local_session() as session: with local_session() as session:
shouts = session.query(Shout).\ shouts = session.query(Shout).\
join(Reaction).\ join(Reaction).\
where(Reaction.createdBy == user.slug).\ where(Reaction.createdBy == user.slug).\
order_by(desc(Reaction.createdAt)).\ order_by(desc(Reaction.createdAt)).\
limit(size).\ limit(size).\
offset(page * size).all() offset(page * size).all()
return shouts return shouts
@query.field("userFollowedTopics") @query.field("userFollowedTopics")
@login_required @login_required
def get_followed_topics(_, slug) -> List[Topic]: def get_followed_topics(_, slug) -> List[Topic]:
rows = [] rows = []
with local_session() as session: with local_session() as session:
rows = session.query(Topic).\ rows = session.query(Topic).\
join(TopicFollower).\ join(TopicFollower).\
where(TopicFollower.follower == slug).\ where(TopicFollower.follower == slug).\
all() all()
return rows return rows
@query.field("userFollowedAuthors") @query.field("userFollowedAuthors")
def get_followed_authors(_, slug) -> List[User]: def get_followed_authors(_, slug) -> List[User]:
authors = [] authors = []
with local_session() as session: with local_session() as session:
authors = session.query(User).\ authors = session.query(User).\
join(AuthorFollower, User.slug == AuthorFollower.author).\ join(AuthorFollower, User.slug == AuthorFollower.author).\
where(AuthorFollower.follower == slug).\ where(AuthorFollower.follower == slug).\
all() all()
return authors return authors
@query.field("userFollowers") @query.field("userFollowers")
async def user_followers(_, slug) -> List[User]: async def user_followers(_, slug) -> List[User]:
with local_session() as session: with local_session() as session:
users = session.query(User).\ users = session.query(User).\
join(AuthorFollower, User.slug == AuthorFollower.follower).\ join(AuthorFollower, User.slug == AuthorFollower.follower).\
where(AuthorFollower.author == slug).\ where(AuthorFollower.author == slug).\
all() all()
return users return users
# for query.field("getCurrentUser") # for query.field("refreshSession")
async def get_user_info(slug): async def get_user_info(slug):
return { return {
"inbox": await get_inbox_counter(slug), "inbox": await get_inbox_counter(slug),
"topics": [t.slug for t in get_followed_topics(0, slug)], "topics": [t.slug for t in get_followed_topics(0, slug)],
"authors": [a.slug for a in get_followed_authors(0, slug)], "authors": [a.slug for a in get_followed_authors(0, slug)],
"reactions": [r.shout for r in get_shout_reactions(0, slug)], "reactions": [r.shout for r in get_shout_reactions(0, slug)],
"communities": [c.slug for c in get_followed_communities(0, slug)] "communities": [c.slug for c in get_followed_communities(0, slug)]
} }
@query.field("getCurrentUser") @query.field("refreshSession")
@login_required @login_required
async def get_current_user(_, info): async def get_current_user(_, info):
user = info.context["request"].user user = info.context["request"].user
return { with local_session() as session:
"user": user, user.lastSeen = datetime.now()
"info": await get_user_info(user.slug) user.save()
} session.commit()
return {
"user": user,
"info": await get_user_info(user.slug)
}
@query.field("getUsersBySlugs") @query.field("getUsersBySlugs")
async def get_users_by_slugs(_, info, slugs): async def get_users_by_slugs(_, info, slugs):
with local_session() as session: with local_session() as session:
users = session.query(User).\ users = session.query(User).\
options(selectinload(User.ratings)).\ options(selectinload(User.ratings)).\
filter(User.slug.in_(slugs)).all() filter(User.slug.in_(slugs)).all()
return users return users
@query.field("getUserRoles") @query.field("getUserRoles")
async def get_user_roles(_, info, slug): async def get_user_roles(_, info, slug):
with local_session() as session: with local_session() as session:
user = session.query(User).where(User.slug == slug).first() user = session.query(User).where(User.slug == slug).first()
roles = session.query(Role).\ roles = session.query(Role).\
options(selectinload(Role.permissions)).\ options(selectinload(Role.permissions)).\
join(UserRole).\ join(UserRole).\
where(UserRole.user_id == user.id).all() where(UserRole.user_id == user.id).all()
return roles return roles
@mutation.field("updateProfile") @mutation.field("updateProfile")
@login_required @login_required
async def update_profile(_, info, profile): async def update_profile(_, info, profile):
auth = info.context["request"].auth auth = info.context["request"].auth
user_id = auth.user_id user_id = auth.user_id
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.id == user_id).first() user = session.query(User).filter(User.id == user_id).first()
user.update(profile) user.update(profile)
session.commit() session.commit()
return {} return {}
@mutation.field("rateUser") @mutation.field("rateUser")
@login_required @login_required
async def rate_user(_, info, slug, value): async def rate_user(_, info, slug, value):
user = info.context["request"].user user = info.context["request"].user
with local_session() as session: with local_session() as session:
rating = session.query(UserRating).\ rating = session.query(UserRating).\
filter(and_(UserRating.rater == user.slug, UserRating.user == slug)).\ filter(and_(UserRating.rater == user.slug, UserRating.user == slug)).\
first() first()
if rating: if rating:
rating.value = value rating.value = value
session.commit() session.commit()
return {} return {}
try: try:
UserRating.create( UserRating.create(
rater=user.slug, rater=user.slug,
user=slug, user=slug,
value=value value=value
) )
except Exception as err: except Exception as err:
return {"error": err} return {"error": err}
return {} return {}
# for mutation.field("follow") # for mutation.field("follow")
def author_follow(user, slug): def author_follow(user, slug):
AuthorFollower.create( AuthorFollower.create(
follower=user.slug, follower=user.slug,
author=slug author=slug
) )
# for mutation.field("unfollow") # for mutation.field("unfollow")
def author_unfollow(user, slug): def author_unfollow(user, slug):
with local_session() as session: with local_session() as session:
flw = session.query(AuthorFollower).\ flw = session.query(AuthorFollower).\
filter(and_(AuthorFollower.follower == user.slug, AuthorFollower.author == slug)).\ filter(and_(AuthorFollower.follower == user.slug, AuthorFollower.author == slug)).\
first() first()
if not flw: if not flw:
raise Exception("[resolvers.profile] follower not exist, cant unfollow") raise Exception("[resolvers.profile] follower not exist, cant unfollow")
else: else:
session.delete(flw) session.delete(flw)
session.commit() session.commit()
@query.field("authorsAll") @query.field("authorsAll")
def get_authors_all(_, info): def get_authors_all(_, info):
return UserStorage.get_all_users() return UserStorage.get_all_users()

View File

@ -140,10 +140,10 @@ type Mutation {
# auth # auth
confirmEmail(token: String!): AuthResult! confirmEmail(token: String!): AuthResult!
refreshSession: AuthResult!
registerUser(email: String!, password: String): AuthResult! registerUser(email: String!, password: String): AuthResult!
requestPasswordUpdate(email: String!): Result! requestPasswordUpdate(email: String!): Result!
updatePassword(password: String!, token: String!): Result! updatePassword(password: String!, token: String!): Result!
# requestEmailConfirmation: User!
# shout # shout
createShout(input: ShoutInput!): Result! createShout(input: ShoutInput!): Result!
@ -203,10 +203,6 @@ type Query {
isEmailUsed(email: String!): Boolean! isEmailUsed(email: String!): Boolean!
signIn(email: String!, password: String): AuthResult! signIn(email: String!, password: String): AuthResult!
signOut: AuthResult! signOut: AuthResult!
forget(email: String!): AuthResult!
requestPasswordReset(email: String!): AuthResult!
updatePassword(password: String!, token: String!): AuthResult!
getCurrentUser: AuthResult!
# profile # profile
getUsersBySlugs(slugs: [String]!): [User]! getUsersBySlugs(slugs: [String]!): [User]!
@ -312,6 +308,7 @@ type User {
id: Int! id: Int!
username: String! # to login, ex. email username: String! # to login, ex. email
createdAt: DateTime! createdAt: DateTime!
lastSeen: DataTime
slug: String! slug: String!
name: String # to display name: String # to display
email: String email: String