core/resolvers/topics.py

97 lines
2.6 KiB
Python
Raw Normal View History

from orm.topic import Topic, TopicFollower
2022-08-11 09:09:57 +00:00
from services.zine.topics import TopicStorage
from orm.shout import Shout
from orm.user import User
2022-08-11 09:09:57 +00:00
from services.stat.topicstat import TopicStat
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import mutation, query
2021-10-28 10:42:34 +00:00
from auth.authenticate import login_required
from sqlalchemy import and_
2022-09-06 15:12:16 +00:00
from numpy import random
2022-01-30 08:50:29 +00:00
2022-09-03 10:50:14 +00:00
2022-07-10 18:52:57 +00:00
@query.field("topicsAll")
2022-09-05 13:28:21 +00:00
async def topics_all(_, info):
topics = await TopicStorage.get_topics_all()
2022-09-03 10:50:14 +00:00
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
2021-10-28 10:42:34 +00:00
@query.field("topicsByCommunity")
async def topics_by_community(_, info, community):
2022-09-03 10:50:14 +00:00
topics = await TopicStorage.get_topics_by_community(community)
for topic in topics:
topic.stat = await TopicStat.get_stat(topic.slug)
return topics
2021-10-28 10:42:34 +00:00
@query.field("topicsByAuthor")
async def topics_by_author(_, info, author):
2022-09-03 10:50:14 +00:00
slugs = set()
with local_session() as session:
shouts = session.query(Shout).filter(Shout.authors.any(User.slug == author))
for shout in shouts:
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
2021-10-28 10:42:34 +00:00
@mutation.field("createTopic")
@login_required
async def create_topic(_, info, input):
2022-09-03 10:50:14 +00:00
new_topic = Topic.create(**input)
await TopicStorage.add_topic(new_topic)
return {"topic": new_topic}
@mutation.field("updateTopic")
@login_required
async def update_topic(_, info, input):
2022-09-03 10:50:14 +00:00
slug = input["slug"]
2022-09-03 10:50:14 +00:00
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
2022-09-03 10:50:14 +00:00
if not topic:
return {"error": "topic not found"}
2022-09-03 10:50:14 +00:00
topic.update(input)
session.commit()
session.close()
2022-09-03 10:50:14 +00:00
await TopicStorage.add_topic(topic)
return {"topic": topic}
def topic_follow(user, slug):
2022-09-03 10:50:14 +00:00
TopicFollower.create(follower=user.slug, topic=slug)
2022-01-30 08:50:29 +00:00
def topic_unfollow(user, slug):
2022-09-03 10:50:14 +00:00
with local_session() as session:
sub = (
session.query(TopicFollower)
.filter(
and_(TopicFollower.follower == user.slug, TopicFollower.topic == slug)
)
.first()
)
if not sub:
raise Exception("[resolvers.topics] follower not exist")
session.delete(sub)
session.commit()
2022-09-05 12:05:20 +00:00
@query.field("topicsRandom")
async def topics_random(_, info):
2022-09-05 13:28:21 +00:00
topics = await TopicStorage.get_topics_all()
2022-09-05 12:05:20 +00:00
normalized_topics = []
for topic in topics:
2022-09-05 12:41:12 +00:00
topic_stat = await TopicStat.get_stat(topic.slug)
topic.stat = topic_stat
if topic_stat["shouts"] > 2:
2022-09-05 12:05:20 +00:00
normalized_topics.push(topic)
2022-09-06 15:12:16 +00:00
return random.shuffle(normalized_topics)[0:12]