core/resolvers/topic.py

214 lines
6.2 KiB
Python
Raw Normal View History

2024-01-25 19:41:27 +00:00
import logging
2023-12-17 20:30:20 +00:00
from sqlalchemy import and_, distinct, func, select
2022-12-01 17:32:09 +00:00
from sqlalchemy.orm import aliased
2023-12-17 20:30:20 +00:00
from orm.author import Author
from orm.shout import ShoutAuthor, ShoutTopic
from orm.topic import Topic, TopicFollower
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-01-22 18:20:17 +00:00
from services.viewed import ViewedStorage
2023-10-23 14:47:11 +00:00
2024-01-25 19:41:27 +00:00
logger = logging.getLogger('\t[resolvers.topic]\t')
logger.setLevel(logging.DEBUG)
2023-10-23 14:47:11 +00:00
async def followed_topics(follower_id):
q = select(Author)
q = add_topic_stat_columns(q)
2023-11-22 16:38:39 +00:00
q = q.join(TopicFollower, TopicFollower.author == Author.id).where(TopicFollower.follower == follower_id)
2024-01-18 12:12:40 +00:00
# Pass the query to the get_topics_from_query function and return the results
2024-01-22 18:20:17 +00:00
return await get_topics_from_query(q)
2022-09-19 13:50:43 +00:00
2024-01-25 19:41:27 +00:00
2022-11-28 20:29:02 +00:00
def add_topic_stat_columns(q):
2022-12-01 17:32:09 +00:00
aliased_shout_author = aliased(ShoutAuthor)
aliased_topic_follower = aliased(TopicFollower)
2023-10-05 18:46:18 +00:00
q = (
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
2024-01-25 19:41:27 +00:00
.add_columns(func.count(distinct(ShoutTopic.shout)).label('shouts_stat'))
2023-10-05 18:46:18 +00:00
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
2024-01-25 19:41:27 +00:00
.add_columns(func.count(distinct(aliased_shout_author.author)).label('authors_stat'))
2023-10-05 18:46:18 +00:00
.outerjoin(aliased_topic_follower)
2024-01-25 19:41:27 +00:00
.add_columns(func.count(distinct(aliased_topic_follower.follower)).label('followers_stat'))
2022-11-28 20:29:02 +00:00
)
q = q.group_by(Topic.id)
return q
2024-01-22 18:20:17 +00:00
async def get_topics_from_query(q):
2022-11-28 20:29:02 +00:00
topics = []
with local_session() as session:
2023-11-29 18:11:05 +00:00
for [topic, shouts_stat, authors_stat, followers_stat] in session.execute(q):
topic.stat = {
2024-01-25 19:41:27 +00:00
'shouts': shouts_stat,
'authors': authors_stat,
'followers': followers_stat,
'viewed': await ViewedStorage.get_topic(topic.slug),
2023-11-29 18:11:05 +00:00
}
2022-11-28 20:29:02 +00:00
topics.append(topic)
return topics
2024-01-25 19:41:27 +00:00
@query.field('get_topics_all')
2023-11-30 10:30:50 +00:00
async def get_topics_all(_, _info):
2022-11-28 20:29:02 +00:00
q = select(Topic)
q = add_topic_stat_columns(q)
2024-01-22 18:20:17 +00:00
return await get_topics_from_query(q)
2022-11-28 20:29:02 +00:00
2024-01-22 18:20:17 +00:00
async def topics_followed_by(author_id):
2024-01-13 08:49:12 +00:00
q = select(Topic, TopicFollower)
2022-11-28 20:29:02 +00:00
q = add_topic_stat_columns(q)
2023-11-30 10:30:50 +00:00
q = q.join(TopicFollower).where(TopicFollower.follower == author_id)
2022-11-28 20:29:02 +00:00
2024-01-22 18:20:17 +00:00
return await get_topics_from_query(q)
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
2024-01-25 19:41:27 +00:00
@query.field('get_topics_by_community')
2023-11-28 07:53:48 +00:00
async def get_topics_by_community(_, _info, community_id: int):
q = select(Topic).where(Topic.community == community_id)
2022-11-28 20:29:02 +00:00
q = add_topic_stat_columns(q)
2024-01-22 18:20:17 +00:00
return await get_topics_from_query(q)
2022-09-03 10:50:14 +00:00
2024-01-25 19:41:27 +00:00
@query.field('get_topics_by_author')
async def get_topics_by_author(_, _info, author_id=None, slug='', user=''):
2022-11-28 20:29:02 +00:00
q = select(Topic)
q = add_topic_stat_columns(q)
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-01-22 18:20:17 +00:00
return await get_topics_from_query(q)
2022-09-03 10:50:14 +00:00
2021-10-28 10:42:34 +00:00
2024-01-25 19:41:27 +00:00
@query.field('get_topic')
2022-11-10 06:51:40 +00:00
async def get_topic(_, _info, slug):
2022-11-28 20:29:02 +00:00
q = select(Topic).where(Topic.slug == slug)
q = add_topic_stat_columns(q)
2024-01-22 18:20:17 +00:00
topics = await get_topics_from_query(q)
2022-11-28 20:29:02 +00:00
return topics[0]
2022-11-10 06:51:40 +00:00
2024-01-25 19:41:27 +00:00
@mutation.field('create_topic')
@login_required
2022-09-07 16:19:13 +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-03 14:31:00 +00:00
return {'topic': new_topic}
return {'error': 'cannot create topic'}
2024-01-25 19:41:27 +00:00
@mutation.field('update_topic')
@login_required
2022-09-07 16:19:13 +00:00
async def update_topic(_, _info, inp):
2024-01-25 19:41:27 +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-01-25 19:41:27 +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-01-25 19:41:27 +00:00
return {'topic': topic}
2024-02-03 17:08:22 +00:00
return {'error': 'cannot update'}
2024-01-25 19:41:27 +00:00
@mutation.field('delete_topic')
2023-11-28 07:53:48 +00:00
@login_required
async def delete_topic(_, info, slug: str):
2024-01-25 19:41:27 +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-01-25 19:41:27 +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-01-25 19:41:27 +00:00
return {'error': 'access denied'}
2023-11-28 07:53:48 +00:00
session.delete(t)
session.commit()
return {}
2024-02-03 14:31:00 +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-01-29 10:02:14 +00:00
except Exception as _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-01-25 19:41:27 +00:00
@query.field('get_topics_random')
2023-11-28 07:53:48 +00:00
async def get_topics_random(_, info, amount=12):
2022-11-28 20:29:02 +00:00
q = select(Topic)
2023-02-16 10:30:35 +00:00
q = q.join(ShoutTopic)
q = q.group_by(Topic.id)
2022-12-02 12:42:36 +00:00
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
2023-12-22 18:08:37 +00: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 18:22:23 +00:00
2023-12-22 18:08:37 +00:00
with local_session() as session:
2023-12-27 21:30:18 +00:00
r = session.execute(q).first()
if r:
[topic] = r
return topic