signIn/getSession optimization (#95)

Co-authored-by: Igor Lobanov <igor.lobanov@onetwotrip.com>
This commit is contained in:
Ilya Y 2023-10-19 17:54:38 +03:00 committed by GitHub
parent 4c6747ab35
commit da8ee9b9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 60 deletions

View File

@ -18,7 +18,6 @@ from base.exceptions import (BaseHttpException, InvalidPassword, InvalidToken,
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 Role, User from orm import Role, User
from resolvers.zine.profile import user_subscriptions
from settings import SESSION_TOKEN_HEADER, FRONTEND_URL from settings import SESSION_TOKEN_HEADER, FRONTEND_URL
@ -35,8 +34,7 @@ async def get_current_user(_, info):
return { return {
"token": token, "token": token,
"user": user, "user": user
"news": await user_subscriptions(user.id),
} }
@ -57,8 +55,7 @@ async def confirm_email(_, info, token):
session.commit() session.commit()
return { return {
"token": session_token, "token": session_token,
"user": user, "user": user
"news": await user_subscriptions(user.id)
} }
except InvalidToken as e: except InvalidToken as e:
raise InvalidToken(e.message) raise InvalidToken(e.message)
@ -177,8 +174,7 @@ async def login(_, info, email: str, password: str = "", lang: str = "ru"):
print(f"[auth] user {email} authorized") print(f"[auth] user {email} authorized")
return { return {
"token": session_token, "token": session_token,
"user": user, "user": user
"news": await user_subscriptions(user.id),
} }
except InvalidPassword: except InvalidPassword:
print(f"[auth] {email}: invalid password") print(f"[auth] {email}: invalid password")

View File

@ -1,5 +1,4 @@
from base.redis import redis from base.redis import redis
import json
async def get_unread_counter(chat_id: str, user_id: int): async def get_unread_counter(chat_id: str, user_id: int):
@ -9,14 +8,3 @@ async def get_unread_counter(chat_id: str, user_id: int):
return unread return unread
except Exception: except Exception:
return 0 return 0
async def get_total_unread_counter(user_id: int):
chats = await redis.execute("GET", f"chats_by_user/{str(user_id)}")
unread = 0
if chats:
chats = json.loads(chats)
for chat_id in chats:
n = await get_unread_counter(chat_id.decode('utf-8'), user_id)
unread += n
return unread

View File

@ -9,11 +9,8 @@ from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
from orm.reaction import Reaction, ReactionKind from orm.reaction import Reaction, ReactionKind
from orm.shout import ShoutAuthor, ShoutTopic from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic from orm.topic import Topic, TopicFollower
from orm.user import AuthorFollower, Role, User, UserRating, UserRole from orm.user import AuthorFollower, Role, User, UserRating, UserRole
# from .community import followed_communities
from resolvers.inbox.unread import get_total_unread_counter
from resolvers.zine.topics import followed_by_user from resolvers.zine.topics import followed_by_user
@ -74,34 +71,6 @@ def get_authors_from_query(q):
return authors return authors
async def user_subscriptions(user_id: int):
return {
"unread": await get_total_unread_counter(user_id), # unread inbox messages counter
"topics": [t.slug for t in await followed_topics(user_id)], # followed topics slugs
"authors": [a.slug for a in await followed_authors(user_id)], # followed authors slugs
"reactions": await followed_reactions(user_id)
# "communities": [c.slug for c in followed_communities(slug)], # communities
}
# @query.field("userFollowedDiscussions")
# @login_required
async def followed_discussions(_, info, user_id) -> List[Topic]:
return await followed_reactions(user_id)
async def followed_reactions(user_id):
with local_session() as session:
user = session.query(User).where(User.id == user_id).first()
return session.query(
Reaction.shout
).where(
Reaction.createdBy == user.id
).filter(
Reaction.createdAt > user.lastSeen
).all()
# dufok mod (^*^') : # dufok mod (^*^') :
@query.field("userFollowedTopics") @query.field("userFollowedTopics")
async def get_followed_topics(_, info, slug) -> List[Topic]: async def get_followed_topics(_, info, slug) -> List[Topic]:
@ -296,3 +265,33 @@ async def load_authors_by(_, info, by, limit, offset):
).limit(limit).offset(offset) ).limit(limit).offset(offset)
return get_authors_from_query(q) return get_authors_from_query(q)
@query.field("loadMySubscriptions")
@login_required
async def load_my_subscriptions(_, info):
auth = info.context["request"].auth
user_id = auth.user_id
authors_query = select(User).join(AuthorFollower, AuthorFollower.author == User.id).where(
AuthorFollower.follower == user_id
)
topics_query = select(Topic).join(TopicFollower).where(
TopicFollower.follower == user_id
)
topics = []
authors = []
with local_session() as session:
for [author] in session.execute(authors_query):
authors.append(author)
for [topic] in session.execute(topics_query):
topics.append(topic)
return {
"topics": topics,
"authors": authors
}

View File

@ -8,19 +8,10 @@ enum MessageStatus {
DELETED DELETED
} }
type UserFollowings {
unread: Int
topics: [String]
authors: [String]
reactions: [Int]
communities: [String]
}
type AuthResult { type AuthResult {
error: String error: String
token: String token: String
user: User user: User
news: UserFollowings
} }
type ChatMember { type ChatMember {
@ -263,6 +254,11 @@ type NotificationsQueryResult {
totalUnreadCount: Int! totalUnreadCount: Int!
} }
type MySubscriptionsQueryResult {
topics: [Topic]!
authors: [Author]!
}
type Query { type Query {
# inbox # inbox
loadChats( limit: Int, offset: Int): Result! # your chats loadChats( limit: Int, offset: Int): Result! # your chats
@ -300,6 +296,8 @@ type Query {
topicsByAuthor(author: String!): [Topic]! topicsByAuthor(author: String!): [Topic]!
loadNotifications(params: NotificationsQueryParams!): NotificationsQueryResult! loadNotifications(params: NotificationsQueryParams!): NotificationsQueryResult!
loadMySubscriptions: MySubscriptionsQueryResult
} }
############################################ Entities ############################################ Entities