From 45d187786bde0a78f57744541c4aa4bcec408d0d Mon Sep 17 00:00:00 2001 From: Tony Rewin Date: Thu, 5 Oct 2023 22:59:50 +0300 Subject: [PATCH] fix-imports --- requirements.txt | 2 +- resolvers/auth.py | 2 +- resolvers/editor.py | 2 +- resolvers/following.py | 9 +++++---- resolvers/profile.py | 6 +++--- services/unread.py | 24 ++++++++++++++++++++++++ 6 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 services/unread.py diff --git a/requirements.txt b/requirements.txt index 659f7c2f..6a55c667 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ aredis~=1.1.8 ariadne>=0.17.0 PyYAML>=5.4 pyjwt>=2.6.0 -starlette +starlette @ git+https://github.com/starlette/starlette#main sqlalchemy>=1.4.41 graphql-core>=3.0.3 gql~=3.4.0 diff --git a/resolvers/auth.py b/resolvers/auth.py index 62a854f9..ebbb6eae 100644 --- a/resolvers/auth.py +++ b/resolvers/auth.py @@ -23,7 +23,7 @@ from services.exceptions import ( from services.db import local_session from services.schema import mutation, query from orm import Role, User -from resolvers.zine.profile import user_subscriptions +from resolvers.profile import user_subscriptions from settings import SESSION_TOKEN_HEADER, FRONTEND_URL diff --git a/resolvers/editor.py b/resolvers/editor.py index da5de73f..c9cba2da 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -9,7 +9,7 @@ from services.db import local_session from services.schema import mutation from orm.shout import Shout, ShoutAuthor, ShoutTopic from orm.topic import Topic -from resolvers.zine.reactions import reactions_follow, reactions_unfollow +from resolvers.reactions import reactions_follow, reactions_unfollow @mutation.field("createShout") diff --git a/resolvers/following.py b/resolvers/following.py index a895f666..2e896189 100644 --- a/resolvers/following.py +++ b/resolvers/following.py @@ -8,9 +8,9 @@ from auth.credentials import AuthCredentials from orm.user import AuthorFollower from orm.topic import TopicFollower from orm.shout import ShoutReactionsFollower -from resolvers.zine.profile import author_follow, author_unfollow -from resolvers.zine.reactions import reactions_follow, reactions_unfollow -from resolvers.zine.topics import topic_follow, topic_unfollow +from resolvers.profile import author_follow, author_unfollow +from resolvers.reactions import reactions_follow, reactions_unfollow +from resolvers.topics import topic_follow, topic_unfollow from services.following import Following, FollowingManager, FollowingResult from graphql.type import GraphQLResolveInfo @@ -30,7 +30,8 @@ async def follow(_, info, what, slug): result = FollowingResult("NEW", "topic", slug) await FollowingManager.push("topic", result) elif what == "COMMUNITY": - if False: # TODO: use community_follow(auth.user_id, slug): + if False: + # TODO: use community_follow(auth.user_id, slug): result = FollowingResult("NEW", "community", slug) await FollowingManager.push("community", result) elif what == "REACTIONS": diff --git a/resolvers/profile.py b/resolvers/profile.py index 57331999..d13eea30 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -5,7 +5,7 @@ from sqlalchemy.orm import aliased, joinedload from auth.authenticate import login_required from auth.credentials import AuthCredentials -from services.orm import local_session +from services.db import local_session from services.schema import mutation, query from orm.reaction import Reaction from orm.shout import ShoutAuthor, ShoutTopic @@ -13,8 +13,8 @@ from orm.topic import Topic 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 services.unread import get_total_unread_counter +from resolvers.topics import followed_by_user def add_author_stat_columns(q, include_heavy_stat=False): diff --git a/services/unread.py b/services/unread.py new file mode 100644 index 00000000..abbd81da --- /dev/null +++ b/services/unread.py @@ -0,0 +1,24 @@ +from services.redis import redis +import json + + +async def get_unread_counter(chat_id: str, author_id: int): + try: + unread = await redis.execute( + "LLEN", f"chats/{chat_id.decode('utf-8')}/unread/{author_id}" + ) + if unread: + return unread + except Exception: + return 0 + + +async def get_total_unread_counter(author_id: int): + chats = await redis.execute("GET", f"chats_by_author/{author_id}") + unread = 0 + if chats: + chats = json.loads(chats) + for chat_id in chats: + n = await get_unread_counter(chat_id.decode("utf-8"), author_id) + unread += n + return unread