From 3f7d9c527bee5516670707d09e982b272aef42f6 Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Wed, 29 Jun 2022 14:32:56 +0300 Subject: [PATCH] return subscribed shout comments in CurrentUserInfo --- resolvers/__init__.py | 3 +-- resolvers/comments.py | 10 ++++++++-- resolvers/profile.py | 26 ++++++-------------------- schema.graphql | 10 +++++----- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 7d164795..0941c76b 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -2,7 +2,7 @@ from resolvers.auth import login, sign_out, is_email_used, register, confirm, au from resolvers.zine import get_shout_by_slug, subscribe, unsubscribe, view_shout, rate_shout, \ top_month, top_overall, recent_published, recent_all, top_viewed, \ shouts_by_authors, shouts_by_topics, shouts_by_communities -from resolvers.profile import get_users_by_slugs, get_current_user, shouts_reviewed, shout_comments_subscribed +from resolvers.profile import get_users_by_slugs, get_current_user, shouts_reviewed from resolvers.topics import topic_subscribe, topic_unsubscribe, topics_by_author, \ topics_by_community, topics_by_slugs from resolvers.comments import create_comment, delete_comment, update_comment, rate_comment @@ -31,7 +31,6 @@ __all__ = [ "shouts_by_topics", "shouts_by_authors", "shouts_by_communities", - "shout_comments_subscribed", "shouts_reviewed", "top_month", "top_overall", diff --git a/resolvers/comments.py b/resolvers/comments.py index 3e58c7d3..0de99dcf 100644 --- a/resolvers/comments.py +++ b/resolvers/comments.py @@ -6,8 +6,6 @@ from auth.authenticate import login_required import asyncio from datetime import datetime -from sqlalchemy import and_ - def comments_subscribe(user, slug): ShoutCommentsSubscription.create( subscriber = user.slug, @@ -103,3 +101,11 @@ async def rate_comment(_, info, id, value): value = value) return {} + +def get_subscribed_shout_comments(slug): + with local_session() as session: + rows = session.query(ShoutCommentsSubscription.shout).\ + filter(ShoutCommentsSubscription.subscriber == slug and not ShoutCommentsSubscription.deletedAt is None).\ + all() + slugs = [row.shout for row in rows] + return slugs diff --git a/resolvers/profile.py b/resolvers/profile.py index 7e603495..930af9aa 100644 --- a/resolvers/profile.py +++ b/resolvers/profile.py @@ -5,6 +5,7 @@ from orm.base import local_session from orm.topic import Topic, TopicSubscription from resolvers.base import mutation, query, subscription from resolvers.community import get_subscribed_communities +from resolvers.comments import get_subscribed_shout_comments from auth.authenticate import login_required from inbox_resolvers.inbox import get_total_unread_messages_for_user @@ -31,10 +32,11 @@ def _get_user_subscribed_authors(slug): async def get_user_info(slug): return { - "totalUnreadMessages" : await get_total_unread_messages_for_user(slug), - "userSubscribedTopics" : _get_user_subscribed_topic_slugs(slug), - "userSubscribedAuthors" : _get_user_subscribed_authors(slug), - "userSubscribedCommunities": get_subscribed_communities(slug) + "totalUnreadMessages" : await get_total_unread_messages_for_user(slug), + "userSubscribedTopics" : _get_user_subscribed_topic_slugs(slug), + "userSubscribedAuthors" : _get_user_subscribed_authors(slug), + "userSubscribedCommunities" : get_subscribed_communities(slug), + "userSubscribedShoutComments": get_subscribed_shout_comments(slug) } @query.field("getCurrentUser") @@ -204,22 +206,6 @@ async def shouts_reviewed(_, info, page, size): return shouts -@query.field("shoutCommentsSubscribed") -@login_required -async def shout_comments_subscribed(_, info, slug, page, size): - user = info.context["request"].user - with local_session() as session: - comments_by_shout = session.query(Comment).\ - join(ShoutCommentsSubscription).\ - join(ShoutCommentsSubscription, ShoutCommentsSubscription.shout == slug).\ - where(ShoutCommentsSubscription.subscriber == user.slug) - comments = comments_by_shout.\ - order_by(desc(Shout.createdAt)).\ - limit(size).\ - offset( (page - 1) * size) - - return shouts - @query.field("shoutsCommentedByUser") async def shouts_commented_by_user(_, info, slug, page, size): user = await UserStorage.get_user_by_slug(slug) diff --git a/schema.graphql b/schema.graphql index d1459826..72f3baf5 100644 --- a/schema.graphql +++ b/schema.graphql @@ -7,10 +7,11 @@ type Result { } type CurrentUserInfo { - totalUnreadMessages: Int - userSubscribedTopics: [String]! - userSubscribedAuthors: [String]! - userSubscribedCommunities: [String]! + totalUnreadMessages: Int + userSubscribedTopics: [String]! + userSubscribedAuthors: [String]! + userSubscribedCommunities: [String]! + userSubscribedShoutComments: [String]! } type AuthResult { @@ -206,7 +207,6 @@ type Query { getCommunity(slug: String): Community! getCommunities: [Community]! - shoutCommentsSubscribed(slug: String!, page: Int!, size: Int!): [Shout]! shoutsReviewed(page: Int!, size: Int!): [Shout]! recentCommented(page: Int!, size: Int!): [Shout]! }