From fab57469d362c2f7eb6590a3febc8b7b915f561d Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 25 Mar 2024 15:03:03 +0300 Subject: [PATCH] random-top-shouts --- resolvers/reader.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index 0888c6b3..acbfaa54 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -381,23 +381,28 @@ async def load_shouts_random_top(_, _info, options): aliased_reaction = aliased(Reaction) subquery = ( - select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None)) + select(Shout.id) + .outerjoin(aliased_reaction) + .where(and_(Shout.deleted_at.is_(None), Shout.layout.is_not(None))) ) subquery = apply_filters(subquery, options.get('filters', {})) + subquery = subquery.group_by(Shout.id).order_by( desc( func.sum( case( - (Reaction.kind == str(ReactionKind.LIKE.value), 1), - (Reaction.kind == str(ReactionKind.DISLIKE.value), -1), + # do not count comments' reactions + (aliased_reaction.replyTo.is_not(None), 0), + (aliased_reaction.kind == ReactionKind.LIKE.value, 1), + (aliased_reaction.kind == ReactionKind.DISLIKE.value, -1), else_=0, + ) ) ) - ) ) - random_limit = options.get('random_limit') + random_limit = options.get('random_limit', 100) if random_limit: subquery = subquery.limit(random_limit) @@ -406,7 +411,7 @@ async def load_shouts_random_top(_, _info, options): .options(joinedload(Shout.authors), joinedload(Shout.topics)) .where(Shout.id.in_(subquery)) ) - aliased_reaction = aliased(Reaction) + q = add_reaction_stat_columns(q, aliased_reaction) limit = options.get('limit', 10) @@ -417,6 +422,7 @@ async def load_shouts_random_top(_, _info, options): return shouts + @query.field('load_shouts_random_topic') async def load_shouts_random_topic(_, info, limit: int = 10): [topic] = get_topics_random(None, None, 1)