postmerge
All checks were successful
deploy / deploy (push) Successful in 1m26s

This commit is contained in:
Untone 2023-12-22 21:08:37 +03:00
parent 4e769332b7
commit d3ea567797
4 changed files with 41 additions and 1 deletions

View File

@ -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",

View File

@ -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}

View File

@ -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

View File

@ -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