core/resolvers/editor.py

220 lines
9.1 KiB
Python
Raw Normal View History

2023-11-03 10:10:22 +00:00
import time # For Unix timestamps
2023-12-17 20:30:20 +00:00
2023-10-23 14:47:11 +00:00
from sqlalchemy import and_, select
2023-05-11 11:03:14 +00:00
from sqlalchemy.orm import joinedload
2023-11-23 23:00:28 +00:00
from orm.author import Author
2023-11-03 10:10:22 +00:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic, ShoutVisibility
2023-05-14 17:02:26 +00:00
from orm.topic import Topic
2023-11-22 18:23:15 +00:00
from resolvers.reaction import reactions_follow, reactions_unfollow
2023-12-17 20:30:20 +00:00
from services.auth import login_required
from services.db import local_session
2023-10-25 18:33:53 +00:00
from services.notify import notify_shout
2023-12-17 20:30:20 +00:00
from services.schema import mutation, query
2024-01-29 01:41:46 +00:00
from services.search import search
2022-07-10 18:52:57 +00:00
2023-11-22 16:38:39 +00:00
2024-01-25 19:41:27 +00:00
@query.field('get_shouts_drafts')
2023-11-23 23:00:28 +00:00
@login_required
2023-11-28 07:53:48 +00:00
async def get_shouts_drafts(_, info):
2024-01-25 19:41:27 +00:00
user_id = info.context['user_id']
2023-10-23 14:47:11 +00:00
with local_session() as session:
2023-11-23 23:00:28 +00:00
author = session.query(Author).filter(Author.user == user_id).first()
2023-11-27 16:15:34 +00:00
if author:
q = (
select(Shout)
.options(
2023-11-30 11:04:55 +00:00
# joinedload(Shout.created_by, Author.id == Shout.created_by),
2023-11-27 16:15:34 +00:00
joinedload(Shout.authors),
joinedload(Shout.topics),
)
2023-11-30 11:04:55 +00:00
.filter(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id))
2023-11-23 23:00:28 +00:00
)
2023-11-27 16:15:34 +00:00
q = q.group_by(Shout.id)
shouts = []
for [shout] in session.execute(q).unique():
shouts.append(shout)
return shouts
2023-10-23 14:47:11 +00:00
2023-11-22 16:38:39 +00:00
2024-01-25 19:41:27 +00:00
@mutation.field('create_shout')
2022-06-19 11:11:14 +00:00
@login_required
async def create_shout(_, info, inp):
2024-01-25 19:41:27 +00:00
user_id = info.context['user_id']
2022-09-22 10:31:44 +00:00
with local_session() as session:
2023-11-23 23:00:28 +00:00
author = session.query(Author).filter(Author.user == user_id).first()
2023-11-29 08:00:00 +00:00
shout_dict = None
2023-11-27 16:15:34 +00:00
if author:
current_time = int(time.time())
2024-01-25 19:41:27 +00:00
slug = inp.get('slug') or f'draft-{current_time}'
2024-01-22 16:57:48 +00:00
shout_dict = {
2024-01-25 19:41:27 +00:00
'title': inp.get('title', ''),
'subtitle': inp.get('subtitle', ''),
'lead': inp.get('lead', ''),
'description': inp.get('description', ''),
'body': inp.get('body', ''),
'layout': inp.get('layout', 'article'),
'created_by': author.id,
'authors': [],
'slug': slug,
'topics': inp.get('topics', []),
'visibility': ShoutVisibility.AUTHORS.value,
'created_at': current_time, # Set created_at as Unix timestamp
2024-01-22 16:57:48 +00:00
}
new_shout = Shout(**shout_dict)
2023-11-27 16:15:34 +00:00
session.add(new_shout)
2023-05-03 15:47:09 +00:00
session.commit()
2023-11-27 16:15:34 +00:00
2024-01-22 16:57:48 +00:00
# NOTE: shout made by one author
shout = session.query(Shout).where(Shout.slug == slug).first()
if shout:
shout_dict = shout.dict()
sa = ShoutAuthor(shout=shout.id, author=author.id)
session.add(sa)
2024-01-25 19:41:27 +00:00
topics = session.query(Topic).filter(Topic.slug.in_(inp.get('topics', []))).all()
2024-01-22 16:57:48 +00:00
for topic in topics:
t = ShoutTopic(topic=topic.id, shout=shout.id)
session.add(t)
reactions_follow(author.id, shout.id, True)
2024-01-29 00:27:30 +00:00
# notifier
2024-01-25 19:41:27 +00:00
await notify_shout(shout_dict, 'create')
return {'shout': shout_dict}
2022-09-03 10:50:14 +00:00
2023-11-22 16:38:39 +00:00
2024-01-25 19:41:27 +00:00
@mutation.field('update_shout')
2022-06-19 11:11:14 +00:00
@login_required
2024-01-25 19:41:27 +00:00
async def update_shout( # noqa: C901
_, info, shout_id, shout_input=None, publish=False
):
user_id = info.context['user_id']
2022-09-22 10:31:44 +00:00
with local_session() as session:
2023-11-23 23:00:28 +00:00
author = session.query(Author).filter(Author.user == user_id).first()
2023-11-29 08:00:00 +00:00
shout_dict = None
current_time = int(time.time())
2023-11-27 16:15:34 +00:00
if author:
shout = (
session.query(Shout)
.options(
2023-11-30 11:04:55 +00:00
# joinedload(Shout.created_by, Author.id == Shout.created_by),
2023-11-27 16:15:34 +00:00
joinedload(Shout.authors),
joinedload(Shout.topics),
2023-05-11 11:03:14 +00:00
)
2023-11-27 16:15:34 +00:00
.filter(Shout.id == shout_id)
.first()
2023-05-11 11:03:14 +00:00
)
2023-11-27 16:15:34 +00:00
if not shout:
2024-01-25 19:41:27 +00:00
return {'error': 'shout not found'}
2024-01-23 13:04:38 +00:00
if shout.created_by is not author.id and author.id not in shout.authors:
2024-01-25 19:41:27 +00:00
return {'error': 'access denied'}
2023-11-27 16:15:34 +00:00
if shout_input is not None:
2024-01-25 19:41:27 +00:00
topics_input = shout_input['topics']
del shout_input['topics']
2023-11-27 16:15:34 +00:00
new_topics_to_link = []
2024-01-25 19:41:27 +00:00
new_topics = [topic_input for topic_input in topics_input if topic_input['id'] < 0]
2023-11-27 16:15:34 +00:00
for new_topic in new_topics:
2024-01-25 19:41:27 +00:00
del new_topic['id']
2023-11-27 16:15:34 +00:00
created_new_topic = Topic(**new_topic)
session.add(created_new_topic)
new_topics_to_link.append(created_new_topic)
if len(new_topics) > 0:
session.commit()
for new_topic_to_link in new_topics_to_link:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
session.add(created_unlinked_topic)
2024-01-25 19:41:27 +00:00
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0]
2023-11-27 16:15:34 +00:00
existing_topic_to_link_ids = [
2024-01-25 19:41:27 +00:00
existing_topic_input['id']
2023-11-27 16:15:34 +00:00
for existing_topic_input in existing_topics_input
2024-01-25 19:41:27 +00:00
if existing_topic_input['id'] not in [topic.id for topic in shout.topics]
2023-11-27 16:15:34 +00:00
]
for existing_topic_to_link_id in existing_topic_to_link_ids:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
session.add(created_unlinked_topic)
topic_to_unlink_ids = [
topic.id
for topic in shout.topics
2024-01-25 19:41:27 +00:00
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
2023-11-27 16:15:34 +00:00
]
shout_topics_to_remove = session.query(ShoutTopic).filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic.in_(topic_to_unlink_ids),
)
)
for shout_topic_to_remove in shout_topics_to_remove:
session.delete(shout_topic_to_remove)
2023-11-30 08:40:27 +00:00
2023-11-27 16:15:34 +00:00
# Replace datetime with Unix timestamp
2024-01-25 19:41:27 +00:00
shout_input['updated_at'] = current_time # Set updated_at as Unix timestamp
2023-11-27 16:15:34 +00:00
Shout.update(shout, shout_input)
session.add(shout)
2023-12-09 16:45:02 +00:00
# main topic
2024-01-25 19:41:27 +00:00
if 'main_topic' in shout_input:
2023-12-17 04:59:16 +00:00
old_main_topic = (
session.query(ShoutTopic)
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main == True))
.first()
)
2024-01-25 19:41:27 +00:00
main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).first()
2024-01-23 13:04:38 +00:00
if isinstance(main_topic, Topic):
new_main_topic = (
session.query(ShoutTopic)
2024-01-25 19:41:27 +00:00
.filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.topic == main_topic.id,
)
)
2024-01-23 13:04:38 +00:00
.first()
)
2024-01-25 19:41:27 +00:00
if (
isinstance(old_main_topic, ShoutTopic)
and isinstance(new_main_topic, ShoutTopic)
and old_main_topic is not new_main_topic
):
ShoutTopic.update(old_main_topic, {'main': False})
2024-01-23 13:04:38 +00:00
session.add(old_main_topic)
2024-01-25 19:41:27 +00:00
ShoutTopic.update(new_main_topic, {'main': True})
2024-01-23 13:04:38 +00:00
session.add(new_main_topic)
2023-12-09 16:45:02 +00:00
2023-11-29 08:00:00 +00:00
shout_dict = shout.dict()
session.commit()
2024-01-25 19:41:27 +00:00
2023-11-29 08:00:00 +00:00
if not publish:
2024-01-25 19:41:27 +00:00
await notify_shout(shout_dict, 'update')
2024-01-29 01:41:46 +00:00
if shout.visibility is ShoutVisibility.COMMUNITY.value or shout.visibility is ShoutVisibility.PUBLIC.value:
# search service indexing
search.index_post(shout)
2024-01-29 00:27:30 +00:00
2024-01-25 19:41:27 +00:00
return {'shout': shout_dict}
2022-09-03 10:50:14 +00:00
2023-11-22 16:38:39 +00:00
2024-01-25 19:41:27 +00:00
@mutation.field('delete_shout')
2022-06-19 11:11:14 +00:00
@login_required
2023-05-08 12:28:17 +00:00
async def delete_shout(_, info, shout_id):
2024-01-25 19:41:27 +00:00
user_id = info.context['user_id']
2022-09-03 10:50:14 +00:00
with local_session() as session:
2023-11-23 23:00:28 +00:00
author = session.query(Author).filter(Author.id == user_id).first()
2023-05-08 12:28:17 +00:00
shout = session.query(Shout).filter(Shout.id == shout_id).first()
2022-09-03 10:50:14 +00:00
if not shout:
2024-01-25 19:41:27 +00:00
return {'error': 'invalid shout id'}
2024-01-23 13:04:38 +00:00
if isinstance(author, Author) and isinstance(shout, Shout):
# TODO: add editor role allowed here
if shout.created_by is not author.id and author.id not in shout.authors:
2024-01-25 19:41:27 +00:00
return {'error': 'access denied'}
2023-11-29 08:00:00 +00:00
for author_id in shout.authors:
reactions_unfollow(author_id, shout_id)
2024-01-23 13:04:38 +00:00
2023-11-29 08:00:00 +00:00
shout_dict = shout.dict()
2024-01-25 19:41:27 +00:00
shout_dict['deleted_at'] = int(time.time())
2023-11-29 08:00:00 +00:00
Shout.update(shout, shout_dict)
session.add(shout)
session.commit()
2024-01-25 19:41:27 +00:00
await notify_shout(shout_dict, 'delete')
2022-09-03 10:50:14 +00:00
return {}