diff --git a/cache/cache.py b/cache/cache.py index 5b4f58ae..6a03da49 100644 --- a/cache/cache.py +++ b/cache/cache.py @@ -104,7 +104,6 @@ async def get_cached_topic(topic_id: int): return None - # Get topic by slug from cache async def get_cached_topic_by_slug(slug: str): topic_key = f"topic:slug:{slug}" diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 952414da..33550f14 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -21,19 +21,21 @@ from services.viewed import ViewedStorage def add_reaction_stat_columns(q, aliased_reaction): - reaction_subquery = ( - select( - aliased_reaction.shout, - func.count(aliased_reaction.id).label("reacted_stat"), - func.count(case([(aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1)])).label("comments_stat"), - func.count(case([(aliased_reaction.kind == str(ReactionKind.LIKE.value), 1)])).label("likes_stat"), - func.count(case([(aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1)])).label("dislikes_stat"), - func.max(aliased_reaction.created_at).label("last_comment_stat"), - ) - .where(aliased_reaction.deleted_at.is_(None)) - .group_by(aliased_reaction.shout) + # Adjust case statements to use positional arguments + q = q.outerjoin(aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns( + func.count(case((aliased_reaction.body.is_not(None), 1), else_=0)).label("comments_stat"), + # Calculate net likes as likes - dislikes + func.sum( + case( + (aliased_reaction.kind == ReactionKind.LIKE.value, 1), + (aliased_reaction.kind == ReactionKind.DISLIKE.value, -1), + else_=0, + ) + ).label("rating_stat"), + func.max(aliased_reaction.created_at).label("last_comment_stat"), ) - return q.outerjoin(reaction_subquery, Shout.id == reaction_subquery.c.shout) + + return q def is_featured_author(session, author_id): @@ -247,10 +249,8 @@ async def update_reaction(_, info, reaction): if result: [ r, - reacted_stat, commented_stat, - likes_stat, - dislikes_stat, + rating_stat, last_reacted_at, ] = result if not r: @@ -275,9 +275,8 @@ async def update_reaction(_, info, reaction): session.commit() r.stat = { - "reacted": reacted_stat, "commented": commented_stat, - "rating": int(likes_stat or 0) - int(dislikes_stat or 0), + "rating": rating_stat, } await notify_reaction(r.dict(), "update") diff --git a/resolvers/reader.py b/resolvers/reader.py index 55489ce8..035c3f2f 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -100,18 +100,15 @@ async def get_shout(_, info, slug: str): if results: [ shout, - reacted_stat, commented_stat, - likes_stat, - dislikes_stat, + rating_stat, last_reaction_at, ] = results shout.stat = { - "viewed": await ViewedStorage.get_shout(shout.slug), - "reacted": reacted_stat, + # "viewed": await ViewedStorage.get_shout(shout.slug), "commented": commented_stat, - "rating": int(likes_stat or 0) - int(dislikes_stat or 0), + "rating": rating_stat, "last_reacted_at": last_reaction_at, } @@ -165,7 +162,7 @@ async def load_shouts_by(_, _info, options): } offset: 0 limit: 50 - order_by: "likes" | "followers" | "comments" | "last_reacted_at" + order_by: "rating" | "followers" | "comments" | "last_reacted_at" order_by_desc: true } @@ -189,7 +186,7 @@ async def load_shouts_by(_, _info, options): # order order_by = Shout.featured_at if filters.get("featured") else Shout.published_at order_str = options.get("order_by") - if order_str in ["likes", "followers", "comments", "last_reacted_at"]: + if order_str in ["rating", "followers", "comments", "last_reacted_at"]: q = q.order_by(desc(text(f"{order_str}_stat"))) query_order_by = desc(order_by) if options.get("order_by_desc", True) else asc(order_by) q = q.order_by(nulls_last(query_order_by)) @@ -203,10 +200,8 @@ async def load_shouts_by(_, _info, options): with local_session() as session: for [ shout, - reacted_stat, commented_stat, - likes_stat, - dislikes_stat, + rating_stat, last_reacted_at, ] in session.execute(q).unique(): main_topic = ( @@ -225,10 +220,9 @@ async def load_shouts_by(_, _info, options): if main_topic: shout.main_topic = main_topic[0] shout.stat = { - "viewed": await ViewedStorage.get_shout(shout.slug), - "reacted": reacted_stat, + # "viewed": await ViewedStorage.get_shout(shout.slug), "commented": commented_stat, - "rating": int(likes_stat) - int(dislikes_stat), + "rating": rating_stat, "last_reacted_at": last_reacted_at, } shouts.append(shout) @@ -267,10 +261,8 @@ async def load_shouts_feed(_, info, options): for [ shout, - reacted_stat, commented_stat, - likes_stat, - dislikes_stat, + rating_stat, last_reacted_at, ] in session.execute(q).unique(): main_topic = ( @@ -290,9 +282,8 @@ async def load_shouts_feed(_, info, options): shout.main_topic = main_topic[0] shout.stat = { "viewed": await ViewedStorage.get_shout(shout.slug), - "reacted": reacted_stat, "commented": commented_stat, - "rating": likes_stat - dislikes_stat, + "rating": rating_stat, "last_reacted_at": last_reacted_at, } shouts.append(shout) @@ -362,18 +353,15 @@ async def get_shouts_from_query(q, author_id=None): with local_session() as session: for [ shout, - reacted_stat, commented_stat, - likes_stat, - dislikes_stat, + rating_stat, last_reacted_at, ] in session.execute(q, {"author_id": author_id}).unique(): shouts.append(shout) shout.stat = { - "viewed": await ViewedStorage.get_shout(shout_slug=shout.slug), - "reacted": reacted_stat, + # "viewed": await ViewedStorage.get_shout(shout_slug=shout.slug), "commented": commented_stat, - "rating": int(likes_stat or 0) - int(dislikes_stat or 0), + "rating": rating_stat, "last_reacted_at": last_reacted_at, } diff --git a/schema/type.graphql b/schema/type.graphql index c1931628..94dda1da 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -92,8 +92,6 @@ type Shout { } type Stat { - viewed: Int - reacted: Int rating: Int commented: Int ranking: Int