core/resolvers/zine/topics.py

153 lines
4.1 KiB
Python
Raw Normal View History

2022-11-23 15:57:22 +00:00
import sqlalchemy as sa
2022-11-28 20:29:02 +00:00
from sqlalchemy import and_, select, distinct
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-11-28 20:29:02 +00:00
from orm.shout import ShoutTopic, ShoutAuthor
from orm.topic import Topic, TopicFollower
from orm import Shout
2022-09-19 13:50:43 +00:00
2022-11-28 20:29:02 +00:00
def add_topic_stat_columns(q):
q = q.outerjoin(ShoutTopic, Topic.slug == ShoutTopic.topic).add_columns(
sa.func.count(distinct(ShoutTopic.shout)).label('shouts_stat')
).outerjoin(ShoutAuthor, ShoutTopic.shout == ShoutAuthor.shout).add_columns(
sa.func.count(distinct(ShoutAuthor.user)).label('authors_stat')
).outerjoin(TopicFollower,
and_(
TopicFollower.topic == Topic.slug,
TopicFollower.follower == ShoutAuthor.user
)).add_columns(
sa.func.count(distinct(TopicFollower.follower)).label('followers_stat')
)
q = q.group_by(Topic.id)
return q
def add_stat(topic, stat_columns):
[shouts_stat, authors_stat, followers_stat] = stat_columns
topic.stat = {
"shouts": shouts_stat,
"authors": authors_stat,
"followers": followers_stat
2022-09-19 13:50:43 +00:00
}
2022-01-30 08:50:29 +00:00
2022-11-28 20:29:02 +00:00
return topic
def get_topics_from_query(q):
topics = []
with local_session() as session:
for [topic, *stat_columns] in session.execute(q):
topic = add_stat(topic, stat_columns)
topics.append(topic)
return topics
def followed_by_user(user_slug):
q = select(Topic)
q = add_topic_stat_columns(q)
q = q.where(TopicFollower.follower == user_slug)
return get_topics_from_query(q)
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-11-28 20:29:02 +00:00
q = select(Topic)
q = add_topic_stat_columns(q)
return get_topics_from_query(q)
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
@query.field("topicsByCommunity")
async def topics_by_community(_, info, community):
2022-11-28 20:29:02 +00:00
q = select(Topic).where(Topic.community == community)
q = add_topic_stat_columns(q)
return get_topics_from_query(q)
2022-09-03 10:50:14 +00:00
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-28 20:29:02 +00:00
q = select(Topic)
q = add_topic_stat_columns(q)
q = q.where(ShoutAuthor.user == author)
return get_topics_from_query(q)
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-28 20:29:02 +00:00
q = select(Topic).where(Topic.slug == slug)
q = add_topic_stat_columns(q)
topics = get_topics_from_query(q)
return topics[0]
2022-11-10 06:51:40 +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-11-28 20:29:02 +00:00
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-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()
2022-11-28 20:29:02 +00:00
2022-09-18 14:29:21 +00:00
return {"topic": topic}
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()
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 = (
2022-11-25 23:35:26 +00:00
session.query(TopicFollower).filter(
and_(
TopicFollower.follower == user.slug,
TopicFollower.topic == slug
)
).first()
2022-09-03 10:50:14 +00:00
)
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-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-11-28 20:29:02 +00:00
q = select(Topic)
q = add_topic_stat_columns(q)
q = q.join(Shout, ShoutTopic.shout == Shout.slug).group_by(Topic.id).having(sa.func.count(Shout.id) > 2)
q = q.order_by(sa.func.random()).limit(amount)
return get_topics_from_query(q)