diff --git a/resolvers/author.py b/resolvers/author.py index 7779dfa9..11aedfec 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -764,34 +764,23 @@ async def get_author( if "stat" in cached_author: author_dict["stat"] = cached_author["stat"] - # ВСЕГДА используем новый резолвер для получения полной статистики - try: - # Используем новый резолвер для получения полной статистики - filter_by: AuthorsBy = {} - if slug: - filter_by["slug"] = slug - elif author_id: - filter_by["id"] = author_id # author_id уже int + if not author_dict or not author_dict.get("stat"): + # Используем ту же логику что и в load_authors_by + try: + filter_by: AuthorsBy = {} + if slug: + filter_by["slug"] = slug + elif author_id: + filter_by["id"] = author_id - logger.debug(f"Calling get_authors_with_stats with filter: {filter_by}") - authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by) - - if authors_with_stats and len(authors_with_stats) > 0: - author_dict = authors_with_stats[0] - logger.debug(f"Got author with stats: shouts={author_dict.get('stat', {}).get('shouts', 'missing')}") - - # Фильтруем данные согласно правам доступа - if not is_admin: - # Убираем защищенные поля для не-админов - protected_fields = ["email", "phone"] - for field in protected_fields: - author_dict.pop(field, None) - - # Кэшируем полные данные для админов - _t = asyncio.create_task(cache_author(author_dict)) - else: - logger.warning(f"No author found with filter: {filter_by}") - # Fallback to basic author data without stats + authors_with_stats = await get_authors_with_stats(limit=1, offset=0, by=filter_by) + if authors_with_stats and len(authors_with_stats) > 0: + author_dict = authors_with_stats[0] + # Кэшируем полные данные + _t = asyncio.create_task(cache_author(author_dict)) + except Exception as e: + logger.error(f"Error getting author stats: {e}") + # Fallback к старому методу with local_session() as session: if slug: author = session.query(Author).filter_by(slug=slug).first() @@ -799,17 +788,6 @@ async def get_author( author = session.query(Author).filter_by(id=author_id).first() if author: author_dict = author.dict(is_admin) - logger.debug(f"Using fallback author dict: {author_dict.get('name', 'unknown')}") - except Exception as e: - logger.error(f"Error getting author stats: {e}") - # Fallback to basic author data without stats - with local_session() as session: - if slug: - author = session.query(Author).filter_by(slug=slug).first() - else: - author = session.query(Author).filter_by(id=author_id).first() - if author: - author_dict = author.dict(is_admin) except ValueError: pass except Exception as exc: diff --git a/resolvers/reaction.py b/resolvers/reaction.py index 8d6b1672..9e02ad1c 100644 --- a/resolvers/reaction.py +++ b/resolvers/reaction.py @@ -413,8 +413,14 @@ async def create_reaction(_: None, info: GraphQLResolveInfo, reaction: dict) -> shout = session.query(Shout).where(Shout.id == shout_id).first() if not shout: return {"error": "Shout not found"} + + # Получаем полного автора из БД вместо неполного из контекста + author = session.query(Author).where(Author.id == author_id).first() + if not author: + return {"error": "Author not found"} + rdict["shout"] = shout.dict() - rdict["created_by"] = author_dict + rdict["created_by"] = author.dict() return {"reaction": rdict} except Exception as e: traceback.print_exc() @@ -470,7 +476,10 @@ async def update_reaction(_: None, info: GraphQLResolveInfo, reaction: dict) -> await notify_reaction(r, "update") - return {"reaction": r.dict()} + # Включаем полную информацию об авторе в ответ + reaction_dict = r.dict() + reaction_dict["created_by"] = author.dict() + return {"reaction": reaction_dict} return {"error": "Reaction not found"} except Exception as e: logger.error(f"{type(e).__name__}: {e}") @@ -527,7 +536,13 @@ async def delete_reaction(_: None, info: GraphQLResolveInfo, reaction_id: int) - await notify_reaction(r, "delete") - return {"error": None, "reaction": r.dict()} + # Включаем полную информацию об авторе в ответ + reaction_dict = r.dict() + reaction_author: Author | None = session.query(Author).where(Author.id == r.created_by).first() + if reaction_author: + reaction_dict["created_by"] = reaction_author.dict() + + return {"error": None, "reaction": reaction_dict} except Exception as e: logger.error(f"{type(e).__name__}: {e}") return {"error": "Cannot delete reaction"}