core/resolvers/topics.py

99 lines
2.9 KiB
Python
Raw Normal View History

import random
from sqlalchemy import and_
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import mutation, query
from orm.topic import Topic, TopicFollower
from services.stat.topicstat import TopicStat
2022-09-07 16:19:13 +00:00
from services.zine.shoutscache import ShoutsCache
from services.zine.topics import TopicStorage
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-14 15:56:49 +00:00
async def topics_all(_, _info):
2022-09-05 13:28:21 +00:00
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")
2022-09-07 16:19:13 +00:00
async def topics_by_author(_, _info, author):
2022-09-07 17:37:11 +00:00
topics = ShoutsCache.by_author.get(author)
2022-09-07 16:19:13 +00:00
author_topics = set()
for tpc in topics:
tpc = await TopicStorage.topics[tpc.slug]
tpc.stat = await TopicStat.get_stat(tpc.slug)
author_topics.add(tpc)
return list(author_topics)
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
@mutation.field("createTopic")
@login_required
2022-09-07 16:19:13 +00:00
async def create_topic(_, _info, inp):
new_topic = Topic.create(**inp)
await TopicStorage.update_topic(new_topic)
2022-09-03 10:50:14 +00:00
return {"topic": new_topic}
@mutation.field("updateTopic")
@login_required
2022-09-07 16:19:13 +00:00
async def update_topic(_, _info, inp):
slug = inp["slug"]
2022-09-03 10:50:14 +00:00
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
return {"error": "topic not found"}
2022-09-07 16:19:13 +00:00
topic.update(**inp)
2022-09-03 10:50:14 +00:00
session.commit()
session.close()
2022-09-07 16:19:13 +00:00
await TopicStorage.update_topic(topic.slug)
2022-09-03 10:50:14 +00:00
return {"topic": topic}
2022-09-07 16:19:13 +00:00
async def topic_follow(user, slug):
TopicFollower.create(topic=slug, follower=user.slug)
2022-09-07 16:19:13 +00:00
await TopicStorage.update_topic(slug)
2022-09-03 10:50:14 +00:00
2022-01-30 08:50:29 +00:00
2022-09-07 16:19:13 +00:00
async 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")
2022-09-07 16:19:13 +00:00
else:
session.delete(sub)
2022-09-03 10:50:14 +00:00
session.commit()
2022-09-07 16:19:13 +00:00
await TopicStorage.update_topic(slug)
2022-09-05 12:05:20 +00:00
@query.field("topicsRandom")
2022-09-06 16:02:05 +00:00
async def topics_random(_, info, amount=12):
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)
2022-09-07 17:37:11 +00:00
# FIXME: expects topicstat fix
# #if topic_stat["shouts"] > 2:
# normalized_topics.append(topic)
2022-09-05 12:41:12 +00:00
topic.stat = topic_stat
2022-09-14 11:09:28 +00:00
normalized_topics.append(topic)
2022-09-07 16:19:13 +00:00
sample_length = min(len(normalized_topics), amount)
return random.sample(normalized_topics, sample_length)