2022-11-23 14:09:35 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2023-01-17 21:07:44 +00:00
|
|
|
from sqlalchemy import and_, asc, desc, select, text, func, case
|
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
2022-09-17 18:12:14 +00:00
|
|
|
from auth.authenticate import login_required
|
2022-12-01 14:45:19 +00:00
|
|
|
from auth.credentials import AuthCredentials
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
2022-09-17 18:12:14 +00:00
|
|
|
from base.resolvers import mutation, query
|
2022-11-13 15:24:29 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
|
|
|
from orm.shout import Shout, ShoutReactionsFollower
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.user import User
|
2022-09-19 13:50:43 +00:00
|
|
|
|
|
|
|
|
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-02-12 14:18:57 +00:00
|
|
|
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.replyTo).add_columns(
|
2023-01-17 21:07:44 +00:00
|
|
|
func.sum(
|
|
|
|
aliased_reaction.id
|
|
|
|
).label('reacted_stat'),
|
|
|
|
func.sum(
|
|
|
|
case(
|
|
|
|
(aliased_reaction.body.is_not(None), 1),
|
|
|
|
else_=0
|
|
|
|
)
|
|
|
|
).label('commented_stat'),
|
|
|
|
func.sum(case(
|
|
|
|
(aliased_reaction.kind == ReactionKind.AGREE, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISAGREE, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.PROOF, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISPROOF, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.ACCEPT, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.REJECT, -1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.LIKE, 1),
|
|
|
|
(aliased_reaction.kind == ReactionKind.DISLIKE, -1),
|
|
|
|
else_=0)
|
|
|
|
).label('rating_stat'))
|
|
|
|
|
|
|
|
return q
|
2022-11-28 20:34:42 +00:00
|
|
|
|
|
|
|
|
2022-12-23 14:45:00 +00:00
|
|
|
def reactions_follow(user_id, shout_id: int, 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 = (
|
|
|
|
session.query(ShoutReactionsFollower).where(and_(
|
|
|
|
ShoutReactionsFollower.follower == user_id,
|
|
|
|
ShoutReactionsFollower.shout == shout.id,
|
|
|
|
)).first()
|
2022-09-18 14:29:21 +00:00
|
|
|
)
|
2023-02-20 17:38:20 +00:00
|
|
|
|
|
|
|
if not following:
|
|
|
|
following = ShoutReactionsFollower.create(
|
|
|
|
follower=user_id,
|
|
|
|
shout=shout.id,
|
|
|
|
auto=auto
|
|
|
|
)
|
|
|
|
session.add(following)
|
|
|
|
session.commit()
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
2022-12-23 14:45:00 +00:00
|
|
|
def reactions_unfollow(user_id: int, 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 = (
|
|
|
|
session.query(ShoutReactionsFollower).where(and_(
|
|
|
|
ShoutReactionsFollower.follower == user_id,
|
|
|
|
ShoutReactionsFollower.shout == shout.id
|
|
|
|
)).first()
|
|
|
|
)
|
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
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return False
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
def is_published_author(session, user_id):
|
2022-11-13 15:24:29 +00:00
|
|
|
''' checks if user has at least one publication '''
|
|
|
|
return session.query(
|
|
|
|
Shout
|
|
|
|
).where(
|
2022-12-01 14:45:19 +00:00
|
|
|
Shout.authors.contains(user_id)
|
2022-11-13 15:24:29 +00:00
|
|
|
).filter(
|
|
|
|
and_(
|
|
|
|
Shout.publishedAt.is_not(None),
|
|
|
|
Shout.deletedAt.is_(None)
|
|
|
|
)
|
|
|
|
).count() > 0
|
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
def check_to_publish(session, user_id, reaction):
|
2022-11-13 15:24:29 +00:00
|
|
|
''' set shout to public if publicated approvers amount > 4 '''
|
|
|
|
if not reaction.replyTo and reaction.kind in [
|
|
|
|
ReactionKind.ACCEPT,
|
|
|
|
ReactionKind.LIKE,
|
|
|
|
ReactionKind.PROOF
|
|
|
|
]:
|
2022-12-01 14:45:19 +00:00
|
|
|
if is_published_author(user_id):
|
2022-11-13 15:24:29 +00:00
|
|
|
# now count how many approvers are voted already
|
2022-11-30 06:27:12 +00:00
|
|
|
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
|
2022-12-01 14:45:19 +00:00
|
|
|
approvers = [user_id, ]
|
2022-11-13 15:24:29 +00:00
|
|
|
for ar in approvers_reactions:
|
|
|
|
a = ar.createdBy
|
|
|
|
if is_published_author(session, a):
|
|
|
|
approvers.append(a)
|
|
|
|
if len(approvers) > 4:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
def check_to_hide(session, user_id, reaction):
|
2022-11-13 15:24:29 +00:00
|
|
|
''' hides any shout if 20% of reactions are negative '''
|
|
|
|
if not reaction.replyTo and reaction.kind in [
|
2022-12-23 16:37:17 +00:00
|
|
|
ReactionKind.REJECT,
|
|
|
|
ReactionKind.DISLIKE,
|
|
|
|
ReactionKind.DISPROOF
|
2022-11-13 15:24:29 +00:00
|
|
|
]:
|
|
|
|
# if is_published_author(user):
|
2022-11-30 06:27:12 +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 [
|
2022-12-23 16:37:17 +00:00
|
|
|
ReactionKind.REJECT,
|
|
|
|
ReactionKind.DISLIKE,
|
|
|
|
ReactionKind.DISPROOF
|
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
|
|
|
|
|
|
|
|
|
2022-12-01 14:45:19 +00:00
|
|
|
def set_published(session, shout_id, publisher):
|
|
|
|
s = session.query(Shout).where(Shout.id == shout_id).first()
|
2022-11-23 14:09:35 +00:00
|
|
|
s.publishedAt = datetime.now(tz=timezone.utc)
|
2022-11-13 15:24:29 +00:00
|
|
|
s.publishedBy = publisher
|
2022-11-15 16:49:38 +00:00
|
|
|
s.visibility = text('public')
|
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()
|
2022-11-15 16:49:38 +00:00
|
|
|
s.visibility = text('authors')
|
2022-11-13 15:35:43 +00:00
|
|
|
s.publishedAt = None # TODO: discuss
|
|
|
|
s.publishedBy = None # TODO: store changes history in git
|
2022-11-13 15:24:29 +00:00
|
|
|
session.add(s)
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@mutation.field("createReaction")
|
|
|
|
@login_required
|
2022-12-23 07:43:19 +00:00
|
|
|
async def create_reaction(_, info, reaction={}):
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-12-23 14:45:00 +00:00
|
|
|
reaction['createdBy'] = auth.user_id
|
2023-02-17 15:31:49 +00:00
|
|
|
rdict = {}
|
2022-10-14 09:25:45 +00:00
|
|
|
with local_session() as session:
|
2022-12-23 07:43:19 +00:00
|
|
|
r = Reaction.create(**reaction)
|
2023-01-17 19:56:48 +00:00
|
|
|
shout = session.query(Shout).where(Shout.id == r.shout).one()
|
2023-02-17 15:31:49 +00:00
|
|
|
author = session.query(User).where(User.id == auth.user_id).one()
|
2023-01-17 19:56:48 +00:00
|
|
|
|
|
|
|
# Proposal accepting logix
|
|
|
|
if r.replyTo is not None and \
|
|
|
|
r.kind == ReactionKind.ACCEPT and \
|
2023-01-31 16:51:48 +00:00
|
|
|
auth.user_id in shout.dict()['authors']:
|
2023-02-17 15:31:49 +00:00
|
|
|
replied_reaction = session.query(Reaction).where(Reaction.id == r.replyTo).first()
|
2023-01-17 19:56:48 +00:00
|
|
|
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
|
|
|
|
# TODO: update git version control
|
|
|
|
|
2022-12-23 07:43:19 +00:00
|
|
|
session.add(r)
|
2022-10-14 09:25:45 +00:00
|
|
|
session.commit()
|
2023-02-17 15:31:49 +00:00
|
|
|
rdict = r.dict()
|
|
|
|
rdict['shout'] = shout.dict()
|
|
|
|
rdict['createdBy'] = author.dict()
|
2022-12-23 16:37:17 +00:00
|
|
|
|
2022-11-13 15:24:29 +00:00
|
|
|
# self-regulation mechanics
|
|
|
|
|
2022-12-23 07:43:19 +00:00
|
|
|
if check_to_hide(session, auth.user_id, r):
|
|
|
|
set_hidden(session, r.shout)
|
|
|
|
elif check_to_publish(session, auth.user_id, r):
|
|
|
|
set_published(session, r.shout, r.createdBy)
|
2022-11-13 15:24:29 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
try:
|
2022-12-23 07:43:19 +00:00
|
|
|
reactions_follow(auth.user_id, reaction["shout"], True)
|
2022-07-21 11:58:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
|
|
|
|
2023-02-17 15:31:49 +00:00
|
|
|
rdict['stat'] = {
|
2022-11-28 08:47:39 +00:00
|
|
|
"commented": 0,
|
|
|
|
"reacted": 0,
|
|
|
|
"rating": 0
|
|
|
|
}
|
2023-02-17 15:31:49 +00:00
|
|
|
return {"reaction": rdict}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("updateReaction")
|
|
|
|
@login_required
|
2023-02-06 13:15:47 +00:00
|
|
|
async def update_reaction(_, info, id, reaction={}):
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
with local_session() as session:
|
2022-12-01 14:45:19 +00:00
|
|
|
user = session.query(User).where(User.id == auth.user_id).first()
|
2023-02-06 13:15:47 +00:00
|
|
|
q = select(Reaction).filter(Reaction.id == id)
|
2022-11-28 22:16:39 +00:00
|
|
|
q = 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
|
|
|
|
2022-12-23 07:43:19 +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-01-11 15:16:21 +00:00
|
|
|
if r.createdBy != user.id:
|
2022-07-21 11:58:50 +00:00
|
|
|
return {"error": "access denied"}
|
2022-11-28 08:47:39 +00:00
|
|
|
|
2022-12-23 07:43:19 +00:00
|
|
|
r.body = reaction["body"]
|
|
|
|
r.updatedAt = datetime.now(tz=timezone.utc)
|
|
|
|
if r.kind != reaction["kind"]:
|
2022-08-11 11:22:10 +00:00
|
|
|
# NOTE: change mind detection can be here
|
2022-07-21 11:58:50 +00:00
|
|
|
pass
|
2022-12-23 07:43:19 +00:00
|
|
|
if reaction.get("range"):
|
|
|
|
r.range = reaction.get("range")
|
2022-07-21 11:58:50 +00:00
|
|
|
session.commit()
|
2022-12-23 07:43:19 +00:00
|
|
|
r.stat = {
|
2022-11-28 22:16:39 +00:00
|
|
|
"commented": commented_stat,
|
|
|
|
"reacted": reacted_stat,
|
|
|
|
"rating": rating_stat
|
2022-11-28 08:47:39 +00:00
|
|
|
}
|
2022-09-19 13:50:43 +00:00
|
|
|
|
2022-12-23 07:43:19 +00:00
|
|
|
return {"reaction": r}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("deleteReaction")
|
|
|
|
@login_required
|
2023-02-06 13:15:47 +00:00
|
|
|
async def delete_reaction(_, info, id):
|
2022-12-23 07:43:19 +00:00
|
|
|
# NOTE: reaction is id
|
2022-12-01 14:45:19 +00:00
|
|
|
auth: AuthCredentials = info.context["request"].auth
|
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
with local_session() as session:
|
2023-02-06 13:15:47 +00:00
|
|
|
r = session.query(Reaction).filter(Reaction.id == id).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-01-17 08:54:05 +00:00
|
|
|
if r.createdBy != auth.user_id:
|
2022-07-21 11:58:50 +00:00
|
|
|
return {"error": "access denied"}
|
2022-12-23 07:43:19 +00:00
|
|
|
r.deletedAt = datetime.now(tz=timezone.utc)
|
2022-07-21 11:58:50 +00:00
|
|
|
session.commit()
|
2023-01-17 19:56:48 +00:00
|
|
|
return {
|
|
|
|
"reaction": r
|
|
|
|
}
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-11-28 20:29:02 +00:00
|
|
|
|
2022-11-23 02:05:34 +00:00
|
|
|
@query.field("loadReactionsBy")
|
|
|
|
async def load_reactions_by(_, _info, by, limit=50, offset=0):
|
|
|
|
"""
|
|
|
|
:param by: {
|
|
|
|
:shout - filter by slug
|
2023-02-12 03:27:55 +00:00
|
|
|
:shouts - filer by shout slug list
|
2022-11-23 02:05:34 +00:00
|
|
|
:createdBy - to filter by author
|
|
|
|
:topic - to filter by topic
|
|
|
|
:search - to search by reactions' body
|
|
|
|
:comment - true if body.length > 0
|
|
|
|
:days - a number of days ago
|
|
|
|
:sort - a fieldname to sort desc by default
|
|
|
|
}
|
|
|
|
:param limit: int amount of shouts
|
|
|
|
:param offset: int offset in this order
|
|
|
|
:return: Reaction[]
|
|
|
|
"""
|
|
|
|
|
|
|
|
q = select(
|
2022-12-06 16:00:19 +00:00
|
|
|
Reaction, User, Shout
|
2022-11-27 08:19:38 +00:00
|
|
|
).join(
|
2022-12-06 16:00:19 +00:00
|
|
|
User, Reaction.createdBy == User.id
|
2022-11-27 08:19:38 +00:00
|
|
|
).join(
|
2022-12-06 16:00:19 +00:00
|
|
|
Shout, Reaction.shout == Shout.id
|
2022-11-27 08:19:38 +00:00
|
|
|
)
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-11-22 07:29:54 +00:00
|
|
|
if by.get("shout"):
|
2022-12-06 16:00:19 +00:00
|
|
|
q = q.filter(Shout.slug == by["shout"])
|
2022-11-22 15:32:58 +00:00
|
|
|
elif by.get("shouts"):
|
2022-12-06 16:00:19 +00:00
|
|
|
q = q.filter(Shout.shout.in_(by["shouts"]))
|
|
|
|
|
2022-11-22 15:32:58 +00:00
|
|
|
if by.get("createdBy"):
|
2022-12-06 16:00:19 +00:00
|
|
|
q = q.filter(User.slug == by.get("createdBy"))
|
|
|
|
|
2022-11-22 15:32:58 +00:00
|
|
|
if by.get("topic"):
|
2022-11-29 12:36:46 +00:00
|
|
|
# TODO: check
|
2022-11-22 15:32:58 +00:00
|
|
|
q = q.filter(Shout.topics.contains(by["topic"]))
|
2022-12-06 16:00:19 +00:00
|
|
|
|
2022-11-22 18:48:28 +00:00
|
|
|
if by.get("comment"):
|
|
|
|
q = q.filter(func.length(Reaction.body) > 0)
|
2022-12-06 16:00:19 +00:00
|
|
|
|
2022-11-29 12:36:46 +00:00
|
|
|
if len(by.get('search', '')) > 2:
|
2022-11-22 18:48:28 +00:00
|
|
|
q = q.filter(Reaction.body.ilike(f'%{by["body"]}%'))
|
2022-12-06 16:00:19 +00:00
|
|
|
|
2022-11-22 15:32:58 +00:00
|
|
|
if by.get("days"):
|
2022-11-23 14:09:35 +00:00
|
|
|
after = datetime.now(tz=timezone.utc) - timedelta(days=int(by["days"]) or 30)
|
2022-11-22 18:48:28 +00:00
|
|
|
q = q.filter(Reaction.createdAt > after)
|
2022-11-28 13:37:05 +00:00
|
|
|
|
2022-11-22 15:32:58 +00:00
|
|
|
order_way = asc if by.get("sort", "").startswith("-") else desc
|
2022-12-02 11:06:20 +00:00
|
|
|
order_field = by.get("sort", "").replace('-', '') or Reaction.createdAt
|
2022-11-28 13:37:05 +00:00
|
|
|
|
2022-11-22 15:32:58 +00:00
|
|
|
q = q.group_by(
|
2022-12-06 16:00:19 +00:00
|
|
|
Reaction.id, User.id, Shout.id
|
2022-11-22 15:32:58 +00:00
|
|
|
).order_by(
|
|
|
|
order_way(order_field)
|
|
|
|
)
|
2022-11-28 13:37:05 +00:00
|
|
|
|
2022-11-28 22:16:39 +00:00
|
|
|
q = add_reaction_stat_columns(q)
|
2022-11-28 13:37:05 +00:00
|
|
|
|
2022-11-23 02:05:34 +00:00
|
|
|
q = q.where(Reaction.deletedAt.is_(None))
|
2022-11-22 07:29:54 +00:00
|
|
|
q = q.limit(limit).offset(offset)
|
2022-11-28 08:47:39 +00:00
|
|
|
reactions = []
|
2022-11-28 13:37:05 +00:00
|
|
|
|
2022-08-13 09:48:07 +00:00
|
|
|
with local_session() as session:
|
2022-11-28 22:16:39 +00:00
|
|
|
for [reaction, user, shout, reacted_stat, commented_stat, rating_stat] in session.execute(q):
|
2022-11-28 13:15:52 +00:00
|
|
|
reaction.createdBy = user
|
2022-11-28 08:47:39 +00:00
|
|
|
reaction.shout = shout
|
|
|
|
reaction.stat = {
|
2022-11-28 22:16:39 +00:00
|
|
|
"rating": rating_stat,
|
|
|
|
"commented": commented_stat,
|
|
|
|
"reacted": reacted_stat
|
2022-11-28 08:47:39 +00:00
|
|
|
}
|
2023-02-12 03:27:55 +00:00
|
|
|
|
|
|
|
reaction.kind = reaction.kind.name
|
|
|
|
|
2022-11-28 08:47:39 +00:00
|
|
|
reactions.append(reaction)
|
|
|
|
|
2022-11-28 22:16:39 +00:00
|
|
|
# ?
|
2022-11-28 08:47:39 +00:00
|
|
|
if by.get("stat"):
|
|
|
|
reactions.sort(lambda r: r.stat.get(by["stat"]) or r.createdAt)
|
2022-11-23 02:05:34 +00:00
|
|
|
|
|
|
|
return reactions
|