From d3ea567797334d862553abb49e47b834b7e99134 Mon Sep 17 00:00:00 2001 From: Untone Date: Fri, 22 Dec 2023 21:08:37 +0300 Subject: [PATCH] postmerge --- resolvers/__init__.py | 2 ++ resolvers/reader.py | 27 ++++++++++++++++++++++++++- resolvers/topic.py | 12 ++++++++++++ schemas/core.graphql | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/resolvers/__init__.py b/resolvers/__init__.py index 97c18202..abc6013d 100644 --- a/resolvers/__init__.py +++ b/resolvers/__init__.py @@ -26,6 +26,7 @@ from resolvers.reader import ( load_shouts_random_top, load_shouts_search, load_shouts_unrated, + load_shouts_random_topic ) from resolvers.topic import get_topic, get_topics_all, get_topics_by_author, get_topics_by_community @@ -56,6 +57,7 @@ __all__ = [ "load_shouts_followed", "load_shouts_unrated", "load_shouts_random_top", + "load_shouts_random_topic", # follower "follow", "unfollow", diff --git a/resolvers/reader.py b/resolvers/reader.py index e082744d..e111a06a 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -12,7 +12,7 @@ from services.db import local_session from services.schema import query from services.search import SearchService from services.viewed import ViewedStorage - +from resolvers.topic import get_random_topic def add_stat_columns(q): aliased_reaction = aliased(Reaction) @@ -407,3 +407,28 @@ async def load_shouts_random_top(_, _info, params): # print(q.compile(compile_kwargs={"literal_binds": True})) return get_shouts_from_query(q) + +@query.field("load_shouts_random_topic") +async def load_shouts_random_topic(_, info, limit): + topic = get_random_topic() + shouts = [] + if topic: + q = ( + select(Shout) + .options( + joinedload(Shout.authors), + joinedload(Shout.topics), + ) + .join(ShoutTopic, and_(Shout.id == ShoutTopic.shout, ShoutTopic.topic == topic.id)) + .where( + and_(Shout.deletedAt.is_(None), Shout.layout.is_not(None), Shout.visibility == "public") + ) + ) + + q = add_stat_columns(q) + + q = q.group_by(Shout.id).order_by(desc(Shout.createdAt)).limit(limit) + + shouts = get_shouts_from_query(q) + + return {"topic": topic, "shouts": shouts} diff --git a/resolvers/topic.py b/resolvers/topic.py index c7128b91..45b9138f 100644 --- a/resolvers/topic.py +++ b/resolvers/topic.py @@ -187,3 +187,15 @@ async def get_topics_random(_, info, amount=12): topics.append(topic) return topics + + +def get_random_topic(): + q = select(Topic) + q = q.join(ShoutTopic) + q = q.group_by(Topic.id) + q = q.having(func.count(distinct(ShoutTopic.shout)) > 10) + q = q.order_by(func.random()).limit(1) + topic = None + with local_session() as session: + topic = session.execute(q).first() + return topic diff --git a/schemas/core.graphql b/schemas/core.graphql index 14b19180..9c89093c 100644 --- a/schemas/core.graphql +++ b/schemas/core.graphql @@ -364,6 +364,7 @@ type Query { load_shouts_feed(options: LoadShoutsOptions): [Shout] load_shouts_unrated(limit: Int, offset: Int): [Shout] load_shouts_random_top(options: LoadShoutsOptions): [Shout] + load_shouts_random_topic(limit: Int!): Result! # { topic shouts } load_shouts_drafts: [Shout] # topic