2024-03-28 23:29:16 +00:00
|
|
|
from sqlalchemy import and_, func, case, true, select
|
2024-02-24 18:45:38 +00:00
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
|
|
|
from orm.author import AuthorRating, Author
|
|
|
|
from orm.reaction import Reaction, ReactionKind
|
|
|
|
from orm.shout import Shout
|
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
|
|
|
from services.schema import mutation
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field('rate_author')
|
|
|
|
@login_required
|
2024-02-26 09:14:08 +00:00
|
|
|
async def rate_author(_, info, rated_slug, value):
|
2024-02-24 18:45:38 +00:00
|
|
|
user_id = info.context['user_id']
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
|
|
|
rater = session.query(Author).filter(Author.slug == user_id).first()
|
|
|
|
if rater and rated_author:
|
|
|
|
rating: AuthorRating = (
|
|
|
|
session.query(AuthorRating)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
AuthorRating.rater == rater.id,
|
|
|
|
AuthorRating.author == rated_author.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if rating:
|
|
|
|
rating.plus = value > 0
|
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
rating = AuthorRating(
|
|
|
|
rater=rater.id, author=rated_author.id, plus=value > 0
|
|
|
|
)
|
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
|
|
|
except Exception as err:
|
|
|
|
return {'error': err}
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
def count_author_comments_rating(session, author_id) -> int:
|
|
|
|
replied_alias = aliased(Reaction)
|
|
|
|
replies_likes = (
|
|
|
|
session.query(replied_alias)
|
|
|
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
replied_alias.created_by == author_id,
|
|
|
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
|
|
|
.count()
|
|
|
|
) or 0
|
|
|
|
replies_dislikes = (
|
|
|
|
session.query(replied_alias)
|
|
|
|
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
replied_alias.created_by == author_id,
|
|
|
|
replied_alias.kind == ReactionKind.COMMENT.value,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
|
|
|
.count()
|
|
|
|
) or 0
|
|
|
|
|
|
|
|
return replies_likes - replies_dislikes
|
|
|
|
|
|
|
|
|
|
|
|
def count_author_shouts_rating(session, author_id) -> int:
|
|
|
|
shouts_likes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Shout.authors.any(id=author_id),
|
|
|
|
Reaction.kind == ReactionKind.LIKE.value,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
shouts_dislikes = (
|
|
|
|
session.query(Reaction, Shout)
|
|
|
|
.join(Shout, Shout.id == Reaction.shout)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
Shout.authors.any(id=author_id),
|
|
|
|
Reaction.kind == ReactionKind.DISLIKE.value,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.count()
|
|
|
|
or 0
|
|
|
|
)
|
|
|
|
return shouts_likes - shouts_dislikes
|
|
|
|
|
|
|
|
|
2024-03-29 11:44:44 +00:00
|
|
|
def get_author_rating_old(session, author: Author):
|
2024-02-25 08:27:08 +00:00
|
|
|
likes_count = (
|
|
|
|
session.query(AuthorRating)
|
|
|
|
.filter(and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True)))
|
|
|
|
.count()
|
|
|
|
)
|
|
|
|
dislikes_count = (
|
|
|
|
session.query(AuthorRating)
|
|
|
|
.filter(and_(AuthorRating.author == author.id, AuthorRating.plus.is_not(True)))
|
|
|
|
.count()
|
|
|
|
)
|
2024-03-29 11:44:44 +00:00
|
|
|
return likes_count - dislikes_count
|
|
|
|
|
|
|
|
|
|
|
|
def get_author_rating_shouts(session, author: Author) -> int:
|
|
|
|
q = (
|
|
|
|
select(
|
2024-04-08 06:17:05 +00:00
|
|
|
func.coalesce(
|
|
|
|
func.sum(
|
|
|
|
case(
|
|
|
|
(Reaction.kind == ReactionKind.LIKE.value, 1),
|
|
|
|
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
|
|
|
|
else_=0,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
0,
|
|
|
|
).label('shouts_rating')
|
2024-03-29 11:44:44 +00:00
|
|
|
)
|
|
|
|
.select_from(Reaction)
|
2024-04-08 06:17:05 +00:00
|
|
|
.outerjoin(Shout, Shout.authors.any(id=author.id))
|
2024-03-29 11:44:44 +00:00
|
|
|
.outerjoin(
|
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
Reaction.reply_to.is_(None),
|
|
|
|
Reaction.shout == Shout.id,
|
|
|
|
Reaction.deleted_at.is_(None),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
result = session.execute(q).scalar()
|
|
|
|
return result
|
2024-03-28 21:29:28 +00:00
|
|
|
|
|
|
|
|
2024-03-29 11:44:44 +00:00
|
|
|
def get_author_rating_comments(session, author: Author) -> int:
|
|
|
|
replied_comment = aliased(Reaction)
|
|
|
|
q = (
|
|
|
|
select(
|
2024-04-08 06:17:05 +00:00
|
|
|
func.coalesce(
|
|
|
|
func.sum(
|
|
|
|
case(
|
|
|
|
(Reaction.kind == ReactionKind.LIKE.value, 1),
|
|
|
|
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
|
|
|
|
else_=0,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
0,
|
|
|
|
).label('shouts_rating')
|
2024-03-29 11:44:44 +00:00
|
|
|
)
|
|
|
|
.select_from(Reaction)
|
|
|
|
.outerjoin(
|
|
|
|
Reaction,
|
|
|
|
and_(
|
|
|
|
replied_comment.kind == ReactionKind.COMMENT.value,
|
|
|
|
replied_comment.created_by == author.id,
|
2024-04-08 06:17:05 +00:00
|
|
|
Reaction.kind.in_(
|
|
|
|
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
|
|
|
|
),
|
2024-03-29 11:44:44 +00:00
|
|
|
Reaction.reply_to == replied_comment.id,
|
|
|
|
Reaction.deleted_at.is_(None),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
result = session.execute(q).scalar()
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def add_author_rating_columns(q, group_list):
|
|
|
|
# NOTE: method is not used
|
|
|
|
|
2024-03-28 21:29:28 +00:00
|
|
|
# old karma
|
|
|
|
q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id)
|
2024-04-08 06:17:05 +00:00
|
|
|
q = q.add_columns(
|
|
|
|
func.sum(case((AuthorRating.plus == true(), 1), else_=-1)).label('rating')
|
|
|
|
)
|
2024-03-28 21:29:28 +00:00
|
|
|
|
2024-03-28 23:15:38 +00:00
|
|
|
# by shouts rating
|
2024-03-28 23:56:25 +00:00
|
|
|
shout_reaction = aliased(Reaction)
|
2024-03-28 23:15:38 +00:00
|
|
|
shouts_rating_subq = (
|
|
|
|
select(
|
|
|
|
Author.id,
|
2024-04-08 06:17:05 +00:00
|
|
|
func.coalesce(
|
|
|
|
func.sum(
|
|
|
|
case(
|
|
|
|
(shout_reaction.kind == ReactionKind.LIKE.value, 1),
|
|
|
|
(shout_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
|
|
|
else_=0,
|
|
|
|
)
|
2024-03-28 23:15:38 +00:00
|
|
|
)
|
2024-04-08 06:17:05 +00:00
|
|
|
).label('shouts_rating'),
|
2024-03-28 21:29:28 +00:00
|
|
|
)
|
2024-03-28 23:56:25 +00:00
|
|
|
.select_from(shout_reaction)
|
2024-04-08 06:17:05 +00:00
|
|
|
.outerjoin(Shout, Shout.authors.any(id=Author.id))
|
2024-03-28 23:15:38 +00:00
|
|
|
.outerjoin(
|
2024-03-28 23:56:25 +00:00
|
|
|
shout_reaction,
|
2024-03-28 23:15:38 +00:00
|
|
|
and_(
|
2024-03-28 23:56:25 +00:00
|
|
|
shout_reaction.reply_to.is_(None),
|
|
|
|
shout_reaction.shout == Shout.id,
|
|
|
|
shout_reaction.deleted_at.is_(None),
|
2024-03-28 23:15:38 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.group_by(Author.id)
|
|
|
|
.subquery()
|
2024-03-28 21:29:28 +00:00
|
|
|
)
|
2024-03-28 23:15:38 +00:00
|
|
|
|
|
|
|
q = q.outerjoin(shouts_rating_subq, Author.id == shouts_rating_subq.c.id)
|
|
|
|
q = q.add_columns(shouts_rating_subq.c.shouts_rating)
|
|
|
|
group_list = [shouts_rating_subq.c.shouts_rating]
|
2024-03-28 21:29:28 +00:00
|
|
|
|
|
|
|
# by comments
|
|
|
|
replied_comment = aliased(Reaction)
|
2024-03-28 23:50:38 +00:00
|
|
|
reaction_2 = aliased(Reaction)
|
2024-04-08 06:17:05 +00:00
|
|
|
comments_subq = (
|
|
|
|
select(
|
|
|
|
Author.id,
|
|
|
|
func.coalesce(
|
|
|
|
func.sum(
|
|
|
|
case(
|
|
|
|
(reaction_2.kind == ReactionKind.LIKE.value, 1),
|
|
|
|
(reaction_2.kind == ReactionKind.DISLIKE.value, -1),
|
|
|
|
else_=0,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).label('comments_rating'),
|
2024-03-28 21:29:28 +00:00
|
|
|
)
|
2024-04-08 06:17:05 +00:00
|
|
|
.select_from(reaction_2)
|
|
|
|
.outerjoin(
|
|
|
|
replied_comment,
|
|
|
|
and_(
|
|
|
|
replied_comment.kind == ReactionKind.COMMENT.value,
|
|
|
|
replied_comment.created_by == Author.id,
|
|
|
|
reaction_2.kind.in_(
|
|
|
|
[ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]
|
|
|
|
),
|
|
|
|
reaction_2.reply_to == replied_comment.id,
|
|
|
|
reaction_2.deleted_at.is_(None),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.group_by(Author.id)
|
|
|
|
.subquery()
|
|
|
|
)
|
2024-03-28 21:29:28 +00:00
|
|
|
|
2024-03-28 23:31:59 +00:00
|
|
|
q = q.outerjoin(comments_subq, Author.id == comments_subq.c.id)
|
2024-03-28 23:45:23 +00:00
|
|
|
q = q.add_columns(comments_subq.c.comments_rating)
|
2024-03-28 23:29:16 +00:00
|
|
|
group_list.extend([comments_subq.c.comments_rating])
|
2024-03-28 21:29:28 +00:00
|
|
|
|
|
|
|
return q, group_list
|