core/resolvers/topic.py

121 lines
3.3 KiB
Python
Raw Normal View History

2024-03-12 07:35:33 +00:00
from sqlalchemy import 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
2024-03-12 07:35:33 +00:00
from orm.topic import Topic
2024-04-09 08:30:20 +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
2024-03-12 14:26:52 +00:00
from services.memorycache import cache_region
2024-04-08 07:38:58 +00:00
from services.schema import mutation, query
2024-01-25 19:41:27 +00:00
2024-02-21 16:14:58 +00:00
@query.field('get_topics_all')
2024-03-12 14:26:52 +00:00
def get_topics_all(_, _info):
2024-03-28 12:56:32 +00:00
cache_key = 'get_topics_all'
2024-03-12 14:26:52 +00:00
@cache_region.cache_on_arguments(cache_key)
def _get_topics_all():
return get_with_stat(select(Topic))
return _get_topics_all()
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-03-12 14:26:52 +00:00
def get_topics_by_community(_, _info, community_id: int):
2024-03-28 12:56:32 +00:00
cache_key = f'get_topics_by_community_{community_id}'
2024-03-12 14:26:52 +00:00
@cache_region.cache_on_arguments(cache_key)
def _get_topics_by_community():
q = select(Topic).where(Topic.community == community_id)
return get_with_stat(q)
return _get_topics_by_community()
2022-09-03 10:50:14 +00:00
2024-02-21 16:14:58 +00:00
@query.field('get_topics_by_author')
2024-03-12 12:50:57 +00:00
async 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-04-09 08:30:20 +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-03-12 14:26:52 +00:00
def get_topic(_, _info, slug: str):
2024-02-22 23:08:43 +00:00
q = select(Topic).filter(Topic.slug == slug)
2024-04-09 16:38:02 +00:00
result = get_with_stat(q)
if result:
[topic] = result
return topic
2022-11-10 06:51:40 +00:00
2024-02-21 16:14:58 +00:00
@mutation.field('create_topic')
@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}
2024-02-21 16:14:58 +00:00
@mutation.field('update_topic')
@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}
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
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