From b144a6ab8b0e568f4187094526c3a9fe7997a08e Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 22 Nov 2022 21:48:28 +0300 Subject: [PATCH] init-prepare-comments --- resolvers/zine/reactions.py | 40 +++++++++++-------------------------- schema.graphql | 13 ++++++------ services/stat/reacted.py | 3 +-- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index 0e10c3d8..6423dc71 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -1,7 +1,6 @@ from datetime import datetime, timedelta from sqlalchemy import and_, asc, desc, select, text, func -from sqlalchemy.orm import selectinload from auth.authenticate import login_required from base.orm import local_session from base.resolvers import mutation, query @@ -203,21 +202,20 @@ async def delete_reaction(_, info, rid): def prepare_reactions(q, by): """ query filters and order """ if by.get("shout"): - q = q.filter(Shout.slug == by["shout"]) + q = q.filter(Reaction.shout == by["shout"]) elif by.get("shouts"): - q = q.filter(Shout.slug.in_(by["shouts"])) + q = q.filter(Reaction.shout.in_(by["shouts"])) if by.get("createdBy"): q = q.filter(Reaction.createdBy == by.get("createdBy")) if by.get("topic"): q = q.filter(Shout.topics.contains(by["topic"])) - if by.get("body"): - if by["body"] is True: - q = q.filter(func.length(Reaction.body) > 0) - else: - q = q.filter(Reaction.body.ilike(f'%{by["body"]}%')) + if by.get("comment"): + q = q.filter(func.length(Reaction.body) > 0) + if by.get('search', 0) > 2: + q = q.filter(Reaction.body.ilike(f'%{by["body"]}%')) if by.get("days"): - before = datetime.now() - timedelta(days=int(by["days"]) or 30) - q = q.filter(Reaction.createdAt > before) + after = datetime.now() - 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( @@ -229,14 +227,15 @@ def prepare_reactions(q, by): @query.field("loadReactionsBy") -async def load_reactions_by(_, info, by, limit=50, offset=0): +async def load_reactions_by(_, _info, by, limit=50, offset=0): """ :param by: { :shout - filter by slug :shouts - filer by shouts luglist :createdBy - to filter by author :topic - to filter by topic - :body - to search by body + :search - to search by reactions' body + :comment - true if body.length > 0 :days - a number of days ago :sort - a fieldname to sort desc by default } @@ -244,30 +243,15 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): :param offset: int offset in this order :return: Reaction[] """ - user = None - try: - user = info.context["request"].user - except Exception: - pass - q = select( Reaction - ).options( - selectinload(Reaction.createdBy), - selectinload(Reaction.shout) ).join( User, Reaction.createdBy == User.slug - ).join( - Shout, Reaction.shout == Shout.slug - ).where( - Reaction.deletedAt.is_(None) ) - q = prepare_reactions(q, by) + q = prepare_reactions(q, by).where(Reaction.deletedAt.is_(None)) q = q.limit(limit).offset(offset) - rrr = [] with local_session() as session: - # post query stats and author's captions for r in list(map(lambda r: r.Reaction, session.execute(q))): r.stat = await get_reaction_stat(r.id) rrr.append(r) diff --git a/schema.graphql b/schema.graphql index df0f465d..1b644df1 100644 --- a/schema.graphql +++ b/schema.graphql @@ -248,13 +248,14 @@ input LoadShoutsOptions { } input ReactionBy { - shout: String + shout: String # slug shouts: [String] - body: String - topic: String - createdBy: String - days: Int - sort: String + search: String # fts on body + comment: Boolean + topic: String # topic.slug + createdBy: String # user.slug + days: Int # before + sort: String # how to sort, default createdAt } ################################### Query diff --git a/services/stat/reacted.py b/services/stat/reacted.py index 6258bfe7..00f4fb66 100644 --- a/services/stat/reacted.py +++ b/services/stat/reacted.py @@ -36,8 +36,7 @@ class ReactedStorage: @staticmethod async def get_shout_stat(slug): return { - # TODO - "viewed": 0, # await ViewedStorage.get_shout(slug), + "viewed": await ViewedStorage.get_shout(slug), "reacted": len(await ReactedStorage.get_shout(slug)), "commented": len(await ReactedStorage.get_comments(slug)), "rating": await ReactedStorage.get_rating(slug),