From 522718f3a15e7f919e514fc2ba425d0c3a6700a7 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 6 Aug 2024 18:18:51 +0300 Subject: [PATCH] last-comment-revert --- orm/user.py | 2 +- resolvers/reaction.py | 33 +++++++++++++++------------------ resolvers/reader.py | 20 ++++++++++---------- schema/type.graphql | 2 +- services/db.py | 2 +- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/orm/user.py b/orm/user.py index 550813e6..f381aa00 100644 --- a/orm/user.py +++ b/orm/user.py @@ -24,7 +24,7 @@ class User(Base): # preferred_username = Column(String, nullable=False) picture = Column(String) revoked_timestamp = Column(Integer) - roles = Column(String, default="author, reader") + roles = Column(String, default="author,reader") signup_methods = Column(String, default="magic_link_login") created_at = Column(Integer, default=lambda: int(time.time())) updated_at = Column(Integer, default=lambda: int(time.time())) diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 6ca29db8..84fd99f4 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -21,18 +21,14 @@ from services.viewed import ViewedStorage def add_reaction_stat_columns(q, aliased_reaction): - q = q.outerjoin(aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns( - func.sum(aliased_reaction.id).label("reacted_stat"), - func.sum(case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)).label("comments_stat"), - func.sum(case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)).label("likes_stat"), - func.sum(case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)).label("dislikes_stat"), - func.max( - case( - (aliased_reaction.kind == str(ReactionKind.COMMENT.value), aliased_reaction.created_at), - else_=None, - ) - ).label("last_comment_stat"), - ) + q = q.outerjoin( + aliased_reaction, aliased_reaction.deleted_at.is_(None)).add_columns( + func.sum(aliased_reaction.id).label("reacted_stat"), + func.sum(case((aliased_reaction.kind == str(ReactionKind.COMMENT.value), 1), else_=0)).label("comments_stat"), + func.sum(case((aliased_reaction.kind == str(ReactionKind.LIKE.value), 1), else_=0)).label("likes_stat"), + func.sum(case((aliased_reaction.kind == str(ReactionKind.DISLIKE.value), 1), else_=0)).label("dislikes_stat"), + func.max(aliased_reaction.created_at).label("last_comment_stat") + ) return q @@ -252,7 +248,7 @@ async def update_reaction(_, info, reaction): commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reacted_at, ] = result if not r: return {"error": "invalid reaction id"} @@ -423,7 +419,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): commented_stat, likes_stat, dislikes_stat, - _last_comment, + last_reacted_at, ] in result_rows: reaction.created_by = author reaction.shout = shout @@ -431,6 +427,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): "rating": int(likes_stat or 0) - int(dislikes_stat or 0), "reacted": reacted_stat, "commented": commented_stat, + "last_reacted_at": last_reacted_at } reactions.add(reaction) @@ -465,8 +462,8 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S ) q2 = add_reaction_stat_columns(q2, aliased(Reaction)) - # Sort shouts by the `last_comment` field - combined_query = union(q1, q2).order_by(desc(text("last_comment_stat"))).limit(limit).offset(offset) + # Sort shouts by the `last_reacted_at` field + combined_query = union(q1, q2).order_by(desc(text("last_reacted_at"))).limit(limit).offset(offset) results = session.execute(combined_query).scalars() for [ @@ -475,14 +472,14 @@ async def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[S commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reacted_at, ] in results: shout.stat = { "viewed": await ViewedStorage.get_shout(shout.slug), "rating": int(likes_stat or 0) - int(dislikes_stat or 0), "reacted": reacted_stat, "commented": commented_stat, - "last_comment": last_comment, + "last_reacted_at": last_reacted_at, } shouts.append(shout) diff --git a/resolvers/reader.py b/resolvers/reader.py index 76462f32..9a7c1565 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -103,7 +103,7 @@ async def get_shout(_, info, slug: str): commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reaction_at, ] = results shout.stat = { @@ -111,7 +111,7 @@ async def get_shout(_, info, slug: str): "reacted": reacted_stat, "commented": commented_stat, "rating": int(likes_stat or 0) - int(dislikes_stat or 0), - "last_comment": last_comment, + "last_reacted_at": last_reaction_at, } for author_caption in ( @@ -164,7 +164,7 @@ async def load_shouts_by(_, _info, options): } offset: 0 limit: 50 - order_by: "likes" | "followers" | "comments" | "last_comment" + order_by: "likes" | "followers" | "comments" | "last_reacted_at" order_by_desc: true } @@ -188,7 +188,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_comment"]: + if order_str in ["likes", "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)) @@ -206,7 +206,7 @@ async def load_shouts_by(_, _info, options): commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reacted_at, ] in session.execute(q).unique(): main_topic = ( session.query(Topic.slug) @@ -228,7 +228,7 @@ async def load_shouts_by(_, _info, options): "reacted": reacted_stat, "commented": commented_stat, "rating": int(likes_stat) - int(dislikes_stat), - "last_comment": last_comment, + "last_reacted_at": last_reacted_at, } shouts.append(shout) @@ -270,7 +270,7 @@ async def load_shouts_feed(_, info, options): commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reacted_at, ] in session.execute(q).unique(): main_topic = ( session.query(Topic.slug) @@ -292,7 +292,7 @@ async def load_shouts_feed(_, info, options): "reacted": reacted_stat, "commented": commented_stat, "rating": likes_stat - dislikes_stat, - "last_comment": last_comment, + "last_reacted_at": last_reacted_at, } shouts.append(shout) @@ -365,7 +365,7 @@ async def get_shouts_from_query(q, author_id=None): commented_stat, likes_stat, dislikes_stat, - last_comment, + last_reacted_at, ] in session.execute(q, {"author_id": author_id}).unique(): shouts.append(shout) shout.stat = { @@ -373,7 +373,7 @@ async def get_shouts_from_query(q, author_id=None): "reacted": reacted_stat, "commented": commented_stat, "rating": int(likes_stat or 0) - int(dislikes_stat or 0), - "last_comment": last_comment, + "last_reacted_at": last_reacted_at, } return shouts diff --git a/schema/type.graphql b/schema/type.graphql index a51c880a..c1931628 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -97,7 +97,7 @@ type Stat { rating: Int commented: Int ranking: Int - last_comment: Int + last_reacted_at: Int } type Community { diff --git a/services/db.py b/services/db.py index 43d44698..95c96d83 100644 --- a/services/db.py +++ b/services/db.py @@ -24,7 +24,7 @@ engine = create_engine( max_overflow=20, pool_timeout=30, # Время ожидания свободного соединения pool_recycle=1800, # Время жизни соединения - connect_args={"sslmode": "disable"} + connect_args={"sslmode": "disable"}, ) inspector = inspect(engine) configure_mappers()