reaction-model-fix
All checks were successful
deploy / deploy (push) Successful in 2m19s

This commit is contained in:
2023-11-27 19:03:47 +03:00
parent 909ddbd79d
commit caa2dbfdf3
6 changed files with 83 additions and 77 deletions

View File

@@ -138,14 +138,14 @@ async def get_authors_all(_, _info):
@query.field("getAuthor")
async def get_author(_, _info, slug="", user=None, author=None):
if slug or user or author:
async def get_author(_, _info, slug="", user=None, author_id=None):
if slug or user or author_id:
if slug:
q = select(Author).where(Author.slug == slug)
elif user:
q = select(Author).where(Author.user == user)
elif author:
q = select(Author).where(Author.id == author)
elif author_id:
q = select(Author).where(Author.id == author_id)
q = add_author_stat_columns(q)
authors = get_authors_from_query(q)

View File

@@ -163,81 +163,82 @@ async def create_reaction(_, info, reaction):
with local_session() as session:
shout = session.query(Shout).where(Shout.id == reaction["shout"]).one()
author = session.query(Author).where(Author.user == user_id).first()
reaction["created_by"] = author.id
if reaction["kind"] in [ReactionKind.DISLIKE.name, ReactionKind.LIKE.name]:
existing_reaction = (
session.query(Reaction)
.where(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == reaction["kind"],
Reaction.reply_to == reaction.get("reply_to"),
if shout and author:
reaction["created_by"] = author.id
if reaction["kind"] in [ReactionKind.DISLIKE.name, ReactionKind.LIKE.name]:
existing_reaction = (
session.query(Reaction)
.where(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == reaction["kind"],
Reaction.reply_to == reaction.get("reply_to"),
)
)
.first()
)
.first()
)
if existing_reaction is not None:
return {"error": "You can't vote twice"}
if existing_reaction is not None:
return {"error": "You can't vote twice"}
opposite_reaction_kind = (
ReactionKind.DISLIKE if reaction["kind"] == ReactionKind.LIKE.name else ReactionKind.LIKE
)
opposite_reaction = (
session.query(Reaction)
.where(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == opposite_reaction_kind,
Reaction.reply_to == reaction.get("reply_to"),
opposite_reaction_kind = (
ReactionKind.DISLIKE if reaction["kind"] == ReactionKind.LIKE.name else ReactionKind.LIKE
)
opposite_reaction = (
session.query(Reaction)
.where(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == opposite_reaction_kind,
Reaction.reply_to == reaction.get("reply_to"),
)
)
.first()
)
.first()
)
if opposite_reaction is not None:
session.delete(opposite_reaction)
if opposite_reaction is not None:
session.delete(opposite_reaction)
r = Reaction(**reaction)
r = Reaction(**reaction)
# Proposal accepting logix
if r.reply_to is not None and r.kind == ReactionKind.ACCEPT and author.id in shout.dict()["authors"]:
replied_reaction = session.query(Reaction).where(Reaction.id == r.reply_to).first()
if replied_reaction and replied_reaction.kind == ReactionKind.PROPOSE:
if replied_reaction.range:
old_body = shout.body
start, end = replied_reaction.range.split(":")
start = int(start)
end = int(end)
new_body = old_body[:start] + replied_reaction.body + old_body[end:]
shout.body = new_body
# Proposal accepting logix
if r.reply_to is not None and r.kind == ReactionKind.ACCEPT and author.id in shout.dict()["authors"]:
replied_reaction = session.query(Reaction).where(Reaction.id == r.reply_to).first()
if replied_reaction and replied_reaction.kind == ReactionKind.PROPOSE:
if replied_reaction.range:
old_body = shout.body
start, end = replied_reaction.range.split(":")
start = int(start)
end = int(end)
new_body = old_body[:start] + replied_reaction.body + old_body[end:]
shout.body = new_body
session.add(r)
session.commit()
rdict = r.dict()
rdict["shout"] = shout.dict()
rdict["created_by"] = author.dict()
session.add(r)
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):
set_hidden(session, r.shout)
elif check_to_publish(session, author.id, r):
set_published(session, r.shout)
if check_to_hide(session, r):
set_hidden(session, r.shout)
elif check_to_publish(session, author.id, r):
set_published(session, r.shout)
try:
reactions_follow(author.id, reaction["shout"], True)
except Exception as e:
print(f"[resolvers.reactions] error on reactions auto following: {e}")
try:
reactions_follow(author.id, reaction["shout"], True)
except Exception as e:
print(f"[resolvers.reactions] error on reactions auto following: {e}")
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
# notifications call
await notify_reaction(rdict, "create")
# notifications call
await notify_reaction(rdict, "create")
return {"reaction": rdict}
return {"reaction": rdict}
@mutation.field("updateReaction")