2023-11-03 10:10:22 +00:00
|
|
|
import time
|
2023-11-22 16:38:39 +00:00
|
|
|
from typing import List
|
2024-01-13 08:49:12 +00:00
|
|
|
import logging
|
2023-11-22 16:38:39 +00:00
|
|
|
|
2023-12-17 20:30:20 +00:00
|
|
|
from sqlalchemy import and_, asc, case, desc, func, select, text
|
2023-11-30 07:38:41 +00:00
|
|
|
from sqlalchemy.orm import aliased, joinedload
|
2023-12-17 20:30:20 +00:00
|
|
|
|
|
|
|
from orm.author import Author
|
|
|
|
from orm.reaction import Reaction, ReactionKind
|
|
|
|
from orm.shout import Shout, ShoutReactionsFollower
|
2024-01-10 13:29:49 +00:00
|
|
|
from services.auth import login_required, add_user_role
|
2023-10-23 14:51:13 +00:00
|
|
|
from services.db import local_session
|
2023-12-17 20:30:20 +00:00
|
|
|
from services.notify import notify_reaction
|
2023-10-23 14:51:13 +00:00
|
|
|
from services.schema import mutation, query
|
2022-09-19 13:50:43 +00:00
|
|
|
|
|
|
|
|
2024-01-13 08:49:12 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
logger = logging.getLogger("\t[resolvers.reaction]\t")
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2022-11-28 20:34:42 +00:00
|
|
|
def add_reaction_stat_columns(q):
|
2023-01-17 21:07:44 +00:00
|
|
|
aliased_reaction = aliased(Reaction)
|
|
|
|
|
2023-12-02 20:38:28 +00:00
|
|
|
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns(
|
|
|
|
func.sum(aliased_reaction.id).label("reacted_stat"),
|
|
|
|
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"),
|
2023-01-17 21:07:44 +00:00
|
|
|
func.sum(
|
|
|
|
case(
|
2023-11-30 07:38:41 +00:00
|
|
|
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISAGREE.value, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.PROOF.value, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISPROOF.value, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.ACCEPT.value, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.REJECT.value, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.LIKE.value, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
2023-10-05 18:46:18 +00:00
|
|
|
else_=0,
|
2023-01-17 21:07:44 +00:00
|
|
|
)
|
2023-12-02 20:38:28 +00:00
|
|
|
).label("rating_stat"),
|
|
|
|
)
|
2023-01-17 21:07:44 +00:00
|
|
|
|
2023-11-29 11:16:09 +00:00
|
|
|
return q, aliased_reaction
|
2022-11-28 20:34:42 +00:00
|
|
|
|
|
|
|
|
2023-11-29 08:00:00 +00:00
|
|
|
def reactions_follow(author_id, shout_id, auto=False):
|
2023-02-20 17:38:20 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2023-02-20 17:38:20 +00:00
|
|
|
following = (
|
2023-10-05 18:46:18 +00:00
|
|
|
session.query(ShoutReactionsFollower)
|
|
|
|
.where(
|
|
|
|
and_(
|
2023-10-23 14:47:11 +00:00
|
|
|
ShoutReactionsFollower.follower == author_id,
|
2023-10-05 18:46:18 +00:00
|
|
|
ShoutReactionsFollower.shout == shout.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.first()
|
2022-09-18 14:29:21 +00:00
|
|
|
)
|
2023-02-20 17:38:20 +00:00
|
|
|
|
|
|
|
if not following:
|
2023-11-22 16:38:39 +00:00
|
|
|
following = ShoutReactionsFollower(follower=author_id, shout=shout.id, auto=auto)
|
2023-02-20 17:38:20 +00:00
|
|
|
session.add(following)
|
|
|
|
session.commit()
|
|
|
|
return True
|
2023-10-16 15:18:29 +00:00
|
|
|
except Exception:
|
2023-02-20 17:38:20 +00:00
|
|
|
return False
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
2024-01-13 08:01:59 +00:00
|
|
|
def reactions_unfollow(author_id, shout_id: int):
|
2023-02-20 17:38:20 +00:00
|
|
|
try:
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).where(Shout.id == shout_id).one()
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2023-02-20 17:38:20 +00:00
|
|
|
following = (
|
2023-10-05 18:46:18 +00:00
|
|
|
session.query(ShoutReactionsFollower)
|
|
|
|
.where(
|
|
|
|
and_(
|
2023-10-23 14:47:11 +00:00
|
|
|
ShoutReactionsFollower.follower == author_id,
|
2023-10-05 18:46:18 +00:00
|
|
|
ShoutReactionsFollower.shout == shout.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.first()
|
2023-02-20 17:38:20 +00:00
|
|
|
)
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2023-02-20 17:38:20 +00:00
|
|
|
if following:
|
|
|
|
session.delete(following)
|
|
|
|
session.commit()
|
|
|
|
return True
|
2023-10-16 15:18:29 +00:00
|
|
|
except Exception:
|
2023-02-20 17:38:20 +00:00
|
|
|
pass
|
|
|
|
return False
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
def is_published_author(session, author_id):
|
|
|
|
"""checks if author has at least one publication"""
|
2023-10-05 18:46:18 +00:00
|
|
|
return (
|
|
|
|
session.query(Shout)
|
2023-12-24 22:06:27 +00:00
|
|
|
.where(Shout.authors.any(author_id))
|
2023-11-29 11:24:59 +00:00
|
|
|
.filter(and_(Shout.published_at != "", Shout.deleted_at.is_(None)))
|
2023-10-05 18:46:18 +00:00
|
|
|
.count()
|
|
|
|
> 0
|
|
|
|
)
|
2022-11-13 15:24:29 +00:00
|
|
|
|
|
|
|
|
2023-12-24 22:42:39 +00:00
|
|
|
def check_to_publish(session, approver_id, reaction):
|
2023-10-05 18:46:18 +00:00
|
|
|
"""set shout to public if publicated approvers amount > 4"""
|
2023-11-03 10:10:22 +00:00
|
|
|
if not reaction.reply_to and reaction.kind in [
|
2023-11-30 07:38:41 +00:00
|
|
|
ReactionKind.ACCEPT.value,
|
|
|
|
ReactionKind.LIKE.value,
|
|
|
|
ReactionKind.PROOF.value,
|
2022-11-13 15:24:29 +00:00
|
|
|
]:
|
2023-12-24 22:42:39 +00:00
|
|
|
if is_published_author(session, approver_id):
|
2022-11-13 15:24:29 +00:00
|
|
|
# now count how many approvers are voted already
|
2023-11-22 16:38:39 +00:00
|
|
|
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
2023-10-05 18:46:18 +00:00
|
|
|
approvers = [
|
2023-12-24 22:42:39 +00:00
|
|
|
approver_id,
|
2023-10-05 18:46:18 +00:00
|
|
|
]
|
2022-11-13 15:24:29 +00:00
|
|
|
for ar in approvers_reactions:
|
2023-11-03 10:10:22 +00:00
|
|
|
a = ar.created_by
|
2022-11-13 15:24:29 +00:00
|
|
|
if is_published_author(session, a):
|
|
|
|
approvers.append(a)
|
|
|
|
if len(approvers) > 4:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
def check_to_hide(session, reaction):
|
2023-10-05 18:46:18 +00:00
|
|
|
"""hides any shout if 20% of reactions are negative"""
|
2023-11-03 10:10:22 +00:00
|
|
|
if not reaction.reply_to and reaction.kind in [
|
2023-11-30 07:38:41 +00:00
|
|
|
ReactionKind.REJECT.value,
|
|
|
|
ReactionKind.DISLIKE.value,
|
|
|
|
ReactionKind.DISPROOF.value,
|
2022-11-13 15:24:29 +00:00
|
|
|
]:
|
2023-10-23 14:47:11 +00:00
|
|
|
# if is_published_author(author_id):
|
2023-11-22 16:38:39 +00:00
|
|
|
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
2022-12-23 16:37:17 +00:00
|
|
|
rejects = 0
|
2022-11-13 15:24:29 +00:00
|
|
|
for r in approvers_reactions:
|
|
|
|
if r.kind in [
|
2023-11-30 07:38:41 +00:00
|
|
|
ReactionKind.REJECT.value,
|
|
|
|
ReactionKind.DISLIKE.value,
|
|
|
|
ReactionKind.DISPROOF.value,
|
2022-11-13 15:24:29 +00:00
|
|
|
]:
|
2022-12-23 16:37:17 +00:00
|
|
|
rejects += 1
|
|
|
|
if len(approvers_reactions) / rejects < 5:
|
2022-11-13 15:24:29 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-01-10 13:29:49 +00:00
|
|
|
async def set_published(session, shout_id, approver_id):
|
2022-12-01 14:45:19 +00:00
|
|
|
s = session.query(Shout).where(Shout.id == shout_id).first()
|
2023-11-03 10:10:22 +00:00
|
|
|
s.published_at = int(time.time())
|
2023-12-24 22:42:39 +00:00
|
|
|
s.published_by = approver_id
|
2023-10-05 18:46:18 +00:00
|
|
|
s.visibility = text("public")
|
2024-01-10 13:29:49 +00:00
|
|
|
author = session.query(Author).filter(Author.id == s.created_by).first()
|
|
|
|
if author:
|
|
|
|
await add_user_role(str(author.user))
|
2022-11-13 15:24:29 +00:00
|
|
|
session.add(s)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
def set_hidden(session, shout_id):
|
|
|
|
s = session.query(Shout).where(Shout.id == shout_id).first()
|
2023-10-05 18:46:18 +00:00
|
|
|
s.visibility = text("community")
|
2022-11-13 15:24:29 +00:00
|
|
|
session.add(s)
|
|
|
|
session.commit()
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@mutation.field("create_reaction")
|
2022-07-21 11:58:50 +00:00
|
|
|
@login_required
|
2023-02-26 21:23:25 +00:00
|
|
|
async def create_reaction(_, info, reaction):
|
2023-11-23 23:00:28 +00:00
|
|
|
user_id = info.context["user_id"]
|
2024-01-22 20:54:02 +00:00
|
|
|
|
|
|
|
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:
|
|
|
|
shout = session.query(Shout).filter(Shout.id == shout_id).one()
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
|
|
|
|
if shout and author:
|
|
|
|
reaction["created_by"] = author.id
|
2024-01-22 22:11:34 +00:00
|
|
|
kind = reaction.get("kind")
|
|
|
|
if not kind and reaction.get("body"):
|
|
|
|
kind = ReactionKind.COMMENT.value
|
|
|
|
if not kind:
|
|
|
|
return { "error": "cannot create reaction with this kind"}
|
|
|
|
existing_reaction = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Reaction.shout == shout_id,
|
|
|
|
Reaction.created_by == author.id,
|
|
|
|
Reaction.kind == kind,
|
|
|
|
Reaction.reply_to == reaction.get("reply_to"),
|
2023-11-27 16:03:47 +00:00
|
|
|
)
|
2023-10-05 18:46:18 +00:00
|
|
|
)
|
2024-01-22 22:11:34 +00:00
|
|
|
.first()
|
|
|
|
)
|
2023-02-26 21:23:25 +00:00
|
|
|
|
2024-01-22 22:11:34 +00:00
|
|
|
if existing_reaction is not None:
|
|
|
|
return {"error": "You can't vote twice"}
|
2023-02-26 21:23:25 +00:00
|
|
|
|
2024-01-22 22:11:34 +00:00
|
|
|
opposite_reaction_kind = (
|
|
|
|
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"),
|
2023-11-27 16:03:47 +00:00
|
|
|
)
|
2023-02-26 21:23:25 +00:00
|
|
|
)
|
2024-01-22 22:11:34 +00:00
|
|
|
.first()
|
|
|
|
)
|
2022-12-23 16:37:17 +00:00
|
|
|
|
2024-01-22 22:11:34 +00:00
|
|
|
if opposite_reaction is not None:
|
|
|
|
session.delete(opposite_reaction)
|
2024-01-22 20:54:02 +00:00
|
|
|
|
|
|
|
r = Reaction(**reaction)
|
|
|
|
rdict = r.dict()
|
|
|
|
|
|
|
|
# Proposal accepting logic
|
|
|
|
if rdict.get("reply_to"):
|
|
|
|
if r.kind is ReactionKind.ACCEPT.value and author.id in shout.authors:
|
|
|
|
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to).first()
|
|
|
|
if replied_reaction:
|
|
|
|
if replied_reaction.kind is ReactionKind.PROPOSE.value:
|
|
|
|
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_dict = shout.dict()
|
|
|
|
shout_dict["body"] = new_body
|
|
|
|
Shout.update(shout, shout_dict)
|
|
|
|
|
|
|
|
session.add(r)
|
|
|
|
session.commit()
|
2024-01-22 21:51:50 +00:00
|
|
|
logger.debug(r)
|
|
|
|
rdict = r.dict()
|
2024-01-22 22:11:34 +00:00
|
|
|
|
2024-01-22 20:54:02 +00:00
|
|
|
# Self-regulation mechanics
|
|
|
|
if check_to_hide(session, r):
|
|
|
|
set_hidden(session, r.shout)
|
|
|
|
elif check_to_publish(session, author.id, r):
|
|
|
|
await set_published(session, r.shout, author.id)
|
2022-11-13 15:24:29 +00:00
|
|
|
|
2024-01-22 20:54:02 +00:00
|
|
|
# Reactions auto-following
|
2023-11-27 16:03:47 +00:00
|
|
|
reactions_follow(author.id, reaction["shout"], True)
|
2024-01-22 21:51:50 +00:00
|
|
|
|
2024-01-22 20:54:02 +00:00
|
|
|
rdict["shout"] = shout.dict()
|
|
|
|
rdict["created_by"] = author.dict()
|
|
|
|
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
|
|
|
|
|
|
|
|
# Notifications call
|
|
|
|
await notify_reaction(rdict, "create")
|
|
|
|
|
|
|
|
return {"reaction": rdict}
|
|
|
|
|
|
|
|
except Exception as e:
|
2024-01-22 21:27:57 +00:00
|
|
|
import traceback
|
2024-01-22 21:51:50 +00:00
|
|
|
traceback.print_exc()
|
2024-01-22 21:27:57 +00:00
|
|
|
logger.error(f"{type(e).__name__}: {e}")
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2024-01-22 20:54:02 +00:00
|
|
|
return {"error": "Cannot create reaction."}
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@mutation.field("update_reaction")
|
2022-07-21 11:58:50 +00:00
|
|
|
@login_required
|
2023-11-22 16:38:39 +00:00
|
|
|
async def update_reaction(_, info, rid, reaction):
|
2023-11-23 23:00:28 +00:00
|
|
|
user_id = info.context["user_id"]
|
2022-07-21 11:58:50 +00:00
|
|
|
with local_session() as session:
|
2023-10-23 14:47:11 +00:00
|
|
|
q = select(Reaction).filter(Reaction.id == rid)
|
2023-11-30 07:38:41 +00:00
|
|
|
q, aliased_reaction = add_reaction_stat_columns(q)
|
2023-02-06 13:15:47 +00:00
|
|
|
q = q.group_by(Reaction.id)
|
2022-11-28 08:47:39 +00:00
|
|
|
|
2023-11-22 16:38:39 +00:00
|
|
|
[r, reacted_stat, commented_stat, rating_stat] = session.execute(q).unique().one()
|
2022-11-28 08:47:39 +00:00
|
|
|
|
2022-12-23 07:43:19 +00:00
|
|
|
if not r:
|
2022-07-21 11:58:50 +00:00
|
|
|
return {"error": "invalid reaction id"}
|
2023-11-23 23:00:28 +00:00
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
|
|
|
if r.created_by != author.id:
|
|
|
|
return {"error": "access denied"}
|
|
|
|
body = reaction.get("body")
|
|
|
|
if body:
|
|
|
|
r.body = body
|
|
|
|
r.updated_at = int(time.time())
|
|
|
|
if r.kind != reaction["kind"]:
|
2023-12-25 07:48:50 +00:00
|
|
|
# TODO: change mind detection can be here
|
2023-11-23 23:00:28 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
session.commit()
|
|
|
|
r.stat = {
|
|
|
|
"commented": commented_stat,
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"rating": rating_stat,
|
|
|
|
}
|
|
|
|
|
|
|
|
await notify_reaction(r.dict(), "update")
|
|
|
|
|
|
|
|
return {"reaction": r}
|
|
|
|
else:
|
2024-01-22 22:11:34 +00:00
|
|
|
return {"error": "not authorized"}
|
|
|
|
return {"error": "cannot create reaction"}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@mutation.field("delete_reaction")
|
2022-07-21 11:58:50 +00:00
|
|
|
@login_required
|
2023-10-23 14:47:11 +00:00
|
|
|
async def delete_reaction(_, info, rid):
|
2023-11-23 23:00:28 +00:00
|
|
|
user_id = info.context["user_id"]
|
2022-07-21 11:58:50 +00:00
|
|
|
with local_session() as session:
|
2023-10-23 14:47:11 +00:00
|
|
|
r = session.query(Reaction).filter(Reaction.id == rid).first()
|
2022-12-23 07:43:19 +00:00
|
|
|
if not r:
|
2022-07-21 11:58:50 +00:00
|
|
|
return {"error": "invalid reaction id"}
|
2023-11-23 23:00:28 +00:00
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
2023-11-28 07:53:48 +00:00
|
|
|
if author:
|
2023-11-29 08:00:00 +00:00
|
|
|
if r.created_by is author.id:
|
2023-11-28 07:53:48 +00:00
|
|
|
return {"error": "access denied"}
|
2023-02-26 21:23:25 +00:00
|
|
|
|
2023-11-30 07:38:41 +00:00
|
|
|
if r.kind in [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]:
|
2023-11-28 07:53:48 +00:00
|
|
|
session.delete(r)
|
|
|
|
else:
|
2023-11-29 08:00:00 +00:00
|
|
|
rdict = r.dict()
|
|
|
|
rdict["deleted_at"] = int(time.time())
|
|
|
|
Reaction.update(r, rdict)
|
|
|
|
session.add(r)
|
2023-11-28 07:53:48 +00:00
|
|
|
session.commit()
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
await notify_reaction(r.dict(), "delete")
|
2023-10-23 14:47:11 +00:00
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
return {"reaction": r}
|
|
|
|
else:
|
|
|
|
return {"error": "access denied"}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-11-28 20:29:02 +00:00
|
|
|
|
2023-11-29 09:59:00 +00:00
|
|
|
def apply_reaction_filters(by, q):
|
|
|
|
if by.get("shout"):
|
|
|
|
q = q.filter(Shout.slug == by["shout"])
|
|
|
|
|
|
|
|
elif by.get("shouts"):
|
|
|
|
q = q.filter(Shout.slug.in_(by["shouts"]))
|
|
|
|
|
|
|
|
if by.get("created_by"):
|
|
|
|
q = q.filter(Author.id == by["created_by"])
|
|
|
|
|
|
|
|
if by.get("topic"):
|
|
|
|
q = q.filter(Shout.topics.contains(by["topic"]))
|
|
|
|
|
|
|
|
if by.get("comment"):
|
|
|
|
q = q.filter(func.length(Reaction.body) > 0)
|
|
|
|
|
|
|
|
# NOTE: not using ElasticSearch here
|
|
|
|
by_search = by.get("search", "")
|
|
|
|
if len(by_search) > 2:
|
2023-11-29 11:24:59 +00:00
|
|
|
q = q.filter(Reaction.body.ilike(f"%{by_search}%"))
|
2023-11-29 09:59:00 +00:00
|
|
|
|
|
|
|
if by.get("after"):
|
|
|
|
after = int(by["after"])
|
|
|
|
q = q.filter(Reaction.created_at > after)
|
|
|
|
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("load_reactions_by")
|
2023-10-23 14:47:11 +00:00
|
|
|
async def load_reactions_by(_, info, by, limit=50, offset=0):
|
2022-11-23 02:05:34 +00:00
|
|
|
"""
|
2023-10-23 14:47:11 +00:00
|
|
|
:param info: graphql meta
|
2022-11-23 02:05:34 +00:00
|
|
|
:param by: {
|
|
|
|
:shout - filter by slug
|
2023-02-12 03:27:55 +00:00
|
|
|
:shouts - filer by shout slug list
|
2023-11-03 10:10:22 +00:00
|
|
|
:created_by - to filter by author
|
2022-11-23 02:05:34 +00:00
|
|
|
:topic - to filter by topic
|
|
|
|
:search - to search by reactions' body
|
|
|
|
:comment - true if body.length > 0
|
2023-11-29 07:23:41 +00:00
|
|
|
:after - amount of time ago
|
2022-11-23 02:05:34 +00:00
|
|
|
:sort - a fieldname to sort desc by default
|
|
|
|
}
|
|
|
|
:param limit: int amount of shouts
|
|
|
|
:param offset: int offset in this order
|
|
|
|
:return: Reaction[]
|
|
|
|
"""
|
|
|
|
|
2023-10-05 18:46:18 +00:00
|
|
|
q = (
|
2023-10-23 14:47:11 +00:00
|
|
|
select(Reaction, Author, Shout)
|
2023-11-03 10:10:22 +00:00
|
|
|
.join(Author, Reaction.created_by == Author.id)
|
2023-10-05 18:46:18 +00:00
|
|
|
.join(Shout, Reaction.shout == Shout.id)
|
2022-11-27 08:19:38 +00:00
|
|
|
)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2023-11-30 07:38:41 +00:00
|
|
|
# calculate counters
|
2023-11-29 11:16:09 +00:00
|
|
|
q, aliased_reaction = add_reaction_stat_columns(q)
|
|
|
|
|
2023-11-30 07:38:41 +00:00
|
|
|
# filter
|
2023-11-29 09:59:00 +00:00
|
|
|
q = apply_reaction_filters(by, q)
|
2023-11-30 07:38:41 +00:00
|
|
|
q = q.where(Reaction.deleted_at.is_(None))
|
2023-11-29 11:16:09 +00:00
|
|
|
|
2024-01-13 07:27:45 +00:00
|
|
|
# group by
|
2024-01-13 08:15:45 +00:00
|
|
|
q = q.group_by(Reaction.id, Author.id, Shout.id, aliased_reaction.id)
|
|
|
|
|
|
|
|
# order by
|
|
|
|
q = q.order_by(desc("created_at"))
|
2023-11-29 11:16:09 +00:00
|
|
|
|
2023-11-30 07:38:41 +00:00
|
|
|
# pagination
|
2022-11-22 07:29:54 +00:00
|
|
|
q = q.limit(limit).offset(offset)
|
2023-11-30 07:38:41 +00:00
|
|
|
|
2022-11-28 08:47:39 +00:00
|
|
|
reactions = []
|
2023-11-29 10:50:20 +00:00
|
|
|
with local_session() as session:
|
|
|
|
result_rows = session.execute(q)
|
|
|
|
for [
|
|
|
|
reaction,
|
|
|
|
author,
|
|
|
|
shout,
|
|
|
|
reacted_stat,
|
|
|
|
commented_stat,
|
|
|
|
rating_stat,
|
|
|
|
] in result_rows:
|
|
|
|
reaction.created_by = author
|
|
|
|
reaction.shout = shout
|
2023-12-02 20:38:28 +00:00
|
|
|
reaction.stat = {"rating": rating_stat, "commented": commented_stat, "reacted": reacted_stat}
|
2023-11-29 10:50:20 +00:00
|
|
|
reactions.append(reaction)
|
|
|
|
|
|
|
|
# sort if by stat is present
|
2024-01-13 08:15:45 +00:00
|
|
|
stat_sort = by.get("stat")
|
|
|
|
if stat_sort:
|
|
|
|
reactions = sorted(reactions, key=lambda r: r.stat.get(stat_sort) or r.created_at, reverse=stat_sort.startswith("-"))
|
2022-11-23 02:05:34 +00:00
|
|
|
|
|
|
|
return reactions
|
2023-10-23 14:47:11 +00:00
|
|
|
|
|
|
|
|
2023-11-28 07:53:48 +00:00
|
|
|
def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
|
|
|
shouts: List[Shout] = []
|
2023-11-22 16:38:39 +00:00
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).where(Author.id == follower_id).first()
|
|
|
|
if author:
|
|
|
|
shouts = (
|
2023-11-28 07:53:48 +00:00
|
|
|
session.query(Shout)
|
2024-01-13 08:15:45 +00:00
|
|
|
.join(Reaction, Reaction.shout == Shout.id)
|
|
|
|
.options(joinedload(Reaction.created_by))
|
2023-11-29 08:00:00 +00:00
|
|
|
.filter(Reaction.created_by == follower_id)
|
2023-11-22 16:38:39 +00:00
|
|
|
.filter(Reaction.created_at > author.last_seen)
|
2023-11-28 07:53:48 +00:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
2023-11-29 08:00:00 +00:00
|
|
|
.all()
|
2023-11-22 16:38:39 +00:00
|
|
|
)
|
|
|
|
return shouts
|
|
|
|
|
|
|
|
|
2023-10-23 14:47:11 +00:00
|
|
|
@login_required
|
2023-11-28 07:53:48 +00:00
|
|
|
@query.field("load_shouts_followed")
|
|
|
|
async def load_shouts_followed(_, info, limit=50, offset=0) -> List[Shout]:
|
2023-11-23 23:00:28 +00:00
|
|
|
user_id = info.context["user_id"]
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
if author:
|
2024-01-13 08:49:12 +00:00
|
|
|
try:
|
|
|
|
author_id: int = author.dict()["id"]
|
|
|
|
shouts = reacted_shouts_updates(author_id, limit, offset)
|
|
|
|
return shouts
|
|
|
|
except Exception as error:
|
|
|
|
logger.debug(error)
|
|
|
|
return []
|