delete-reaction-fix
Some checks failed
Deploy to core / deploy (push) Failing after 2m11s

This commit is contained in:
Untone 2024-02-07 16:41:17 +03:00
parent 7746d1992f
commit 85931d04ba

View File

@ -157,46 +157,36 @@ async def create_reaction(_, info, reaction):
kind = ReactionKind.COMMENT.value kind = ReactionKind.COMMENT.value
if not kind: if not kind:
return {'error': 'cannot create reaction with this kind'} return {'error': 'cannot create reaction without a kind'}
if kind in ['LIKE', 'DISLIKE', 'AGREE', 'DISAGREE']: if kind in RATING_REACTIONS:
same_reaction = (
opposite_kind = ( ReactionKind.DISLIKE.value
if is_positive(kind)
else ReactionKind.LIKE.value
)
rating_reactions = (
session.query(Reaction) session.query(Reaction)
.filter( .filter(
and_( and_(
Reaction.shout == shout_id, Reaction.shout == shout_id,
Reaction.created_by == author.id, Reaction.created_by == author.id,
Reaction.kind == kind, Reaction.kind.in_(RATING_REACTIONS),
Reaction.reply_to == reaction.get('reply_to'), Reaction.reply_to == int(reaction.get('reply_to')),
) )
) )
.first() .all()
) )
same_rating = filter(lambda r: r.created_by == author.id and r.kind == opposite_kind, rating_reactions)
if same_reaction is not None: opposite_rating = filter(lambda r: r.created_by == author.id and r.kind == opposite_kind, rating_reactions)
return {'error': "You can't like or dislike same thing twice"} if same_rating:
return {'error': "You can't rate the same thing twice"}
opposite_reaction_kind = ( elif opposite_rating:
ReactionKind.DISLIKE.value
if reaction['kind'] == ReactionKind.LIKE.value
else ReactionKind.LIKE.value
)
opposite_reaction = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == reaction['shout'],
Reaction.created_by == author.id,
Reaction.kind == opposite_reaction_kind,
Reaction.reply_to == reaction.get('reply_to'),
)
)
.first()
)
if opposite_reaction is not None:
return {'error': 'Remove opposite vote first'} return {'error': 'Remove opposite vote first'}
else: elif filter(lambda r: r.created_by == author.id, rating_reactions):
return {'error': "You can't rate your own thing"}
rdict = await _create_reaction(session, shout, author, reaction) rdict = await _create_reaction(session, shout, author, reaction)
return {'reaction': rdict} return {'reaction': rdict}
except Exception as e: except Exception as e:
@ -253,15 +243,15 @@ async def update_reaction(_, info, rid, reaction):
@mutation.field('delete_reaction') @mutation.field('delete_reaction')
@login_required @login_required
async def delete_reaction(_, info, reaction_id): async def delete_reaction(_, info, reaction_id: int):
user_id = info.context['user_id'] user_id = info.context['user_id']
roles = info.context['roles'] roles = info.context['roles']
if isinstance(reaction_id, int) and user_id and isinstance(roles, list):
with local_session() as session: with local_session() as session:
r = session.query(Reaction).filter(Reaction.id == reaction_id).first() try:
if not r: author = session.query(Author).filter(Author.user == user_id).one()
return {'error': 'invalid reaction id'} r = session.query(Reaction).filter(Reaction.id == reaction_id).one()
author = session.query(Author).filter(Author.user == user_id).first() if r and author:
if author:
if r.created_by is author.id and 'editor' not in roles: if r.created_by is author.id and 'editor' not in roles:
return {'error': 'access denied'} return {'error': 'access denied'}
@ -269,9 +259,9 @@ async def delete_reaction(_, info, reaction_id):
session.delete(r) session.delete(r)
session.commit() session.commit()
await notify_reaction(r.dict(), 'delete') await notify_reaction(r.dict(), 'delete')
else: except Exception as exc:
return {'error': 'access denied'} return {'error': f'cannot delete reaction: {exc}'}
return {} return {'error': 'cannot delete reaction'}
def apply_reaction_filters(by, q): def apply_reaction_filters(by, q):