diff --git a/resolvers/editor.py b/resolvers/editor.py index 384e1269..b332b0e7 100644 --- a/resolvers/editor.py +++ b/resolvers/editor.py @@ -81,8 +81,7 @@ def create_shout(_, info, inp): # NOTE: requesting new shout back shout = session.query(Shout).where(Shout.slug == slug).first() if shout: - shout_dict = shout.dict() - sa = ShoutAuthor(shout=shout.id, author=author.id) + sa = ShoutAuthor(shout=shout.id, author=author.id, auto=True) session.add(sa) topics = ( @@ -94,6 +93,8 @@ def create_shout(_, info, inp): t = ShoutTopic(topic=topic.id, shout=shout.id) session.add(t) + session.commit() + reactions_follow(author.id, shout.id, True) # notifier diff --git a/resolvers/stat.py b/resolvers/stat.py index f170f276..f38a4fa1 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -43,15 +43,19 @@ def add_author_stat_columns(q): aliased_author_authors = aliased(AuthorFollower) aliased_author_followers = aliased(AuthorFollower) + aliased_reaction = aliased(Reaction) + q = ( q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id) .add_columns( func.count(distinct(aliased_shout_author.shout)).label('shouts_stat') ) + .outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id) .add_columns( func.count(distinct(aliased_author_authors.author)).label('authors_stat') ) + .outerjoin( aliased_author_followers, aliased_author_followers.author == Author.id ) @@ -60,6 +64,18 @@ def add_author_stat_columns(q): 'followers_stat' ) ) + + .outerjoin(aliased_reaction) + .add_columns( + func.count(distinct(aliased_reaction.id)).filter( + and_( + aliased_reaction.created_by == Author.id, + aliased_reaction.kind == ReactionKind.COMMENT.value, + aliased_reaction.deleted_at.is_(None), + ) + ) + .label('comments_count') + ) ) q = q.group_by(Author.id) @@ -138,9 +154,9 @@ def get_with_stat(q): with local_session() as session: for cols in session.execute(q): entity = cols[0] - entity.stat = {'shouts': cols[1], 'authors': cols[2], 'followers': cols[3]} + entity.stat = {'shouts': cols[1], 'authors': cols[2], 'followers': cols[3], 'comments': cols[4]} if is_author: - # entity.stat['comments'] = cols[4] + # entity.stat[ # entity.stat['rating'] = cols[5] - cols[6] # entity.stat['rating_shouts'] = cols[7] - cols[8] pass diff --git a/schema/type.graphql b/schema/type.graphql index 99173521..cdcdf789 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -5,7 +5,7 @@ type AuthorStat { rating: Int rating_shouts: Int rating_comments: Int - commented: Int + comments: Int viewed: Int } @@ -123,7 +123,7 @@ type TopicStat { shouts: Int! followers: Int! authors: Int! - viewed: Int + comments: Int } type Topic { diff --git a/services/db.py b/services/db.py index 68d11d2f..94ba22ba 100644 --- a/services/db.py +++ b/services/db.py @@ -60,7 +60,7 @@ class Base(declarative_base()): make_searchable(Base.metadata) -Base.metadata.create_all(engine) +Base.metadata.create_all(bind=engine) # Функция для вывода полного трейсбека при предупреждениях diff --git a/services/event_listeners.py b/services/event_listeners.py index 22524839..e0e0863d 100644 --- a/services/event_listeners.py +++ b/services/event_listeners.py @@ -35,13 +35,14 @@ def after_shouts_update(mapper, connection, shout: Shout): Shout.id == shout.id, ShoutAuthor.shout == Shout.id, ShoutAuthor.author == Author.id, - ), - ) + ), + ) ) # Основной запрос с использованием объединения и подзапроса exists authors_query = ( select(Author) + .select_from(Author) .join(ShoutAuthor, Author.id == ShoutAuthor.author) .where(ShoutAuthor.shout == shout.id) .union(select(Author).where(exists(subquery))) @@ -56,6 +57,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction): author_subquery = select(Author).where(Author.id == reaction.created_by) replied_author_subquery = ( select(Author) + .select_from(Author) .join(Reaction, Author.id == Reaction.created_by) .where(Reaction.id == reaction.reply_to) ) diff --git a/services/webhook.py b/services/webhook.py index 818f3cdb..ecce0d1e 100644 --- a/services/webhook.py +++ b/services/webhook.py @@ -34,7 +34,7 @@ class WebhookEndpoint(HTTPEndpoint): ) if author: slug = slug + '-' + user_id.split('-').pop() - await create_author(user_id, slug, name) + create_author(user_id, slug, name) return JSONResponse({'status': 'success'}) except Exception as e: