topicsRandom query

This commit is contained in:
tonyrewin 2022-09-05 15:05:20 +03:00
parent 635be7e6a3
commit ef6adbb4ea
2 changed files with 14 additions and 0 deletions

View File

@ -7,6 +7,7 @@ from base.orm import local_session
from base.resolvers import mutation, query from base.resolvers import mutation, query
from auth.authenticate import login_required from auth.authenticate import login_required
from sqlalchemy import and_ from sqlalchemy import and_
import random
@query.field("topicsAll") @query.field("topicsAll")
@ -81,3 +82,15 @@ def topic_unfollow(user, slug):
raise Exception("[resolvers.topics] follower not exist") raise Exception("[resolvers.topics] follower not exist")
session.delete(sub) session.delete(sub)
session.commit() session.commit()
@query.field("topicsRandom")
async def topics_random(_, info):
topics = await TopicStorage.get_topics_all(1, 700)
normalized_topics = []
for topic in topics:
shouts_amount = await TopicStat.get_stat(topic.slug)
topic.stat = shouts_amount
if shouts_amount > 2:
normalized_topics.push(topic)
return random.choices(topics)[0:12]

View File

@ -248,6 +248,7 @@ type Query {
# topics # topics
topicsAll(page: Int!, size: Int!): [Topic]! topicsAll(page: Int!, size: Int!): [Topic]!
topicsRandom: [Topic]!
topicsByCommunity(community: String!): [Topic]! topicsByCommunity(community: String!): [Topic]!
topicsByAuthor(author: String!): [Topic]! topicsByAuthor(author: String!): [Topic]!