session-info

This commit is contained in:
tonyrewin 2022-08-12 15:50:59 +03:00
parent 0873304068
commit 9c60318919
4 changed files with 21 additions and 20 deletions

View File

@ -36,7 +36,7 @@ class MessageResult:
self.status = status self.status = status
self.message = message 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}") chats = await redis.execute("GET", f"chats_by_user/{user_slug}")
if not chats: if not chats:
return 0 return 0
@ -250,13 +250,8 @@ async def mark_as_read(_, info, chatId, ids):
return {} return {}
@subscription.source("chatUpdated") @subscription.source("chatUpdated")
@login_required
async def message_generator(obj, info, chatId): 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: try:
following_chat = ChatFollowing(chatId) following_chat = ChatFollowing(chatId)
await MessagesStorage.register_chat(following_chat) await MessagesStorage.register_chat(following_chat)

View File

@ -9,7 +9,7 @@ from base.resolvers import mutation, query
from resolvers.community import get_followed_communities from resolvers.community import get_followed_communities
from resolvers.reactions import get_shout_reactions from resolvers.reactions import get_shout_reactions
from auth.authenticate import login_required 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 import and_, desc
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
from typing import List from typing import List
@ -60,10 +60,10 @@ async def user_followers(_, slug) -> List[User]:
all() all()
return users return users
# for query.field("refreshSession") # for mutation.field("refreshSession")
async def get_user_info(slug): async def get_user_info(slug):
return { return {
"inbox": await get_inbox_counter(slug), "unread": await get_unread_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)],
@ -71,7 +71,7 @@ async def get_user_info(slug):
} }
@query.field("refreshSession") @mutation.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
@ -80,6 +80,7 @@ async def get_current_user(_, info):
user.save() user.save()
session.commit() session.commit()
return { return {
"token": "", # same token?
"user": user, "user": user,
"info": await get_user_info(user.slug) "info": await get_user_info(user.slug)
} }

View File

@ -11,19 +11,15 @@ from sqlalchemy import and_
@query.field("topicsAll") @query.field("topicsAll")
async def topics_by_slugs(_, info, page = 1, size = 50): async def topics_by_slugs(_, info, page = 1, size = 50):
topics = await TopicStorage.get_topics_all(page, size) topics = await TopicStorage.get_topics_all(page, size)
all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections] for topic in topics:
if "stat" in all_fields: topic.stat = await TopicStat.get_stat(topic.slug)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics return topics
@query.field("topicsByCommunity") @query.field("topicsByCommunity")
async def topics_by_community(_, info, community): async def topics_by_community(_, info, community):
topics = await TopicStorage.get_topics_by_community(community) topics = await TopicStorage.get_topics_by_community(community)
all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections] for topic in topics:
if "stat" in all_fields: topic.stat = await TopicStat.get_stat(topic.slug)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics return topics
@query.field("topicsByAuthor") @query.field("topicsByAuthor")

View File

@ -35,10 +35,19 @@ type UserChatsResult {
chats: [String] chats: [String]
} }
type SessionInfo {
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
info: SessionInfo
} }
type Result { type Result {
@ -308,7 +317,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 lastSeen: DateTime
slug: String! slug: String!
name: String # to display name: String # to display
email: String email: String