From ff834987d410c5c15ea2e31f5cc801c5d023e1aa Mon Sep 17 00:00:00 2001 From: Igor Lobanov Date: Mon, 18 Dec 2023 14:38:45 +0100 Subject: [PATCH] unrated shouts query fix (#112) Co-authored-by: Igor Lobanov --- resolvers/zine/load.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/resolvers/zine/load.py b/resolvers/zine/load.py index bda57f6d..c5f04d6e 100644 --- a/resolvers/zine/load.py +++ b/resolvers/zine/load.py @@ -257,6 +257,9 @@ async def load_random_top_shouts(_, info, params): @query.field("loadUnratedShouts") async def load_unrated_shouts(_, info, limit): auth: AuthCredentials = info.context["request"].auth + user_id = auth.user_id + + aliased_reaction = aliased(Reaction) q = ( select(Shout) @@ -272,22 +275,36 @@ async def load_unrated_shouts(_, info, limit): Reaction.kind.in_([ReactionKind.LIKE, ReactionKind.DISLIKE]), ), ) - .where( + ) + + if user_id: + q = q.outerjoin( + aliased_reaction, and_( - Shout.deletedAt.is_(None), - Shout.layout.is_not(None), - Shout.createdAt >= (datetime.now() - timedelta(days=14)).date(), - ) + aliased_reaction.shout == Shout.id, + aliased_reaction.replyTo.is_(None), + aliased_reaction.kind.in_([ReactionKind.LIKE, ReactionKind.DISLIKE]), + aliased_reaction.createdBy == user_id, + ), + ) + + q = q.where( + and_( + Shout.deletedAt.is_(None), + Shout.layout.is_not(None), + Shout.createdAt >= (datetime.now() - timedelta(days=14)).date(), ) ) - user_id = auth.user_id if user_id: - q = q.where(Reaction.createdBy != user_id) + q = q.where(Shout.createdBy != user_id) # 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3) q = q.having(func.count(distinct(Reaction.id)) <= 4) + if user_id: + q = q.having(func.count(distinct(aliased_reaction.id)) == 0) + q = add_stat_columns(q) q = q.group_by(Shout.id).order_by(func.random()).limit(limit)