get_my_rates_comments-fix
All checks were successful
Deploy on push / deploy (push) Successful in 55s

This commit is contained in:
Untone 2025-02-04 02:53:01 +03:00
parent 40b4703b1a
commit 56db33d7f1
7 changed files with 58 additions and 44 deletions

View File

@ -2,6 +2,8 @@
- `Reaction.deleted_at` filter on `update_reaction` resolver added - `Reaction.deleted_at` filter on `update_reaction` resolver added
- `triggers` module updated with `after_shout_handler`, `after_reaction_handler` for cache revalidation - `triggers` module updated with `after_shout_handler`, `after_reaction_handler` for cache revalidation
- `after_shout_handler`, `after_reaction_handler` now also handle `deleted_at` field - `after_shout_handler`, `after_reaction_handler` now also handle `deleted_at` field
- `get_cached_topic_followers` fixed
- `get_my_rates_comments` fixed
#### [0.4.7] #### [0.4.7]
- `get_my_rates_shouts` resolver added with: - `get_my_rates_shouts` resolver added with:

8
cache/cache.py vendored
View File

@ -175,10 +175,12 @@ async def get_cached_topic_followers(topic_id: int):
# Если данные не найдены в кеше, загрузка из базы данных # Если данные не найдены в кеше, загрузка из базы данных
with local_session() as session: with local_session() as session:
result = session.query(Author.id)\ result = (
.join(TopicFollower, TopicFollower.follower == Author.id)\ session.query(Author.id)
.filter(TopicFollower.topic == topic_id)\ .join(TopicFollower, TopicFollower.follower == Author.id)
.filter(TopicFollower.topic == topic_id)
.all() .all()
)
followers_ids = [f[0] for f in result] followers_ids = [f[0] for f in result]
# Кэширование результатов # Кэширование результатов

12
cache/triggers.py vendored
View File

@ -5,8 +5,8 @@ from orm.author import Author, AuthorFollower
from orm.reaction import Reaction, ReactionKind from orm.reaction import Reaction, ReactionKind
from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from utils.logger import root_logger as logger
from services.db import local_session from services.db import local_session
from utils.logger import root_logger as logger
def mark_for_revalidation(entity, *args): def mark_for_revalidation(entity, *args):
@ -86,11 +86,11 @@ def after_reaction_handler(mapper, connection, target):
if is_comment: if is_comment:
# Для комментариев обновляем также авторов и темы # Для комментариев обновляем также авторов и темы
with local_session() as session: with local_session() as session:
shout = session.query(Shout).filter( shout = (
Shout.id == shout_id, session.query(Shout)
Shout.published_at.is_not(None), .filter(Shout.id == shout_id, Shout.published_at.is_not(None), Shout.deleted_at.is_(None))
Shout.deleted_at.is_(None) .first()
).first() )
if shout: if shout:
for author in shout.authors: for author in shout.authors:

View File

@ -473,23 +473,23 @@ async def update_shout(_, info, shout_id: int, shout_input=None, publish=False):
shout_dict = shout_with_relations.dict() shout_dict = shout_with_relations.dict()
# Явно добавляем связанные данные # Явно добавляем связанные данные
shout_dict["topics"] = [ shout_dict["topics"] = (
{ [
"id": topic.id, {"id": topic.id, "slug": topic.slug, "title": topic.title}
"slug": topic.slug,
"title": topic.title
}
for topic in shout_with_relations.topics for topic in shout_with_relations.topics
] if shout_with_relations.topics else [] ]
if shout_with_relations.topics
else []
)
shout_dict["authors"] = [ shout_dict["authors"] = (
{ [
"id": author.id, {"id": author.id, "name": author.name, "slug": author.slug}
"name": author.name,
"slug": author.slug
}
for author in shout_with_relations.authors for author in shout_with_relations.authors
] if shout_with_relations.authors else [] ]
if shout_with_relations.authors
else []
)
logger.info(f"Final shout data with relations: {shout_dict}") logger.info(f"Final shout data with relations: {shout_dict}")
return {"shout": shout_dict, "error": None} return {"shout": shout_dict, "error": None}

View File

@ -15,11 +15,21 @@ from utils.logger import root_logger as logger
async def get_my_rates_comments(_, info, comments: list[int]) -> list[dict]: async def get_my_rates_comments(_, info, comments: list[int]) -> list[dict]:
""" """
Получение реакций пользователя на комментарии Получение реакций пользователя на комментарии
Args:
info: Контекст запроса
comments: Список ID комментариев
Returns:
list[dict]: Список словарей с реакциями пользователя на комментарии
Каждый словарь содержит:
- comment_id: ID комментария
- my_rate: Тип реакции (LIKE/DISLIKE)
""" """
author_dict = info.context.get("author") if info.context else None author_dict = info.context.get("author") if info.context else None
author_id = author_dict.get("id") if author_dict else None author_id = author_dict.get("id") if author_dict else None
if not author_id: if not author_id:
return {"error": "Author not found"} return [] # Возвращаем пустой список вместо словаря с ошибкой
# Подзапрос для реакций текущего пользователя # Подзапрос для реакций текущего пользователя
rated_query = ( rated_query = (

View File

@ -208,7 +208,7 @@ def get_author_shouts_stat(author_id: int) -> int:
and_( and_(
aliased_shout_author.author == author_id, aliased_shout_author.author == author_id,
aliased_shout.published_at.is_not(None), aliased_shout.published_at.is_not(None),
aliased_shout.deleted_at.is_(None) # Добавляем проверку на удаление aliased_shout.deleted_at.is_(None), # Добавляем проверку на удаление
) )
) )
) )