From 9c60318919a4ef053b2b548ec6b3c07ab385ec8e Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Fri, 12 Aug 2022 15:50:59 +0300 Subject: [PATCH] session-info --- resolvers/inbox.py | 9 ++------- resolvers/profile.py | 9 +++++---- resolvers/topics.py | 12 ++++-------- schema.graphql | 11 ++++++++++- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/resolvers/inbox.py b/resolvers/inbox.py index 664b3ad7..87c1bdac 100644 --- a/resolvers/inbox.py +++ b/resolvers/inbox.py @@ -36,7 +36,7 @@ class MessageResult: self.status = status self.message = message -async def get_inbox_counter(user_slug): +async def get_unread_counter(user_slug): chats = await redis.execute("GET", f"chats_by_user/{user_slug}") if not chats: return 0 @@ -250,13 +250,8 @@ async def mark_as_read(_, info, chatId, ids): return {} @subscription.source("chatUpdated") +@login_required async def message_generator(obj, info, chatId): - - #TODO: send AUTH header - #auth = info.context["request"].auth - #if not auth.logged_in: - # yield {"error" : auth.error_message or "Please login"} - try: following_chat = ChatFollowing(chatId) await MessagesStorage.register_chat(following_chat) diff --git a/resolvers/profile.py b/resolvers/profile.py index 3cb3943c..3f5e09b7 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -9,7 +9,7 @@ from base.resolvers import mutation, query from resolvers.community import get_followed_communities from resolvers.reactions import get_shout_reactions from auth.authenticate import login_required -from resolvers.inbox import get_inbox_counter +from resolvers.inbox import get_unread_counter from sqlalchemy import and_, desc from sqlalchemy.orm import selectinload from typing import List @@ -60,10 +60,10 @@ async def user_followers(_, slug) -> List[User]: all() return users -# for query.field("refreshSession") +# for mutation.field("refreshSession") async def get_user_info(slug): return { - "inbox": await get_inbox_counter(slug), + "unread": await get_unread_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)], @@ -71,7 +71,7 @@ async def get_user_info(slug): } -@query.field("refreshSession") +@mutation.field("refreshSession") @login_required async def get_current_user(_, info): user = info.context["request"].user @@ -80,6 +80,7 @@ async def get_current_user(_, info): user.save() session.commit() return { + "token": "", # same token? "user": user, "info": await get_user_info(user.slug) } diff --git a/resolvers/topics.py b/resolvers/topics.py index c9424008..148d401d 100644 --- a/resolvers/topics.py +++ b/resolvers/topics.py @@ -11,19 +11,15 @@ from sqlalchemy import and_ @query.field("topicsAll") async def topics_by_slugs(_, info, page = 1, size = 50): topics = await TopicStorage.get_topics_all(page, size) - all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections] - if "stat" in all_fields: - for topic in topics: - topic.stat = await TopicStat.get_stat(topic.slug) + for topic in topics: + topic.stat = await TopicStat.get_stat(topic.slug) return topics @query.field("topicsByCommunity") async def topics_by_community(_, info, community): topics = await TopicStorage.get_topics_by_community(community) - all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections] - if "stat" in all_fields: - for topic in topics: - topic.stat = await TopicStat.get_stat(topic.slug) + for topic in topics: + topic.stat = await TopicStat.get_stat(topic.slug) return topics @query.field("topicsByAuthor") diff --git a/schema.graphql b/schema.graphql index 30d657da..90cb39c0 100644 --- a/schema.graphql +++ b/schema.graphql @@ -35,10 +35,19 @@ type UserChatsResult { chats: [String] } +type SessionInfo { + unread: Int + topics: [String] + authors: [String] + reactions: [Int] + communities: [String] +} + type AuthResult { error: String token: String user: User + info: SessionInfo } type Result { @@ -308,7 +317,7 @@ type User { id: Int! username: String! # to login, ex. email createdAt: DateTime! - lastSeen: DataTime + lastSeen: DateTime slug: String! name: String # to display email: String