This commit is contained in:
parent
7746d1992f
commit
85931d04ba
|
@ -157,48 +157,38 @@ 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):
|
||||||
rdict = await _create_reaction(session, shout, author, reaction)
|
return {'error': "You can't rate your own thing"}
|
||||||
return {'reaction': rdict}
|
|
||||||
|
rdict = await _create_reaction(session, shout, author, reaction)
|
||||||
|
return {'reaction': rdict}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
@ -253,25 +243,25 @@ 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']
|
||||||
with local_session() as session:
|
if isinstance(reaction_id, int) and user_id and isinstance(roles, list):
|
||||||
r = session.query(Reaction).filter(Reaction.id == reaction_id).first()
|
with local_session() as session:
|
||||||
if not r:
|
try:
|
||||||
return {'error': 'invalid reaction id'}
|
author = session.query(Author).filter(Author.user == user_id).one()
|
||||||
author = session.query(Author).filter(Author.user == user_id).first()
|
r = session.query(Reaction).filter(Reaction.id == reaction_id).one()
|
||||||
if author:
|
if r and 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'}
|
||||||
|
|
||||||
if r.kind in [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]:
|
if r.kind in [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]:
|
||||||
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):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user