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