core/resolvers/topic.py

155 lines
4.9 KiB
Python
Raw Normal View History

2024-05-20 23:01:18 +00:00
from sqlalchemy import and_, distinct, func, join, select
2022-12-01 17:32:09 +00:00
2023-12-17 20:30:20 +00:00
from orm.author import Author
2024-05-20 23:01:18 +00:00
from orm.shout import Shout, ShoutAuthor, 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
2024-05-20 22:40:57 +00:00
from services.cache import get_cached_topic_authors, get_cached_topic_followers
2023-10-05 18:46:18 +00:00
from services.db import local_session
2024-05-20 22:40:57 +00:00
from services.logger import root_logger as logger
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-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-04-17 15:32:23 +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)
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-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-04-17 15:32:23 +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-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-04-17 15:32:23 +00:00
@query.field("get_topic")
2024-03-12 14:26:52 +00:00
def get_topic(_, _info, slug: str):
2024-05-18 11:15:05 +00:00
topic_query = select(Topic).filter(Topic.slug == slug)
result = get_with_stat(topic_query)
2024-04-09 19:35:11 +00:00
for topic in result:
2024-04-09 16:38:02 +00:00
return topic
2022-11-10 06:51:40 +00:00
2024-04-17 15:32:23 +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-04-17 15:32:23 +00:00
return {"topic": new_topic}
2024-04-17 15:32:23 +00:00
@mutation.field("update_topic")
@login_required
2024-02-26 09:14:08 +00:00
async def update_topic(_, _info, inp):
2024-04-17 15:32:23 +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-04-17 15:32:23 +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-04-17 15:32:23 +00:00
return {"topic": topic}
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-04-17 15:32:23 +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
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}")
topic_query = select(Topic.id).filter(Topic.slug == slug).first()
topic_id_result = local_session().execute(topic_query)
topic_id = topic_id_result[0] if topic_id_result else None
followers = await get_cached_topic_followers(topic_id)
return followers
@query.field("get_topic_authors")
async def get_topic_authors(_, _info, slug: str):
logger.debug(f"getting authors for @{slug}")
topic_query = select(Topic.id).filter(Topic.slug == slug).first()
topic_id_result = local_session().execute(topic_query)
topic_id = topic_id_result[0] if topic_id_result else None
2024-05-20 23:01:18 +00:00
topic_authors_query = (
select(ShoutAuthor.author)
.select_from(join(ShoutTopic, Shout, ShoutTopic.shout == Shout.id))
.join(ShoutAuthor, ShoutAuthor.shout == Shout.id)
.filter(
and_(
ShoutTopic.topic == topic_id,
Shout.published_at.is_not(None),
Shout.deleted_at.is_(None),
)
)
)
authors = await get_cached_topic_authors(topic_id, topic_authors_query)
2024-05-20 22:40:57 +00:00
return authors