2022-09-17 18:12:14 +00:00
|
|
|
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
|
2022-09-17 18:12:14 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
|
|
|
from services.zine.topics import TopicStorage
|
2022-09-19 13:50:43 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
|
|
|
from services.stat.topicstat import TopicStat
|
2022-11-21 05:18:50 +00:00
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-09-19 13:50:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def get_topic_stat(slug):
|
|
|
|
return {
|
2022-10-03 21:45:26 +00:00
|
|
|
"shouts": len(TopicStat.shouts_by_topic.get(slug, {}).keys()),
|
|
|
|
"authors": len(TopicStat.authors_by_topic.get(slug, {}).keys()),
|
|
|
|
"followers": len(TopicStat.followers_by_topic.get(slug, {}).keys()),
|
2022-11-21 05:18:50 +00:00
|
|
|
"viewed": await ViewedStorage.get_topic(slug),
|
2022-09-19 13:50:43 +00:00
|
|
|
"reacted": len(await ReactedStorage.get_topic(slug)),
|
|
|
|
"commented": len(await ReactedStorage.get_topic_comments(slug)),
|
|
|
|
"rating": await ReactedStorage.get_topic_rating(slug)
|
|
|
|
}
|
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:
|
2022-09-19 13:50:43 +00:00
|
|
|
topic.stat = await get_topic_stat(topic.slug)
|
2022-09-03 10:50:14 +00:00
|
|
|
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:
|
2022-09-19 13:50:43 +00:00
|
|
|
topic.stat = await get_topic_stat(topic.slug)
|
2022-09-03 10:50:14 +00:00
|
|
|
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-11-15 02:36:30 +00:00
|
|
|
shouts = TopicStorage.get_topics_by_author(author)
|
2022-09-07 16:19:13 +00:00
|
|
|
author_topics = set()
|
2022-09-19 13:50:43 +00:00
|
|
|
for s in shouts:
|
|
|
|
for tpc in s.topics:
|
|
|
|
tpc = await TopicStorage.topics[tpc.slug]
|
|
|
|
tpc.stat = await get_topic_stat(tpc.slug)
|
|
|
|
author_topics.add(tpc)
|
2022-09-07 16:19:13 +00:00
|
|
|
return list(author_topics)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2021-10-28 10:42:34 +00:00
|
|
|
|
2022-11-10 06:51:40 +00:00
|
|
|
@query.field("getTopic")
|
|
|
|
async def get_topic(_, _info, slug):
|
2022-11-11 15:32:30 +00:00
|
|
|
t = TopicStorage.topics[slug]
|
|
|
|
t.stat = await get_topic_stat(slug)
|
2022-11-10 06:51:40 +00:00
|
|
|
return t
|
|
|
|
|
|
|
|
|
2021-12-12 15:29:51 +00:00
|
|
|
@mutation.field("createTopic")
|
|
|
|
@login_required
|
2022-09-07 16:19:13 +00:00
|
|
|
async def create_topic(_, _info, inp):
|
2022-09-22 10:31:44 +00:00
|
|
|
with local_session() as session:
|
|
|
|
# TODO: check user permissions to create topic for exact community
|
|
|
|
new_topic = Topic.create(**inp)
|
|
|
|
session.add(new_topic)
|
|
|
|
session.commit()
|
2022-09-07 16:19:13 +00:00
|
|
|
await TopicStorage.update_topic(new_topic)
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"topic": new_topic}
|
2021-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("updateTopic")
|
|
|
|
@login_required
|
2022-09-07 16:19:13 +00:00
|
|
|
async def update_topic(_, _info, inp):
|
|
|
|
slug = inp["slug"]
|
2022-09-18 14:29:21 +00:00
|
|
|
with local_session() as session:
|
|
|
|
topic = session.query(Topic).filter(Topic.slug == slug).first()
|
|
|
|
if not topic:
|
|
|
|
return {"error": "topic not found"}
|
|
|
|
else:
|
|
|
|
topic.update(**inp)
|
|
|
|
session.commit()
|
|
|
|
await TopicStorage.update_topic(topic.slug)
|
|
|
|
return {"topic": topic}
|
2021-12-12 15:29:51 +00:00
|
|
|
|
|
|
|
|
2022-09-07 16:19:13 +00:00
|
|
|
async def topic_follow(user, slug):
|
2022-09-18 14:29:21 +00:00
|
|
|
with local_session() as session:
|
|
|
|
following = TopicFollower.create(topic=slug, follower=user.slug)
|
|
|
|
session.add(following)
|
|
|
|
session.commit()
|
|
|
|
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-19 13:50:43 +00:00
|
|
|
topic.stat = await get_topic_stat(topic.slug)
|
|
|
|
if topic.stat["shouts"] > 2:
|
|
|
|
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)
|