schema-fixes

This commit is contained in:
tonyrewin 2022-10-04 15:07:41 +03:00
parent fd18a1221d
commit 4bc5d90168
4 changed files with 27 additions and 24 deletions

View File

@ -18,7 +18,7 @@ from base.exceptions import (
from base.orm import local_session from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
from orm import User, Role 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 from settings import SESSION_TOKEN_HEADER, CONFIRM_CALLBACK_URL
@ -34,12 +34,12 @@ async def get_current_user(_, info):
return { return {
"token": token, "token": token,
"user": user, "user": user,
"info": await get_user_info(user.slug), "news": await get_user_subscriptions(user.slug),
} }
@mutation.field("confirmEmail") @mutation.field("confirmEmail")
async def confirm_email(_, confirm_token): async def confirm_email(_, _info, confirm_token):
"""confirm owning email address""" """confirm owning email address"""
try: try:
user_id = await TokenStorage.get(confirm_token) user_id = await TokenStorage.get(confirm_token)
@ -91,7 +91,7 @@ def generate_unique_slug(username):
@mutation.field("registerUser") @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""" """creates new user account"""
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.email == email).first() 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) user = create_user(user_dict)
if not password: if not password:
user = await auth_send_link(_, email) user = await auth_send_link(_, _info, email)
return user return user
@mutation.field("sendLink") @mutation.field("sendLink")
async def auth_send_link(_, email): async def auth_send_link(_, _info, email):
"""send link with confirm code to email""" """send link with confirm code to email"""
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.email == email).first() user = session.query(User).filter(User.email == email).first()
@ -129,7 +129,7 @@ async def auth_send_link(_, email):
@query.field("signIn") @query.field("signIn")
async def login(_, email: str, password: str = ""): async def login(_, _info, email: str, password: str = ""):
with local_session() as session: with local_session() as session:
orm_user = session.query(User).filter(User.email == email).first() orm_user = session.query(User).filter(User.email == email).first()
@ -158,7 +158,7 @@ async def login(_, email: str, password: str = ""):
return { return {
"token": session_token, "token": session_token,
"user": user, "user": user,
"info": await get_user_info(user.slug), "news": await get_user_subscriptions(user.slug),
} }
except InvalidPassword: except InvalidPassword:
print(f"[auth] {email}: invalid password") print(f"[auth] {email}: invalid password")
@ -175,7 +175,7 @@ async def sign_out(_, info: GraphQLResolveInfo):
@query.field("isEmailUsed") @query.field("isEmailUsed")
async def is_email_used(_, email): async def is_email_used(_, _info, email):
with local_session() as session: with local_session() as session:
user = session.query(User).filter(User.email == email).first() user = session.query(User).filter(User.email == email).first()
return user is not None return user is not None

View File

@ -1,6 +1,6 @@
from typing import List from typing import List
from sqlalchemy import and_, desc from sqlalchemy import and_, desc, func
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from auth.authenticate import login_required from auth.authenticate import login_required
@ -17,7 +17,7 @@ from .topics import get_topic_stat
from services.auth.users import UserStorage from services.auth.users import UserStorage
async def get_user_info(slug): async def get_user_subscriptions(slug):
return { return {
"unread": await get_unread_counter(slug), # unread inbox messages counter "unread": await get_unread_counter(slug), # unread inbox messages counter
"topics": [t.slug for t in get_followed_topics(0, slug)], # followed topics slugs "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): async def get_author_stat(slug):
# TODO: implement author stat # TODO: implement author stat
with local_session() as session:
return { 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") @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) user = await UserStorage.get_user_by_slug(slug)
if not user: if not user:
return [] return []
@ -54,7 +57,7 @@ async def get_user_reacted_shouts(_, _info, slug, offset, limit) -> List[Shout]:
@query.field("userFollowedTopics") @query.field("userFollowedTopics")
@login_required @login_required
async def get_followed_topics(_, slug) -> List[Topic]: async def get_followed_topics(_, info, slug) -> List[Topic]:
topics = [] topics = []
with local_session() as session: with local_session() as session:
topics = ( topics = (
@ -69,7 +72,7 @@ async def get_followed_topics(_, slug) -> List[Topic]:
@query.field("userFollowedAuthors") @query.field("userFollowedAuthors")
async def get_followed_authors(_, slug) -> List[User]: async def get_followed_authors(_, _info, slug) -> List[User]:
authors = [] authors = []
with local_session() as session: with local_session() as session:
authors = ( authors = (
@ -84,7 +87,7 @@ async def get_followed_authors(_, slug) -> List[User]:
@query.field("userFollowers") @query.field("userFollowers")
async def user_followers(_, slug) -> List[User]: async def user_followers(_, _info, slug) -> List[User]:
with local_session() as session: with local_session() as session:
users = ( users = (
session.query(User) session.query(User)
@ -137,12 +140,12 @@ async def update_profile(_, info, profile):
@mutation.field("rateUser") @mutation.field("rateUser")
@login_required @login_required
async def rate_user(_, info, slug, value): async def rate_user(_, info, rated_userslug, value):
user = info.context["request"].user user = info.context["request"].user
with local_session() as session: with local_session() as session:
rating = ( rating = (
session.query(UserRating) session.query(UserRating)
.filter(and_(UserRating.rater == user.slug, UserRating.user == slug)) .filter(and_(UserRating.rater == user.slug, UserRating.user == rated_userslug))
.first() .first()
) )
if rating: if rating:
@ -150,7 +153,7 @@ async def rate_user(_, info, slug, value):
session.commit() session.commit()
return {} return {}
try: try:
UserRating.create(rater=user.slug, user=slug, value=value) UserRating.create(rater=user.slug, user=rated_userslug, value=value)
except Exception as err: except Exception as err:
return {"error": err} return {"error": err}
return {} return {}

View File

@ -39,7 +39,7 @@ type ChatResult {
title: String title: String
} }
type SessionInfo { type UserFollowings {
unread: Int unread: Int
topics: [String] topics: [String]
authors: [String] authors: [String]
@ -51,7 +51,7 @@ type AuthResult {
error: String error: String
token: String token: String
user: User user: User
info: SessionInfo news: UserFollowings
} }
type Result { type Result {

View File

@ -15,5 +15,5 @@ async def storages_init():
RoleStorage.init(session) RoleStorage.init(session)
UserStorage.init(session) UserStorage.init(session)
TopicStorage.init(session) TopicStorage.init(session)
SearchService.init(session) await SearchService.init(session)
session.commit() session.commit()