From d69f29bda3291dede241ad58b66e78135f7b1eec Mon Sep 17 00:00:00 2001 From: Untone Date: Thu, 22 Feb 2024 23:32:26 +0300 Subject: [PATCH] move-author-fix --- resolvers/author.py | 6 +++--- resolvers/stat.py | 18 ++++++++---------- services/follows.py | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 3b0223ae..06ff0249 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -98,7 +98,7 @@ def count_author_shouts_rating(session, author_id) -> int: async def load_author_with_stats(q): - q = add_author_stat_columns(q) + q = add_author_stat_columns(q, author_model=Author) result = await get_authors_from_query(q) @@ -176,7 +176,7 @@ async def get_author_id(_, _info, user: str): @query.field('load_authors_by') async def load_authors_by(_, _info, by, limit, offset): q = select(Author) - q = add_author_stat_columns(q) + q = add_author_stat_columns(q, author_model=Author) if by.get('slug'): q = q.filter(Author.slug.ilike(f"%{by['slug']}%")) elif by.get('name'): @@ -273,7 +273,7 @@ async def create_author(user_id: str, slug: str, name: str = ''): @query.field('get_author_followers') async def get_author_followers(_, _info, slug) -> List[Author]: q = select(Author) - q = add_author_stat_columns(q) + q = add_author_stat_columns(q, author_model=Author) aliased_author = aliased(Author) q = ( diff --git a/resolvers/stat.py b/resolvers/stat.py index ebc0bf33..50e50366 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -8,23 +8,23 @@ from services.db import local_session # from services.viewed import ViewedStorage -def add_author_stat_columns(q, author_model = None): +def add_author_stat_columns(q, author_model=None): aliased_author = author_model or aliased(Author) shout_author_aliased = aliased(ShoutAuthor) q = q.outerjoin(shout_author_aliased).add_columns( func.count(distinct(shout_author_aliased.shout)).label('shouts_stat') ) + authors_table = aliased(AuthorFollower) + q = q.outerjoin( + authors_table, authors_table.follower == aliased_author.id + ).add_columns(func.count(distinct(authors_table.author)).label('authors_stat')) + followers_table = aliased(AuthorFollower) q = q.outerjoin(followers_table, followers_table.author == aliased_author.id).add_columns( func.count(distinct(followers_table.follower)).label('followers_stat') ) - followings_table = aliased(AuthorFollower) - q = q.outerjoin( - followings_table, followings_table.follower == aliased_author.id - ).add_columns(func.count(distinct(followers_table.author)).label('followings_stat')) - q = q.group_by(aliased_author.id) return q @@ -32,13 +32,11 @@ def add_author_stat_columns(q, author_model = None): async def get_authors_from_query(q): authors = [] with local_session() as session: - for [author, shouts_stat, followers_stat, followings_stat] in session.execute( - q - ): + for [author, shouts_stat, authors_stat, followers_stat] in session.execute(q): author.stat = { 'shouts': shouts_stat, 'followers': followers_stat, - 'followings': followings_stat, + 'followings': authors_stat, # viewed } authors.append(author) diff --git a/services/follows.py b/services/follows.py index ff8a93c3..89aa7d30 100644 --- a/services/follows.py +++ b/services/follows.py @@ -77,7 +77,7 @@ async def update_follows_for_user( async def handle_author_follower_change(connection, author_id, follower_id, is_insert): q = select(Author).filter(Author.id == author_id) - q = add_author_stat_columns(q) + q = add_author_stat_columns(q, author_model=Author) async with connection.begin() as conn: [author, shouts_stat, followers_stat, followings_stat] = await conn.execute( q