diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 585f1261..66ed0eb7 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -35,6 +35,8 @@ from resolvers.reader import ( load_shouts_random_topic, load_shouts_search, load_shouts_unrated, + load_shouts_coauthored, + load_shouts_discussed, ) from resolvers.topic import ( get_topic, @@ -79,6 +81,8 @@ __all__ = [ "load_shouts_followed", "load_shouts_followed_by", "load_shouts_unrated", + "load_shouts_coauthored", + "load_shouts_discussed", "load_shouts_random_top", "load_shouts_random_topic", # follower diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 747de0e1..150c54f5 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -342,6 +342,7 @@ def apply_reaction_filters(by, q): if by.get("comment", False): q = q.filter(Reaction.kind == ReactionKind.COMMENT.value) + if by.get("rating", False): q = q.filter(Reaction.kind.in_(RATING_REACTIONS)) @@ -367,6 +368,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0): :topic - to filter by topic :search - to search by reactions' body :comment - true if body.length > 0 + :rating - true if kind is rating related :after - amount of time ago :sort - a fieldname to sort desc by default } diff --git a/resolvers/reader.py b/resolvers/reader.py index f6daa34c..0414b632 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -1,5 +1,17 @@ from sqlalchemy.orm import aliased, joinedload -from sqlalchemy.sql.expression import and_, asc, bindparam, case, desc, distinct, func, nulls_last, or_, select, text +from sqlalchemy.sql.expression import ( + and_, + asc, + bindparam, + case, + desc, + distinct, + func, + nulls_last, + or_, + select, + text, +) from orm.author import Author, AuthorFollower from orm.reaction import Reaction, ReactionKind @@ -154,7 +166,7 @@ async def load_shouts_by(_, _info, options): } offset: 0 limit: 50 - order_by: 'created_at' | 'commented' | 'likes_stat' + order_by: "likes" | "followers" | "comments" | "last_comment" order_by_desc: true } @@ -331,7 +343,10 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0): Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]), ), ) - .outerjoin(Author, and_(Author.user == bindparam("user_id"), Reaction.created_by == Author.id)) + .outerjoin( + Author, + and_(Author.user == bindparam("user_id"), Reaction.created_by == Author.id), + ) .where( and_( Shout.deleted_at.is_(None), @@ -471,3 +486,33 @@ def fetch_shouts_by_topic(topic, limit): shouts = get_shouts_from_query(q) return shouts + + +@query.field("load_shouts_coauthored") +@login_required +async def load_shouts_coauthored(_, info, limit=50, offset=0): + author_id = info.context.get("author", {}).get("id") + shouts_query = query_shouts().filter(Shout.authors.any(id=author_id)).where(Shout.deleted_at.is_(None)) + shouts = get_shouts_from_query(shouts_query.limit(limit).offset(offset)) + return shouts + + +@query.field("load_shouts_discussed") +@login_required +async def load_shouts_discussed(_, info, limit=50, offset=0): + author_id = info.context.get("author", {}).get("id") + q = query_shouts() + q = ( + q.outerjoin( + Reaction, + and_( + Reaction.shout == Shout.id, + Reaction.created_by == author_id, + Reaction.kind.is_(ReactionKind.COMMENT.value), + ), + ) + .outerjoin(Author, Reaction.created_by == Author.id) + .where(and_(Shout.deleted_at.is_(None), Shout.published_at.is_not(None))) + ) + q = q.limit(limit).offset(offset) + return await get_shouts_from_query(q) diff --git a/resolvers/stat.py b/resolvers/stat.py index 4418fdfb..6ac9807c 100644 --- a/resolvers/stat.py +++ b/resolvers/stat.py @@ -76,7 +76,6 @@ def add_author_stat_columns(q): return q - def get_topic_shouts_stat(topic_id: int): q = ( select(func.count(distinct(ShoutTopic.shout))) diff --git a/schema/query.graphql b/schema/query.graphql index c57f4c41..ccaee9bd 100644 --- a/schema/query.graphql +++ b/schema/query.graphql @@ -30,6 +30,8 @@ type Query { load_shouts_search(text: String!, limit: Int, offset: Int): [SearchResult] load_shouts_feed(options: LoadShoutsOptions): [Shout] load_shouts_unrated(limit: Int, offset: Int): [Shout] + load_shouts_coauthored(limit: Int, offset: Int): [Shout] + load_shouts_discussed(limit: Int, offset: Int): [Shout] load_shouts_random_top(options: LoadShoutsOptions): [Shout] load_shouts_random_topic(limit: Int!): CommonResult! # { topic shouts }