signIn/getSession optimization (#95)
Co-authored-by: Igor Lobanov <igor.lobanov@onetwotrip.com>
This commit is contained in:
parent
4c6747ab35
commit
da8ee9b9c3
|
@ -18,7 +18,6 @@ from base.exceptions import (BaseHttpException, InvalidPassword, InvalidToken,
|
|||
from base.orm import local_session
|
||||
from base.resolvers import mutation, query
|
||||
from orm import Role, User
|
||||
from resolvers.zine.profile import user_subscriptions
|
||||
from settings import SESSION_TOKEN_HEADER, FRONTEND_URL
|
||||
|
||||
|
||||
|
@ -35,8 +34,7 @@ async def get_current_user(_, info):
|
|||
|
||||
return {
|
||||
"token": token,
|
||||
"user": user,
|
||||
"news": await user_subscriptions(user.id),
|
||||
"user": user
|
||||
}
|
||||
|
||||
|
||||
|
@ -57,8 +55,7 @@ async def confirm_email(_, info, token):
|
|||
session.commit()
|
||||
return {
|
||||
"token": session_token,
|
||||
"user": user,
|
||||
"news": await user_subscriptions(user.id)
|
||||
"user": user
|
||||
}
|
||||
except InvalidToken as e:
|
||||
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")
|
||||
return {
|
||||
"token": session_token,
|
||||
"user": user,
|
||||
"news": await user_subscriptions(user.id),
|
||||
"user": user
|
||||
}
|
||||
except InvalidPassword:
|
||||
print(f"[auth] {email}: invalid password")
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from base.redis import redis
|
||||
import json
|
||||
|
||||
|
||||
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
|
||||
except Exception:
|
||||
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
|
||||
|
|
|
@ -9,11 +9,8 @@ from base.orm import local_session
|
|||
from base.resolvers import mutation, query
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
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 .community import followed_communities
|
||||
from resolvers.inbox.unread import get_total_unread_counter
|
||||
from resolvers.zine.topics import followed_by_user
|
||||
|
||||
|
||||
|
@ -74,34 +71,6 @@ def get_authors_from_query(q):
|
|||
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 (^*^') :
|
||||
@query.field("userFollowedTopics")
|
||||
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)
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -8,19 +8,10 @@ enum MessageStatus {
|
|||
DELETED
|
||||
}
|
||||
|
||||
type UserFollowings {
|
||||
unread: Int
|
||||
topics: [String]
|
||||
authors: [String]
|
||||
reactions: [Int]
|
||||
communities: [String]
|
||||
}
|
||||
|
||||
type AuthResult {
|
||||
error: String
|
||||
token: String
|
||||
user: User
|
||||
news: UserFollowings
|
||||
}
|
||||
|
||||
type ChatMember {
|
||||
|
@ -263,6 +254,11 @@ type NotificationsQueryResult {
|
|||
totalUnreadCount: Int!
|
||||
}
|
||||
|
||||
type MySubscriptionsQueryResult {
|
||||
topics: [Topic]!
|
||||
authors: [Author]!
|
||||
}
|
||||
|
||||
type Query {
|
||||
# inbox
|
||||
loadChats( limit: Int, offset: Int): Result! # your chats
|
||||
|
@ -300,6 +296,8 @@ type Query {
|
|||
topicsByAuthor(author: String!): [Topic]!
|
||||
|
||||
loadNotifications(params: NotificationsQueryParams!): NotificationsQueryResult!
|
||||
|
||||
loadMySubscriptions: MySubscriptionsQueryResult
|
||||
}
|
||||
|
||||
############################################ Entities
|
||||
|
|
Loading…
Reference in New Issue
Block a user