Files
core/resolvers/topic.py

151 lines
4.1 KiB
Python
Raw Normal View History

2023-12-17 23:30:20 +03:00
from sqlalchemy import and_, distinct, func, select
2022-12-01 18:32:09 +01:00
2023-12-17 23:30:20 +03:00
from orm.author import Author
2024-02-21 20:12:47 +03:00
from orm.shout import ShoutTopic
2023-12-17 23:30:20 +03:00
from orm.topic import Topic, TopicFollower
2024-02-25 11:27:08 +03:00
from resolvers.stat import get_with_stat
2023-10-23 17:47:11 +03:00
from services.auth import login_required
2023-10-05 21:46:18 +03:00
from services.db import local_session
2023-11-22 19:38:39 +03:00
from services.schema import mutation, query
2024-02-20 19:19:46 +03:00
from services.logger import root_logger as logger
2024-01-25 22:41:27 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_topics_all')
2024-02-23 02:08:43 +03:00
def get_topics_all(_, _info):
2024-02-25 11:27:08 +03:00
return get_with_stat(select(Topic))
2022-09-03 13:50:14 +03:00
2021-10-28 13:42:34 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_topics_by_community')
2024-02-23 02:49:34 +03:00
def get_topics_by_community(_, _info, community_id: int):
2023-11-28 10:53:48 +03:00
q = select(Topic).where(Topic.community == community_id)
2024-02-25 11:27:08 +03:00
return get_with_stat(q)
2022-09-03 13:50:14 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_topics_by_author')
2024-02-24 13:22:35 +03:00
def get_topics_by_author(_, _info, author_id=0, slug='', user=''):
2022-11-28 21:29:02 +01:00
q = select(Topic)
2023-11-28 10:53:48 +03:00
if author_id:
q = q.join(Author).where(Author.id == author_id)
elif slug:
q = q.join(Author).where(Author.slug == slug)
elif user:
q = q.join(Author).where(Author.user == user)
2022-11-28 21:29:02 +01:00
2024-02-25 11:27:08 +03:00
return get_with_stat(q)
2022-09-03 13:50:14 +03:00
2021-10-28 13:42:34 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_topic')
2024-02-24 13:22:35 +03:00
def get_topic(_, _info, slug: str):
2024-02-23 02:08:43 +03:00
q = select(Topic).filter(Topic.slug == slug)
2024-02-25 11:27:08 +03:00
topics = get_with_stat(q)
2024-02-17 21:44:22 +03:00
if topics:
return topics[0]
2022-11-10 09:51:40 +03:00
2024-02-21 19:14:58 +03:00
@mutation.field('create_topic')
@login_required
2024-02-23 02:49:34 +03:00
def create_topic(_, _info, inp):
2022-09-22 13:31:44 +03:00
with local_session() as session:
# TODO: check user permissions to create topic for exact community
2024-02-03 17:31:00 +03:00
# and actor is permitted to craete it
2023-11-22 19:38:39 +03:00
new_topic = Topic(**inp)
2022-09-22 13:31:44 +03:00
session.add(new_topic)
session.commit()
2022-11-28 21:29:02 +01:00
2024-02-21 19:14:58 +03:00
return {'topic': new_topic}
2024-02-21 19:14:58 +03:00
@mutation.field('update_topic')
@login_required
2024-02-23 02:49:34 +03:00
def update_topic(_, _info, inp):
2024-02-21 19:14:58 +03:00
slug = inp['slug']
2022-09-18 17:29:21 +03:00
with local_session() as session:
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
2024-02-21 19:14:58 +03:00
return {'error': 'topic not found'}
2022-09-18 17:29:21 +03:00
else:
2023-11-22 19:38:39 +03:00
Topic.update(topic, inp)
session.add(topic)
2022-09-18 17:29:21 +03:00
session.commit()
2022-11-28 21:29:02 +01:00
2024-02-21 19:14:58 +03:00
return {'topic': topic}
2024-02-21 19:14:58 +03:00
@mutation.field('delete_topic')
2023-11-28 10:53:48 +03:00
@login_required
2024-02-23 02:49:34 +03:00
def delete_topic(_, info, slug: str):
2024-02-21 19:14:58 +03:00
user_id = info.context['user_id']
2023-11-28 10:53:48 +03:00
with local_session() as session:
t: Topic = session.query(Topic).filter(Topic.slug == slug).first()
if not t:
2024-02-21 19:14:58 +03:00
return {'error': 'invalid topic slug'}
2023-11-28 10:53:48 +03:00
author = session.query(Author).filter(Author.user == user_id).first()
if author:
if t.created_by != author.id:
2024-02-21 19:14:58 +03:00
return {'error': 'access denied'}
2023-11-28 10:53:48 +03:00
session.delete(t)
session.commit()
return {}
2024-02-21 19:14:58 +03:00
return {'error': 'access denied'}
2023-11-28 10:53:48 +03:00
2023-10-23 17:47:11 +03:00
def topic_follow(follower_id, slug):
2023-02-20 20:38:20 +03:00
try:
with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one()
2023-11-22 19:38:39 +03:00
_following = TopicFollower(topic=topic.id, follower=follower_id)
2023-02-20 20:38:20 +03:00
return True
2024-01-29 13:02:14 +03:00
except Exception as _exc:
2023-02-20 20:38:20 +03:00
return False
2022-09-03 13:50:14 +03:00
2022-01-30 11:50:29 +03:00
2023-11-24 02:00:28 +03:00
def topic_unfollow(follower_id, slug):
2023-02-20 20:38:20 +03:00
try:
with local_session() as session:
sub = (
2023-10-05 21:46:18 +03:00
session.query(TopicFollower)
.join(Topic)
2023-11-24 02:00:28 +03:00
.filter(and_(TopicFollower.follower == follower_id, Topic.slug == slug))
2023-10-05 21:46:18 +03:00
.first()
2023-02-20 20:38:20 +03:00
)
if sub:
session.delete(sub)
session.commit()
return True
2024-01-25 22:41:27 +03:00
except Exception as ex:
logger.debug(ex)
2023-02-20 20:38:20 +03:00
return False
2022-09-05 15:05:20 +03:00
2024-02-21 19:14:58 +03:00
@query.field('get_topics_random')
2023-11-28 10:53:48 +03:00
async def get_topics_random(_, info, amount=12):
2022-11-28 21:29:02 +01:00
q = select(Topic)
2023-02-16 11:30:35 +01:00
q = q.join(ShoutTopic)
q = q.group_by(Topic.id)
2022-12-02 13:42:36 +01:00
q = q.having(func.count(distinct(ShoutTopic.shout)) > 2)
q = q.order_by(func.random()).limit(amount)
2022-11-28 21:29:02 +01:00
2023-02-16 11:30:35 +01:00
topics = []
with local_session() as session:
for [topic] in session.execute(q):
topics.append(topic)
return topics
2023-12-22 21:08:37 +03:00
def get_random_topic():
q = select(Topic)
q = q.join(ShoutTopic)
q = q.group_by(Topic.id)
q = q.having(func.count(distinct(ShoutTopic.shout)) > 10)
q = q.order_by(func.random()).limit(1)
2023-12-22 21:22:23 +03:00
2023-12-22 21:08:37 +03:00
with local_session() as session:
2023-12-28 00:30:18 +03:00
r = session.execute(q).first()
if r:
[topic] = r
return topic
2024-02-03 20:13:51 +03:00
return