From a4411cfa343b043498b71934ce366ca9cea95da8 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 22 Jul 2024 11:32:47 +0300 Subject: [PATCH] comment-ratings --- CHANGELOG.txt | 5 +++++ pyproject.toml | 2 +- resolvers/__init__.py | 4 +++- resolvers/reaction.py | 45 ++++++++++++++++++++++++++++++++++++++++--- schema/query.graphql | 1 + 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 6f02d101..be101997 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,8 @@ +[0.4.2] +- reactions load resolvers separated for ratings (no stats) and comments +- reactions stats improved +- load_comment_ratings separate resolver + [0.4.1] - follow/unfollow logic updated and unified with cache diff --git a/pyproject.toml b/pyproject.toml index cf9c34f6..6a4c9eec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "core" -version = "0.4.1" +version = "0.4.2" description = "core module for discours.io" authors = ["discoursio devteam"] license = "MIT" diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 18f811f9..e30e21e2 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -27,7 +27,8 @@ from resolvers.reaction import ( load_shouts_followed_by, update_reaction, load_shout_comments, - load_shout_ratings + load_shout_ratings, + load_comment_ratings, ) from resolvers.reader import ( get_shout, @@ -102,6 +103,7 @@ __all__ = [ "load_reactions_by", "load_shout_comments", "load_shout_ratings", + "load_comment_ratings", # notifier "load_notifications", "notifications_seen_thread", diff --git a/resolvers/reaction.py b/resolvers/reaction.py index a24b55e6..dd247016 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -379,7 +379,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): :after - amount of time ago :sort - a fieldname to sort desc by default } - :param limit: int amount of shouts + :param limit: int amount of reactions :param offset: int offset in this order :return: Reaction[] """ @@ -528,7 +528,7 @@ async def load_shout_ratings(_, info, shout: int, limit=100, offset=0): get paginated reactions with no stats :param info: graphql meta :param shout: int shout id - :param limit: int amount of shouts + :param limit: int amount of reactions :param offset: int offset in this order :return: Reaction[] """ @@ -567,7 +567,7 @@ async def load_shout_comments(_, info, shout: int, limit=50, offset=0): getting paginated comments with stats :param info: graphql meta :param shout: int shout id - :param limit: int amount of shouts + :param limit: int amount of reactions :param offset: int offset in this order :return: Reaction[] """ @@ -612,3 +612,42 @@ async def load_shout_comments(_, info, shout: int, limit=50, offset=0): reactions.add(reaction) return reactions + + +@query.field("load_comment_ratings") +async def load_comment_ratings(_, info, comment: int, limit=100, offset=0): + """ + get paginated reactions with no stats + :param info: graphql meta + :param comment: int comment id + :param limit: int amount of reactions + :param offset: int offset in this order + :return: Reaction[] + """ + + q = ( + select(Reaction, Author, Shout) + .select_from(Reaction) + .join(Author, Reaction.created_by == Author.id) + .join(Shout, Reaction.shout == Shout.id) + ) + + # filter, group, order, limit, offset + q = q.filter(and_(Reaction.deleted_at.is_(None), Reaction.reply_to == comment, Reaction.kind.in_(RATING_REACTIONS))) + q = q.group_by(Reaction.id) + q = q.order_by(desc(Reaction.created_at)) + q = q.limit(limit).offset(offset) + + reactions = set() + with local_session() as session: + result_rows = session.execute(q) + for [ + reaction, + author, + shout, + ] in result_rows: + reaction.created_by = author + reaction.shout = shout + reactions.add(reaction) + + return reactions diff --git a/schema/query.graphql b/schema/query.graphql index f688c5ca..7a6bf278 100644 --- a/schema/query.graphql +++ b/schema/query.graphql @@ -29,6 +29,7 @@ type Query { load_shouts_by(options: LoadShoutsOptions): [Shout] load_shout_comments(shout: Int!, limit: Int, offset: Int): [Reaction] load_shout_ratings(shout: Int!, limit: Int, offset: Int): [Reaction] + load_comment_ratings(shout: Int!, limit: Int, offset: Int): [Reaction] load_shouts_search(text: String!, limit: Int, offset: Int): [SearchResult] load_shouts_feed(options: LoadShoutsOptions): [Shout] load_shouts_unrated(limit: Int, offset: Int): [Shout]