core/resolvers/topic.py

136 lines
4.7 KiB
Python
Raw Normal View History

2024-11-01 08:09:16 +00:00
from sqlalchemy import select
2022-12-01 17:32:09 +00:00
2024-08-12 08:00:01 +00:00
from cache.cache import (
get_cached_topic_authors,
get_cached_topic_by_slug,
get_cached_topic_followers,
)
from cache.memorycache import cache_region
2023-12-17 20:30:20 +00:00
from orm.author import Author
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-04-08 07:38:58 +00:00
from services.schema import mutation, query
2024-08-12 08:00:01 +00:00
from utils.logger import root_logger as logger
2024-01-25 19:41:27 +00:00
2024-08-07 14:45:22 +00:00
# Запрос на получение всех тем
2024-04-17 15:32:23 +00:00
@query.field("get_topics_all")
2024-03-12 14:26:52 +00:00
def get_topics_all(_, _info):
2024-08-07 14:45:22 +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():
2024-05-18 11:15:05 +00:00
topics_query = select(Topic)
2024-08-07 14:45:22 +00:00
return get_with_stat(topics_query) # Получение тем с учетом статистики
2024-03-12 14:26:52 +00:00
return _get_topics_all()
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
2024-08-07 14:45:22 +00:00
# Запрос на получение тем по сообществу
2024-04-17 15:32:23 +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-08-07 14:45:22 +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():
2024-05-18 11:15:05 +00:00
topics_by_community_query = select(Topic).where(Topic.community == community_id)
return get_with_stat(topics_by_community_query)
2024-03-12 14:26:52 +00:00
return _get_topics_by_community()
2022-09-03 10:50:14 +00:00
2024-08-07 14:45:22 +00:00
# Запрос на получение тем по автору
2024-04-17 15:32:23 +00:00
@query.field("get_topics_by_author")
async def get_topics_by_author(_, _info, author_id=0, slug="", user=""):
2024-05-18 11:15:05 +00:00
topics_by_author_query = select(Topic)
2023-11-28 07:53:48 +00:00
if author_id:
2024-05-30 04:12:00 +00:00
topics_by_author_query = topics_by_author_query.join(Author).where(Author.id == author_id)
2023-11-28 07:53:48 +00:00
elif slug:
2024-05-30 04:12:00 +00:00
topics_by_author_query = topics_by_author_query.join(Author).where(Author.slug == slug)
2023-11-28 07:53:48 +00:00
elif user:
2024-05-30 04:12:00 +00:00
topics_by_author_query = topics_by_author_query.join(Author).where(Author.user == user)
2022-11-28 20:29:02 +00:00
2024-05-18 11:15:05 +00:00
return get_with_stat(topics_by_author_query)
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
2024-08-07 14:45:22 +00:00
# Запрос на получение одной темы по её slug
2024-04-17 15:32:23 +00:00
@query.field("get_topic")
2024-06-05 14:45:55 +00:00
async def get_topic(_, _info, slug: str):
2024-06-06 06:23:45 +00:00
topic = await get_cached_topic_by_slug(slug, get_with_stat)
2024-06-05 14:45:55 +00:00
if topic:
2024-04-09 16:38:02 +00:00
return topic
2022-11-10 06:51:40 +00:00
2024-08-07 14:45:22 +00:00
# Мутация для создания новой темы
2024-04-17 15:32:23 +00:00
@mutation.field("create_topic")
@login_required
2025-02-11 09:00:35 +00:00
async def create_topic(_, _info, topic_input):
2022-09-22 10:31:44 +00:00
with local_session() as session:
2024-08-07 14:45:22 +00:00
# TODO: проверить права пользователя на создание темы для конкретного сообщества
# и разрешение на создание
2025-02-11 09:00:35 +00:00
new_topic = Topic(**topic_input)
2022-09-22 10:31:44 +00:00
session.add(new_topic)
session.commit()
2022-11-28 20:29:02 +00:00
2024-04-17 15:32:23 +00:00
return {"topic": new_topic}
2024-08-07 14:45:22 +00:00
# Мутация для обновления темы
2024-04-17 15:32:23 +00:00
@mutation.field("update_topic")
@login_required
2025-02-11 09:00:35 +00:00
async def update_topic(_, _info, topic_input):
slug = topic_input["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-04-17 15:32:23 +00:00
return {"error": "topic not found"}
2022-09-18 14:29:21 +00:00
else:
2025-02-11 09:00:35 +00:00
Topic.update(topic, topic_input)
2023-11-22 16:38:39 +00:00
session.add(topic)
2022-09-18 14:29:21 +00:00
session.commit()
2022-11-28 20:29:02 +00:00
2024-04-17 15:32:23 +00:00
return {"topic": topic}
2024-08-07 14:45:22 +00:00
# Мутация для удаления темы
2024-04-17 15:32:23 +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-04-17 15:32:23 +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-04-17 15:32:23 +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-04-17 15:32:23 +00:00
return {"error": "access denied"}
2023-11-28 07:53:48 +00:00
session.delete(t)
session.commit()
return {}
2024-04-17 15:32:23 +00:00
return {"error": "access denied"}
2023-11-28 07:53:48 +00:00
2024-08-07 14:45:22 +00:00
# Запрос на получение подписчиков темы
2024-05-20 22:40:57 +00:00
@query.field("get_topic_followers")
async def get_topic_followers(_, _info, slug: str):
logger.debug(f"getting followers for @{slug}")
2024-06-06 06:23:45 +00:00
topic = await get_cached_topic_by_slug(slug, get_with_stat)
2024-06-05 14:45:55 +00:00
topic_id = topic.id if isinstance(topic, Topic) else topic.get("id")
2024-05-20 22:40:57 +00:00
followers = await get_cached_topic_followers(topic_id)
return followers
2024-08-07 14:45:22 +00:00
# Запрос на получение авторов темы
2024-05-20 22:40:57 +00:00
@query.field("get_topic_authors")
async def get_topic_authors(_, _info, slug: str):
logger.debug(f"getting authors for @{slug}")
2024-06-06 06:23:45 +00:00
topic = await get_cached_topic_by_slug(slug, get_with_stat)
2024-06-05 14:45:55 +00:00
topic_id = topic.id if isinstance(topic, Topic) else topic.get("id")
authors = await get_cached_topic_authors(topic_id)
2024-05-20 22:40:57 +00:00
return authors