2024-04-08 07:38:58 +00:00
|
|
|
from sqlalchemy import and_, case, func, select, true
|
2024-02-24 18:45:38 +00:00
|
|
|
from sqlalchemy.orm import aliased
|
|
|
|
|
2024-04-08 07:38:58 +00:00
|
|
|
from orm.author import Author, AuthorRating
|
2024-02-24 18:45:38 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
|
|
|
from orm.shout import Shout
|
|
|
|
from services.auth import login_required
|
|
|
|
from services.db import local_session
|
2024-11-18 08:31:19 +00:00
|
|
|
from services.schema import mutation, query
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("get_my_rates_comments")
|
|
|
|
@login_required
|
2024-11-18 19:05:45 +00:00
|
|
|
async def get_my_rates_comments(_, info, comments: list[int], shout: int) -> list[dict]:
|
2024-11-18 08:31:19 +00:00
|
|
|
"""
|
|
|
|
Получение реакций пользователя на комментарии
|
|
|
|
"""
|
|
|
|
author_dict = info.context.get("author") if info.context else None
|
|
|
|
author_id = author_dict.get("id") if author_dict else None
|
|
|
|
if not author_id:
|
|
|
|
return {"error": "Author not found"}
|
|
|
|
|
|
|
|
# Подзапрос для реакций текущего пользователя
|
|
|
|
rated_query = (
|
2024-11-18 10:14:32 +00:00
|
|
|
select(Reaction.id.label("comment_id"), Reaction.kind.label("my_rate"))
|
2024-11-18 08:31:19 +00:00
|
|
|
.where(
|
|
|
|
and_(
|
|
|
|
Reaction.shout == shout,
|
|
|
|
Reaction.reply_to.in_(comments),
|
|
|
|
Reaction.created_by == author_id,
|
|
|
|
Reaction.deleted_at.is_(None),
|
|
|
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.order_by(Reaction.shout, Reaction.created_at.desc())
|
|
|
|
.distinct(Reaction.shout)
|
|
|
|
.subquery()
|
|
|
|
)
|
|
|
|
with local_session() as session:
|
|
|
|
comments_result = session.execute(rated_query).all()
|
2024-11-18 10:14:32 +00:00
|
|
|
return [{"comment_id": row.comment_id, "my_rate": row.my_rate} for row in comments_result]
|
2024-11-18 08:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
@query.field("get_my_rates_shouts")
|
|
|
|
@login_required
|
2024-11-18 19:05:45 +00:00
|
|
|
async def get_my_rates_shouts(_, info, shouts):
|
2024-11-18 08:31:19 +00:00
|
|
|
"""
|
|
|
|
Получение реакций пользователя на публикации
|
|
|
|
"""
|
|
|
|
author_dict = info.context.get("author") if info.context else None
|
|
|
|
author_id = author_dict.get("id") if author_dict else None
|
|
|
|
if not author_id:
|
|
|
|
return {"error": "Author not found"}
|
|
|
|
|
|
|
|
with local_session() as session:
|
2024-11-18 19:10:25 +00:00
|
|
|
# Используем прямой запрос без подзапросов
|
|
|
|
result = session.execute(
|
|
|
|
select([
|
|
|
|
Reaction.shout.label("shout_id"),
|
|
|
|
Reaction.kind.label("my_rate")
|
|
|
|
]).where(
|
|
|
|
and_(
|
|
|
|
Reaction.shout.in_(shouts),
|
|
|
|
Reaction.reply_to.is_(None),
|
|
|
|
Reaction.created_by == author_id,
|
|
|
|
Reaction.deleted_at.is_(None),
|
|
|
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value])
|
|
|
|
)
|
|
|
|
).order_by(
|
|
|
|
Reaction.shout,
|
|
|
|
Reaction.created_at.desc()
|
|
|
|
).distinct(Reaction.shout)
|
|
|
|
).all()
|
|
|
|
|
|
|
|
return [
|
|
|
|
{"shout_id": row.shout_id, "my_rate": row.my_rate}
|
|
|
|
for row in result
|
|
|
|
]
|
2024-02-24 18:45:38 +00:00
|
|
|
|
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
@mutation.field("rate_author")
|
2024-02-24 18:45:38 +00:00
|
|
|
@login_required
|
2024-02-26 09:14:08 +00:00
|
|
|
async def rate_author(_, info, rated_slug, value):
|
2024-04-19 15:22:07 +00:00
|
|
|
info.context["user_id"]
|
|
|
|
rater_id = info.context.get("author", {}).get("id")
|
2024-02-24 18:45:38 +00:00
|
|
|
with local_session() as session:
|
2024-04-19 15:22:07 +00:00
|
|
|
rater_id = int(rater_id)
|
2024-02-24 18:45:38 +00:00
|
|
|
rated_author = session.query(Author).filter(Author.slug == rated_slug).first()
|
2024-04-19 15:22:07 +00:00
|
|
|
if rater_id and rated_author:
|
2024-02-24 18:45:38 +00:00
|
|
|
rating: AuthorRating = (
|
|
|
|
session.query(AuthorRating)
|
|
|
|
.filter(
|
|
|
|
and_(
|
2024-04-19 15:22:07 +00:00
|
|
|
AuthorRating.rater == rater_id,
|
2024-02-24 18:45:38 +00:00
|
|
|
AuthorRating.author == rated_author.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if rating:
|
|
|
|
rating.plus = value > 0
|
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
|
|
|
return {}
|
|
|
|
else:
|
|
|
|
try:
|
2024-05-30 04:12:00 +00:00
|
|
|
rating = AuthorRating(rater=rater_id, author=rated_author.id, plus=value > 0)
|
2024-02-24 18:45:38 +00:00
|
|
|
session.add(rating)
|
|
|
|
session.commit()
|
|
|
|
except Exception as err:
|
2024-04-17 15:32:23 +00:00
|
|
|
return {"error": err}
|
2024-02-24 18:45:38 +00:00
|
|
|
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 = (
|
2024-05-30 04:12:00 +00:00
|
|
|
session.query(AuthorRating).filter(and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True))).count()
|
2024-02-25 08:27:08 +00:00
|
|
|
)
|
|
|
|
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,
|
2024-04-17 15:32:23 +00:00
|
|
|
).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,
|
2024-04-17 15:32:23 +00:00
|
|
|
).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-05-30 04:12:00 +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-05-30 04:12:00 +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-10-15 08:12:09 +00:00
|
|
|
),
|
|
|
|
0,
|
2024-04-17 15:32:23 +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,
|
|
|
|
)
|
2024-10-15 08:12:09 +00:00
|
|
|
),
|
|
|
|
0,
|
2024-04-17 15:32:23 +00:00
|
|
|
).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,
|
2024-05-30 04:12:00 +00:00
|
|
|
reaction_2.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
2024-04-08 06:17:05 +00:00
|
|
|
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
|