2025-06-01 23:56:11 +00:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from graphql import GraphQLResolveInfo
|
2024-04-08 07:38:58 +00:00
|
|
|
|
from sqlalchemy import and_, case, func, select, true
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from sqlalchemy.orm import Session, aliased
|
2024-02-24 18:45:38 +00:00
|
|
|
|
|
2025-05-16 06:23:48 +00:00
|
|
|
|
from auth.orm import Author, AuthorRating
|
2024-02-24 18:45:38 +00:00
|
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2025-06-01 23:56:11 +00:00
|
|
|
|
from orm.shout import Shout, ShoutAuthor
|
2024-02-24 18:45:38 +00:00
|
|
|
|
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
|
2024-11-18 19:16:42 +00:00
|
|
|
|
from utils.logger import root_logger as logger
|
2024-11-18 08:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("get_my_rates_comments")
|
|
|
|
|
@login_required
|
2025-06-01 23:56:11 +00:00
|
|
|
|
async def get_my_rates_comments(_: None, info: GraphQLResolveInfo, comments: list[int]) -> list[dict]:
|
2024-11-18 08:31:19 +00:00
|
|
|
|
"""
|
|
|
|
|
Получение реакций пользователя на комментарии
|
2025-02-03 23:53:01 +00:00
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
info: Контекст запроса
|
|
|
|
|
comments: Список ID комментариев
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list[dict]: Список словарей с реакциями пользователя на комментарии
|
|
|
|
|
Каждый словарь содержит:
|
|
|
|
|
- comment_id: ID комментария
|
|
|
|
|
- my_rate: Тип реакции (LIKE/DISLIKE)
|
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:
|
2025-02-03 23:53:01 +00:00
|
|
|
|
return [] # Возвращаем пустой список вместо словаря с ошибкой
|
2024-11-18 08:31:19 +00:00
|
|
|
|
|
|
|
|
|
# Подзапрос для реакций текущего пользователя
|
|
|
|
|
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.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)
|
|
|
|
|
)
|
|
|
|
|
with local_session() as session:
|
|
|
|
|
comments_result = session.execute(rated_query).all()
|
2025-06-01 23:56:11 +00:00
|
|
|
|
# For each row, we need to extract the Reaction object and its attributes
|
|
|
|
|
return [{"comment_id": reaction.id, "my_rate": reaction.kind} for (reaction,) in comments_result]
|
2024-11-18 08:31:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("get_my_rates_shouts")
|
|
|
|
|
@login_required
|
2025-06-01 23:56:11 +00:00
|
|
|
|
async def get_my_rates_shouts(_: None, info: GraphQLResolveInfo, shouts: list[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
|
2024-12-11 20:02:14 +00:00
|
|
|
|
|
2024-11-18 08:31:19 +00:00
|
|
|
|
if not author_id:
|
2024-11-18 19:16:42 +00:00
|
|
|
|
return []
|
2024-11-18 08:31:19 +00:00
|
|
|
|
|
|
|
|
|
with local_session() as session:
|
2024-11-18 19:16:42 +00:00
|
|
|
|
try:
|
2024-11-18 19:21:15 +00:00
|
|
|
|
stmt = (
|
2024-12-11 20:02:14 +00:00
|
|
|
|
select(Reaction)
|
|
|
|
|
.where(
|
2024-11-18 19:16:42 +00:00
|
|
|
|
and_(
|
|
|
|
|
Reaction.shout.in_(shouts),
|
|
|
|
|
Reaction.reply_to.is_(None),
|
|
|
|
|
Reaction.created_by == author_id,
|
|
|
|
|
Reaction.deleted_at.is_(None),
|
2024-12-11 20:02:14 +00:00
|
|
|
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
2024-11-18 19:16:42 +00:00
|
|
|
|
)
|
2024-12-11 20:02:14 +00:00
|
|
|
|
)
|
|
|
|
|
.order_by(Reaction.shout, Reaction.created_at.desc())
|
|
|
|
|
.distinct(Reaction.shout)
|
2024-11-18 19:21:15 +00:00
|
|
|
|
)
|
2024-12-11 20:02:14 +00:00
|
|
|
|
|
2024-11-18 19:21:15 +00:00
|
|
|
|
result = session.execute(stmt).all()
|
2024-11-18 19:10:25 +00:00
|
|
|
|
|
2024-11-18 19:16:42 +00:00
|
|
|
|
return [
|
2024-11-18 19:21:15 +00:00
|
|
|
|
{
|
2025-06-01 23:56:11 +00:00
|
|
|
|
"shout_id": reaction.shout, # Получаем shout_id из объекта Reaction
|
|
|
|
|
"my_rate": reaction.kind, # Получаем kind (my_rate) из объекта Reaction
|
2024-11-18 19:21:15 +00:00
|
|
|
|
}
|
2025-06-01 23:56:11 +00:00
|
|
|
|
for (reaction,) in result
|
2024-11-18 19:16:42 +00:00
|
|
|
|
]
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Error in get_my_rates_shouts: {e}")
|
|
|
|
|
return []
|
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
|
2025-06-01 23:56:11 +00:00
|
|
|
|
async def rate_author(_: None, info: GraphQLResolveInfo, rated_slug: str, value: int) -> dict:
|
2024-04-19 15:22:07 +00:00
|
|
|
|
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:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
rating = (
|
2024-02-24 18:45:38 +00:00
|
|
|
|
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:
|
2025-06-01 23:56:11 +00:00
|
|
|
|
rating.plus = value > 0 # type: ignore[assignment]
|
2024-02-24 18:45:38 +00:00
|
|
|
|
session.add(rating)
|
|
|
|
|
session.commit()
|
|
|
|
|
return {}
|
2025-06-01 23:56:11 +00:00
|
|
|
|
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}
|
2024-02-24 18:45:38 +00:00
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def count_author_comments_rating(session: Session, author_id: int) -> 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_replies_rating(session: Session, author_id: int) -> int:
|
2024-02-24 18:45:38 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def count_author_shouts_rating(session: Session, author_id: int) -> int:
|
2024-02-24 18:45:38 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def get_author_rating_old(session: Session, author: Author) -> dict[str, int]:
|
2024-02-25 08:27:08 +00:00
|
|
|
|
likes_count = (
|
2025-05-29 09:37:39 +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 = (
|
2025-06-01 23:56:11 +00:00
|
|
|
|
session.query(AuthorRating).filter(and_(AuthorRating.author == author.id, AuthorRating.plus.is_(False))).count()
|
2024-02-25 08:27:08 +00:00
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
rating = likes_count - dislikes_count
|
|
|
|
|
return {"rating": rating, "likes": likes_count, "dislikes": dislikes_count}
|
2024-03-29 11:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def get_author_rating_shouts(session: Session, author: Author) -> int:
|
2024-03-29 11:44:44 +00:00
|
|
|
|
q = (
|
|
|
|
|
select(
|
2025-06-01 23:56:11 +00:00
|
|
|
|
Reaction.shout,
|
|
|
|
|
Reaction.plus,
|
2024-03-29 11:44:44 +00:00
|
|
|
|
)
|
|
|
|
|
.select_from(Reaction)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
.join(ShoutAuthor, Reaction.shout == ShoutAuthor.shout)
|
|
|
|
|
.where(
|
2024-03-29 11:44:44 +00:00
|
|
|
|
and_(
|
2025-06-01 23:56:11 +00:00
|
|
|
|
ShoutAuthor.author == author.id,
|
|
|
|
|
Reaction.kind == "RATING",
|
2024-03-29 11:44:44 +00:00
|
|
|
|
Reaction.deleted_at.is_(None),
|
2025-06-01 23:56:11 +00:00
|
|
|
|
)
|
2024-03-29 11:44:44 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
|
|
|
|
|
results = session.execute(q)
|
|
|
|
|
rating = 0
|
|
|
|
|
for row in results:
|
|
|
|
|
rating += 1 if row[1] else -1
|
|
|
|
|
|
|
|
|
|
return rating
|
2024-03-28 21:29:28 +00:00
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def get_author_rating_comments(session: Session, author: Author) -> int:
|
2024-03-29 11:44:44 +00:00
|
|
|
|
replied_comment = aliased(Reaction)
|
|
|
|
|
q = (
|
|
|
|
|
select(
|
2025-06-01 23:56:11 +00:00
|
|
|
|
Reaction.id,
|
|
|
|
|
Reaction.plus,
|
2024-03-29 11:44:44 +00:00
|
|
|
|
)
|
|
|
|
|
.select_from(Reaction)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
.outerjoin(replied_comment, Reaction.reply_to == replied_comment.id)
|
|
|
|
|
.join(Shout, Reaction.shout == Shout.id)
|
|
|
|
|
.join(ShoutAuthor, Shout.id == ShoutAuthor.shout)
|
|
|
|
|
.where(
|
2024-03-29 11:44:44 +00:00
|
|
|
|
and_(
|
2025-06-01 23:56:11 +00:00
|
|
|
|
ShoutAuthor.author == author.id,
|
|
|
|
|
Reaction.kind == "RATING",
|
|
|
|
|
Reaction.created_by != author.id,
|
2024-03-29 11:44:44 +00:00
|
|
|
|
Reaction.deleted_at.is_(None),
|
2025-06-01 23:56:11 +00:00
|
|
|
|
)
|
2024-03-29 11:44:44 +00:00
|
|
|
|
)
|
|
|
|
|
)
|
2025-06-01 23:56:11 +00:00
|
|
|
|
|
|
|
|
|
results = session.execute(q)
|
|
|
|
|
rating = 0
|
|
|
|
|
for row in results:
|
|
|
|
|
rating += 1 if row[1] else -1
|
|
|
|
|
|
|
|
|
|
return rating
|
2024-03-29 11:44:44 +00:00
|
|
|
|
|
|
|
|
|
|
2025-06-01 23:56:11 +00:00
|
|
|
|
def add_author_rating_columns(q: Any, group_list: list[Any]) -> Any:
|
2024-03-29 11:44:44 +00:00
|
|
|
|
# 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
|