create-reaction-revision
This commit is contained in:
parent
ad45cd4b10
commit
6be7ada9a1
|
@ -170,15 +170,24 @@ def set_hidden(session, shout_id):
|
||||||
@login_required
|
@login_required
|
||||||
async def create_reaction(_, info, reaction):
|
async def create_reaction(_, info, reaction):
|
||||||
user_id = info.context["user_id"]
|
user_id = info.context["user_id"]
|
||||||
|
|
||||||
|
shout_id = reaction.get("shout")
|
||||||
|
|
||||||
|
if not shout_id:
|
||||||
|
return {"error": "Shout ID is required to create a reaction."}
|
||||||
|
|
||||||
|
try:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
shout = session.query(Shout).where(Shout.id == reaction["shout"]).one()
|
shout = session.query(Shout).filter(Shout.id == shout_id).one()
|
||||||
author = session.query(Author).where(Author.user == user_id).first()
|
author = session.query(Author).filter(Author.user == user_id).first()
|
||||||
|
|
||||||
if shout and author:
|
if shout and author:
|
||||||
reaction["created_by"] = author.id
|
reaction["created_by"] = author.id
|
||||||
|
|
||||||
if reaction["kind"] in [ReactionKind.DISLIKE.value, ReactionKind.LIKE.value]:
|
if reaction["kind"] in [ReactionKind.DISLIKE.value, ReactionKind.LIKE.value]:
|
||||||
existing_reaction = (
|
existing_reaction = (
|
||||||
session.query(Reaction)
|
session.query(Reaction)
|
||||||
.where(
|
.filter(
|
||||||
and_(
|
and_(
|
||||||
Reaction.shout == reaction["shout"],
|
Reaction.shout == reaction["shout"],
|
||||||
Reaction.created_by == author.id,
|
Reaction.created_by == author.id,
|
||||||
|
@ -199,7 +208,7 @@ async def create_reaction(_, info, reaction):
|
||||||
)
|
)
|
||||||
opposite_reaction = (
|
opposite_reaction = (
|
||||||
session.query(Reaction)
|
session.query(Reaction)
|
||||||
.where(
|
.filter(
|
||||||
and_(
|
and_(
|
||||||
Reaction.shout == reaction["shout"],
|
Reaction.shout == reaction["shout"],
|
||||||
Reaction.created_by == author.id,
|
Reaction.created_by == author.id,
|
||||||
|
@ -215,10 +224,11 @@ async def create_reaction(_, info, reaction):
|
||||||
|
|
||||||
r = Reaction(**reaction)
|
r = Reaction(**reaction)
|
||||||
rdict = r.dict()
|
rdict = r.dict()
|
||||||
# Proposal accepting logix
|
|
||||||
|
# Proposal accepting logic
|
||||||
if rdict.get("reply_to"):
|
if rdict.get("reply_to"):
|
||||||
if r.kind is ReactionKind.ACCEPT.value and author.id in shout.authors:
|
if r.kind is ReactionKind.ACCEPT.value and author.id in shout.authors:
|
||||||
replied_reaction = session.query(Reaction).where(Reaction.id == r.reply_to).first()
|
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to).first()
|
||||||
if replied_reaction:
|
if replied_reaction:
|
||||||
if replied_reaction.kind is ReactionKind.PROPOSE.value:
|
if replied_reaction.kind is ReactionKind.PROPOSE.value:
|
||||||
if replied_reaction.range:
|
if replied_reaction.range:
|
||||||
|
@ -233,29 +243,31 @@ async def create_reaction(_, info, reaction):
|
||||||
|
|
||||||
session.add(r)
|
session.add(r)
|
||||||
session.commit()
|
session.commit()
|
||||||
rdict = r.dict()
|
|
||||||
rdict["shout"] = shout.dict()
|
|
||||||
rdict["created_by"] = author.dict()
|
|
||||||
|
|
||||||
# self-regulation mechanics
|
|
||||||
|
|
||||||
|
# Self-regulation mechanics
|
||||||
if check_to_hide(session, r):
|
if check_to_hide(session, r):
|
||||||
set_hidden(session, r.shout)
|
set_hidden(session, r.shout)
|
||||||
elif check_to_publish(session, author.id, r):
|
elif check_to_publish(session, author.id, r):
|
||||||
await set_published(session, r.shout, author.id)
|
await set_published(session, r.shout, author.id)
|
||||||
|
|
||||||
try:
|
# Reactions auto-following
|
||||||
reactions_follow(author.id, reaction["shout"], True)
|
reactions_follow(author.id, reaction["shout"], True)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"[resolvers.reactions] error on reactions auto following: {e}")
|
|
||||||
|
|
||||||
|
rdict["shout"] = shout.dict()
|
||||||
|
rdict["created_by"] = author.dict()
|
||||||
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
|
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
|
||||||
|
|
||||||
# notifications call
|
# Notifications call
|
||||||
await notify_reaction(rdict, "create")
|
await notify_reaction(rdict, "create")
|
||||||
|
|
||||||
return {"reaction": rdict}
|
return {"reaction": rdict}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"[resolvers.reactions] error: {type(e).__name__} - {e}")
|
||||||
|
|
||||||
|
return {"error": "Cannot create reaction."}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@mutation.field("update_reaction")
|
@mutation.field("update_reaction")
|
||||||
@login_required
|
@login_required
|
||||||
|
|
Loading…
Reference in New Issue
Block a user