From 77d8e2f2fd4be32d0c582bdfeeb5ccac9c194c41 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Mon, 28 Nov 2022 16:15:52 +0300 Subject: [PATCH 1/2] pushed-wip --- resolvers/zine/reactions.py | 12 ++++-------- schema.graphql | 4 ++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index 3f4fccf1..f071a93e 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -201,11 +201,10 @@ async def delete_reaction(_, info, rid): def map_result_item(result_item): - [user, shout, reaction] = result_item + [reaction, user, shout] = result_item print(reaction) reaction.createdBy = user reaction.shout = shout - reaction.replyTo = reaction return reaction @@ -229,15 +228,12 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0): CreatedByUser = aliased(User) ReactedShout = aliased(Shout) - RepliedReaction = aliased(Reaction) q = select( - Reaction, CreatedByUser, ReactedShout, RepliedReaction + Reaction, CreatedByUser, ReactedShout ).join( CreatedByUser, Reaction.createdBy == CreatedByUser.slug ).join( ReactedShout, Reaction.shout == ReactedShout.slug - ).join( - RepliedReaction, Reaction.replyTo == RepliedReaction.id ) if by.get("shout"): @@ -268,10 +264,10 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0): reactions = [] with local_session() as session: for [ - [reaction, rating, commented, reacted], shout, reply + [reaction, rating, commented, reacted], user, shout ] in list(map(map_result_item, session.execute(q))): + reaction.createdBy = user reaction.shout = shout - reaction.replyTo = reply reaction.stat = { "rating": rating, "commented": commented, diff --git a/schema.graphql b/schema.graphql index b61f7b43..9933cb35 100644 --- a/schema.graphql +++ b/schema.graphql @@ -152,7 +152,7 @@ type Mutation { updateChat(chat: ChatInput!): Result! deleteChat(chatId: String!): Result! - createMessage(chat: String!, body: String!, replyTo: String): Result! + createMessage(chat: String!, body: String!, replyTo: Int): Result! updateMessage(chatId: String!, id: Int!, body: String!): Result! deleteMessage(chatId: String!, id: Int!): Result! markAsRead(chatId: String!, ids: [Int]!): Result! @@ -410,7 +410,7 @@ type Reaction { range: String # full / 0:2340 kind: ReactionKind! body: String - replyTo: Reaction + replyTo: Int stat: Stat old_id: String old_thread: String From 7da187470a44b088aefe43e18ff59a272cd65342 Mon Sep 17 00:00:00 2001 From: Igor Lobanov Date: Mon, 28 Nov 2022 14:37:05 +0100 Subject: [PATCH 2/2] loadReactionsBy fix --- resolvers/zine/load.py | 25 +++++++++++++------------ resolvers/zine/reactions.py | 18 ++++++------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/resolvers/zine/load.py b/resolvers/zine/load.py index b10e90a1..53b1841a 100644 --- a/resolvers/zine/load.py +++ b/resolvers/zine/load.py @@ -1,6 +1,6 @@ from datetime import datetime, timedelta, timezone import sqlalchemy as sa -from sqlalchemy.orm import joinedload +from sqlalchemy.orm import joinedload, aliased from sqlalchemy.sql.expression import desc, asc, select, case from base.orm import local_session from base.resolvers import query @@ -12,26 +12,27 @@ from services.stat.viewed import ViewedStorage def calc_reactions(q): - return q.join(Reaction).add_columns( + aliased_reaction = aliased(Reaction) + return q.join(aliased_reaction).add_columns( sa.func.sum(case( - (Reaction.kind == ReactionKind.AGREE, 1), - (Reaction.kind == ReactionKind.DISAGREE, -1), - (Reaction.kind == ReactionKind.PROOF, 1), - (Reaction.kind == ReactionKind.DISPROOF, -1), - (Reaction.kind == ReactionKind.ACCEPT, 1), - (Reaction.kind == ReactionKind.REJECT, -1), - (Reaction.kind == ReactionKind.LIKE, 1), - (Reaction.kind == ReactionKind.DISLIKE, -1), + (aliased_reaction.kind == ReactionKind.AGREE, 1), + (aliased_reaction.kind == ReactionKind.DISAGREE, -1), + (aliased_reaction.kind == ReactionKind.PROOF, 1), + (aliased_reaction.kind == ReactionKind.DISPROOF, -1), + (aliased_reaction.kind == ReactionKind.ACCEPT, 1), + (aliased_reaction.kind == ReactionKind.REJECT, -1), + (aliased_reaction.kind == ReactionKind.LIKE, 1), + (aliased_reaction.kind == ReactionKind.DISLIKE, -1), else_=0) ).label('rating'), sa.func.sum( case( - (Reaction.body.is_not(None), 1), + (aliased_reaction.body.is_not(None), 1), else_=0 ) ).label('commented'), sa.func.sum( - Reaction.id + aliased_reaction.id ).label('reacted') ) diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index f071a93e..b77d4e5e 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -199,15 +199,6 @@ async def delete_reaction(_, info, rid): session.commit() return {} - -def map_result_item(result_item): - [reaction, user, shout] = result_item - print(reaction) - reaction.createdBy = user - reaction.shout = shout - return reaction - - @query.field("loadReactionsBy") async def load_reactions_by(_, _info, by, limit=50, offset=0): """ @@ -251,21 +242,24 @@ async def load_reactions_by(_, _info, by, limit=50, offset=0): if by.get("days"): after = datetime.now(tz=timezone.utc) - timedelta(days=int(by["days"]) or 30) q = q.filter(Reaction.createdAt > after) + order_way = asc if by.get("sort", "").startswith("-") else desc order_field = by.get("sort") or Reaction.createdAt + q = q.group_by( Reaction.id, CreatedByUser.id, ReactedShout.id ).order_by( order_way(order_field) ) + q = calc_reactions(q) + q = q.where(Reaction.deletedAt.is_(None)) q = q.limit(limit).offset(offset) reactions = [] + with local_session() as session: - for [ - [reaction, rating, commented, reacted], user, shout - ] in list(map(map_result_item, session.execute(q))): + for [reaction, user, shout, rating, commented, reacted] in session.execute(q): reaction.createdBy = user reaction.shout = shout reaction.stat = {