revised
Some checks failed
Deploy to core / deploy (push) Has been cancelled

This commit is contained in:
Untone 2024-02-02 23:38:16 +03:00
parent 6d3bd13218
commit 8cc7e21338

View File

@ -1,4 +1,4 @@
import time # For Unix timestamps import time
from sqlalchemy import and_, select from sqlalchemy import and_, select
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
@ -27,16 +27,13 @@ async def get_shouts_drafts(_, info):
q = ( q = (
select(Shout) select(Shout)
.options( .options(
# joinedload(Shout.created_by, Author.id == Shout.created_by),
joinedload(Shout.authors), joinedload(Shout.authors),
joinedload(Shout.topics), joinedload(Shout.topics),
) )
.filter(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id)) .filter(and_(Shout.deleted_at.is_(None), Shout.created_by == author.id))
.group_by(Shout.id)
) )
q = q.group_by(Shout.id) shouts = [shout for [shout] in session.execute(q).unique()]
shouts = []
for [shout] in session.execute(q).unique():
shouts.append(shout)
return shouts return shouts
@ -46,7 +43,6 @@ async def create_shout(_, info, inp):
user_id = info.context['user_id'] user_id = info.context['user_id']
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
shout_dict = None
if author: if author:
current_time = int(time.time()) current_time = int(time.time())
slug = inp.get('slug') or f'draft-{current_time}' slug = inp.get('slug') or f'draft-{current_time}'
@ -85,14 +81,15 @@ async def create_shout(_, info, inp):
# notifier # notifier
await notify_shout(shout_dict, 'create') await notify_shout(shout_dict, 'create')
return {'shout': shout_dict}
return {'shout': shout_dict}
return {'error': 'cant create shout'}
@mutation.field('update_shout') @mutation.field('update_shout')
@login_required @login_required
async def update_shout( # noqa: C901 async def update_shout(_, info, shout_id, shout_input=None, publish=False):
_, info, shout_id, shout_input=None, publish=False
):
user_id = info.context['user_id'] user_id = info.context['user_id']
with local_session() as session: with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
@ -102,7 +99,6 @@ async def update_shout( # noqa: C901
shout = ( shout = (
session.query(Shout) session.query(Shout)
.options( .options(
# joinedload(Shout.created_by, Author.id == Shout.created_by),
joinedload(Shout.authors), joinedload(Shout.authors),
joinedload(Shout.topics), joinedload(Shout.topics),
) )
@ -116,43 +112,40 @@ async def update_shout( # noqa: C901
if shout_input is not None: if shout_input is not None:
topics_input = shout_input['topics'] topics_input = shout_input['topics']
del shout_input['topics'] del shout_input['topics']
new_topics_to_link = [] new_topics_to_link = [Topic(**new_topic) for new_topic in topics_input if new_topic['id'] < 0]
new_topics = [topic_input for topic_input in topics_input if topic_input['id'] < 0] if new_topics_to_link:
for new_topic in new_topics: session.add_all(new_topics_to_link)
del new_topic['id']
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() session.commit()
for new_topic_to_link in new_topics_to_link: for new_topic_to_link in new_topics_to_link:
created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id) created_unlinked_topic = ShoutTopic(shout=shout.id, topic=new_topic_to_link.id)
session.add(created_unlinked_topic) session.add(created_unlinked_topic)
existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0] existing_topics_input = [topic_input for topic_input in topics_input if topic_input.get('id', 0) > 0]
existing_topic_to_link_ids = [ existing_topic_to_link_ids = [
existing_topic_input['id'] existing_topic_input['id']
for existing_topic_input in existing_topics_input for existing_topic_input in existing_topics_input
if existing_topic_input['id'] not in [topic.id for topic in shout.topics] if existing_topic_input['id'] not in [topic.id for topic in shout.topics]
] ]
for existing_topic_to_link_id in existing_topic_to_link_ids: 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) created_unlinked_topic = ShoutTopic(shout=shout.id, topic=existing_topic_to_link_id)
session.add(created_unlinked_topic) session.add(created_unlinked_topic)
topic_to_unlink_ids = [ topic_to_unlink_ids = [
topic.id topic.id
for topic in shout.topics for topic in shout.topics
if topic.id not in [topic_input['id'] for topic_input in existing_topics_input] if topic.id not in [topic_input['id'] for topic_input in existing_topics_input]
] ]
shout_topics_to_remove = session.query(ShoutTopic).filter(
session.query(ShoutTopic).filter(
and_( and_(
ShoutTopic.shout == shout.id, ShoutTopic.shout == shout.id,
ShoutTopic.topic.in_(topic_to_unlink_ids), ShoutTopic.topic.in_(topic_to_unlink_ids),
) )
) ).delete(synchronize_session=False)
for shout_topic_to_remove in shout_topics_to_remove:
session.delete(shout_topic_to_remove)
# Replace datetime with Unix timestamp shout_input['updated_at'] = current_time
shout_input['updated_at'] = current_time # Set updated_at as Unix timestamp
shout_input['published_at'] = current_time if publish else None shout_input['published_at'] = current_time if publish else None
Shout.update(shout, shout_input) Shout.update(shout, shout_input)
session.add(shout) session.add(shout)
@ -161,11 +154,18 @@ async def update_shout( # noqa: C901
if 'main_topic' in shout_input: if 'main_topic' in shout_input:
old_main_topic = ( old_main_topic = (
session.query(ShoutTopic) session.query(ShoutTopic)
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.main == True)) .filter(
and_(
ShoutTopic.shout == shout.id,
ShoutTopic.main == True,
)
)
.first() .first()
) )
main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).first() main_topic = session.query(Topic).filter(Topic.slug == shout_input['main_topic']).first()
if isinstance(main_topic, Topic):
if main_topic:
new_main_topic = ( new_main_topic = (
session.query(ShoutTopic) session.query(ShoutTopic)
.filter( .filter(
@ -176,13 +176,11 @@ async def update_shout( # noqa: C901
) )
.first() .first()
) )
if (
isinstance(old_main_topic, ShoutTopic) if old_main_topic and new_main_topic and old_main_topic is not new_main_topic:
and isinstance(new_main_topic, ShoutTopic)
and old_main_topic is not new_main_topic
):
ShoutTopic.update(old_main_topic, {'main': False}) ShoutTopic.update(old_main_topic, {'main': False})
session.add(old_main_topic) session.add(old_main_topic)
ShoutTopic.update(new_main_topic, {'main': True}) ShoutTopic.update(new_main_topic, {'main': True})
session.add(new_main_topic) session.add(new_main_topic)
@ -208,10 +206,10 @@ async def delete_shout(_, info, shout_id):
shout = session.query(Shout).filter(Shout.id == shout_id).first() shout = session.query(Shout).filter(Shout.id == shout_id).first()
if not shout: if not shout:
return {'error': 'invalid shout id'} return {'error': 'invalid shout id'}
if isinstance(author, Author) and isinstance(shout, Shout): if author and shout:
# TODO: add editor role allowed here
if shout.created_by is not author.id and author.id not in shout.authors: if shout.created_by is not author.id and author.id not in shout.authors:
return {'error': 'access denied'} return {'error': 'access denied'}
for author_id in shout.authors: for author_id in shout.authors:
reactions_unfollow(author_id, shout_id) reactions_unfollow(author_id, shout_id)
@ -221,20 +219,27 @@ async def delete_shout(_, info, shout_id):
session.add(shout) session.add(shout)
session.commit() session.commit()
await notify_shout(shout_dict, 'delete') await notify_shout(shout_dict, 'delete')
return {} return {}
def handle_proposing(session, r, shout): def handle_proposing(session, r, shout):
if is_positive(r.kind): if is_positive(r.kind):
# Proposal accepting logic replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to, Reaction.shout == r.shout).first()
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to).first()
if replied_reaction and replied_reaction.kind is ReactionKind.PROPOSE.value and replied_reaction.quote: if replied_reaction and replied_reaction.kind is ReactionKind.PROPOSE.value and replied_reaction.quote:
# patch all the proposals' quotes # patch all the proposals' quotes
proposals = ( proposals = (
session.query(Reaction) session.query(Reaction)
.filter(and_(Reaction.shout == r.shout, Reaction.kind == ReactionKind.PROPOSE.value)) .filter(
and_(
Reaction.shout == r.shout,
Reaction.kind == ReactionKind.PROPOSE.value,
)
)
.all() .all()
) )
for proposal in proposals: for proposal in proposals:
if proposal.quote: if proposal.quote:
proposal_diff = get_diff(shout.body, proposal.quote) proposal_diff = get_diff(shout.body, proposal.quote)