following-debug
All checks were successful
Deploy on push / deploy (push) Successful in 6s

This commit is contained in:
Untone 2024-11-02 11:35:02 +03:00
parent 65bd2ef9cf
commit 0eb95e238b

View File

@ -19,101 +19,136 @@ from services.auth import login_required
from services.db import local_session from services.db import local_session
from services.notify import notify_follower from services.notify import notify_follower
from services.schema import mutation, query from services.schema import mutation, query
from utils.logger import root_logger as logger
@mutation.field("follow") @mutation.field("follow")
@login_required @login_required
async def follow(_, info, what, slug): async def follow(_, info, what, slug):
logger.debug("Начало выполнения функции 'follow'")
user_id = info.context.get("user_id") user_id = info.context.get("user_id")
follower_dict = info.context.get("author") follower_dict = info.context.get("author")
logger.debug(f"user_id: {user_id}, follower_dict: {follower_dict}")
if not user_id or not follower_dict: if not user_id or not follower_dict:
logger.warning("Неавторизованный доступ при попытке следования")
return {"error": "unauthorized"} return {"error": "unauthorized"}
follower_id = follower_dict.get("id") follower_id = follower_dict.get("id")
logger.debug(f"follower_id: {follower_id}")
entity_classes = { entity_classes = {
"AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author), "AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author),
"TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic), "TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic),
"COMMUNITY": (Community, CommunityFollower, None, None), # No cache methods provided for community "COMMUNITY": (Community, CommunityFollower, None, None), # Нет методов кэша для сообщества
"SHOUT": (Shout, ShoutReactionsFollower, None, None), # No cache methods provided for shout "SHOUT": (Shout, ShoutReactionsFollower, None, None), # Нет методов кэша для shout
} }
if what not in entity_classes: if what not in entity_classes:
logger.error(f"Неверный тип для следования: {what}")
return {"error": "invalid follow type"} return {"error": "invalid follow type"}
entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what] entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what]
entity_type = what.lower() entity_type = what.lower()
logger.debug(f"entity_class: {entity_class}, follower_class: {follower_class}, entity_type: {entity_type}")
entity_id = None entity_id = None
entity_dict = None entity_dict = None
try: try:
# Fetch entity id from the database logger.debug("Попытка получить сущность из базы данных")
with local_session() as session: with local_session() as session:
entity_query = select(entity_class).filter(entity_class.slug == slug) entity_query = select(entity_class).filter(entity_class.slug == slug)
[entity] = get_with_stat(entity_query) entities = get_with_stat(entity_query)
logger.debug(f"Полученные сущности: {entities}")
[entity] = entities
if not entity: if not entity:
logger.warning(f"{what.lower()} не найден по slug: {slug}")
return {"error": f"{what.lower()} not found"} return {"error": f"{what.lower()} not found"}
entity_id = entity.id entity_id = entity.id
entity_dict = entity.dict() entity_dict = entity.dict()
logger.debug(f"entity_id: {entity_id}, entity_dict: {entity_dict}")
if entity_id: if entity_id:
# Update database logger.debug("Попытка добавить запись в базу данных")
with local_session() as session: with local_session() as session:
sub = follower_class(follower=follower_id, **{entity_type: entity_id}) sub = follower_class(follower=follower_id, **{entity_type: entity_id})
logger.debug(f"Создан объект подписки: {sub}")
session.add(sub) session.add(sub)
session.commit() session.commit()
logger.info(f"Пользователь {follower_id} подписался на {what.lower()} с ID {entity_id}")
follows = None follows = None
# Update cache # Обновление кэша
if cache_method: if cache_method:
logger.debug("Обновление кэша")
await cache_method(entity_dict) await cache_method(entity_dict)
if get_cached_follows_method: if get_cached_follows_method:
logger.debug("Получение обновленных подписок из кэша")
follows = await get_cached_follows_method(follower_id) follows = await get_cached_follows_method(follower_id)
logger.debug(f"Текущие подписки: {follows}")
# Notify author (only for AUTHOR type) # Уведомление автора (только для типа AUTHOR)
if what == "AUTHOR": if what == "AUTHOR":
logger.debug("Отправка уведомления автору о подписке")
await notify_follower(follower=follower_dict, author=entity_id, action="follow") await notify_follower(follower=follower_dict, author=entity_id, action="follow")
except Exception as exc: except Exception as exc:
logger.exception("Произошла ошибка в функции 'follow'")
return {"error": str(exc)} return {"error": str(exc)}
logger.debug(f"Функция 'follow' завершена успешно с результатом: {what.lower()}s={follows}")
return {f"{what.lower()}s": follows} return {f"{what.lower()}s": follows}
@mutation.field("unfollow") @mutation.field("unfollow")
@login_required @login_required
async def unfollow(_, info, what, slug): async def unfollow(_, info, what, slug):
logger.debug("Начало выполнения функции 'unfollow'")
user_id = info.context.get("user_id") user_id = info.context.get("user_id")
follower_dict = info.context.get("author") follower_dict = info.context.get("author")
logger.debug(f"user_id: {user_id}, follower_dict: {follower_dict}")
if not user_id or not follower_dict: if not user_id or not follower_dict:
logger.warning("Неавторизованный доступ при попытке отписаться")
return {"error": "unauthorized"} return {"error": "unauthorized"}
follower_id = follower_dict.get("id") follower_id = follower_dict.get("id")
logger.debug(f"follower_id: {follower_id}")
entity_classes = { entity_classes = {
"AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author), "AUTHOR": (Author, AuthorFollower, get_cached_follower_authors, cache_author),
"TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic), "TOPIC": (Topic, TopicFollower, get_cached_follower_topics, cache_topic),
"COMMUNITY": (Community, CommunityFollower, None, None), # No cache methods provided for community "COMMUNITY": (Community, CommunityFollower, None, None), # Нет методов кэша для сообщества
"SHOUT": ( "SHOUT": (
Shout, Shout,
ShoutReactionsFollower, ShoutReactionsFollower,
None, None,
), # No cache methods provided for shout ), # Нет методов кэша для shout
} }
if what not in entity_classes: if what not in entity_classes:
logger.error(f"Неверный тип для отписки: {what}")
return {"error": "invalid unfollow type"} return {"error": "invalid unfollow type"}
entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what] entity_class, follower_class, get_cached_follows_method, cache_method = entity_classes[what]
entity_type = what.lower() entity_type = what.lower()
logger.debug(f"entity_class: {entity_class}, follower_class: {follower_class}, entity_type: {entity_type}")
entity_id = None entity_id = None
follows = [] follows = []
error = None error = None
try: try:
logger.debug("Попытка получить сущность из базы данных")
with local_session() as session: with local_session() as session:
entity = session.query(entity_class).filter(entity_class.slug == slug).first() entity = session.query(entity_class).filter(entity_class.slug == slug).first()
logger.debug(f"Полученная сущность: {entity}")
if not entity: if not entity:
logger.warning(f"{what.lower()} не найден по slug: {slug}")
return {"error": f"{what.lower()} not found"} return {"error": f"{what.lower()} not found"}
entity_id = entity.id entity_id = entity.id
logger.debug(f"entity_id: {entity_id}")
sub = ( sub = (
session.query(follower_class) session.query(follower_class)
@ -125,37 +160,60 @@ async def unfollow(_, info, what, slug):
) )
.first() .first()
) )
logger.debug(f"Найдена подписка для удаления: {sub}")
if sub: if sub:
session.delete(sub) session.delete(sub)
session.commit() session.commit()
logger.info(f"Пользователь {follower_id} отписался от {what.lower()} с ID {entity_id}")
if cache_method: if cache_method:
logger.debug("Обновление кэша после отписки")
await cache_method(entity.dict()) await cache_method(entity.dict())
if get_cached_follows_method: if get_cached_follows_method:
logger.debug("Получение обновленных подписок из кэша")
follows = await get_cached_follows_method(follower_id) follows = await get_cached_follows_method(follower_id)
logger.debug(f"Текущие подписки: {follows}")
if what == "AUTHOR": if what == "AUTHOR":
logger.debug("Отправка уведомления автору об отписке")
await notify_follower(follower=follower_dict, author=entity_id, action="unfollow") await notify_follower(follower=follower_dict, author=entity_id, action="unfollow")
except Exception as exc: except Exception as exc:
logger.exception("Произошла ошибка в функции 'unfollow'")
import traceback
traceback.print_exc()
return {"error": str(exc)} return {"error": str(exc)}
logger.debug(f"Функция 'unfollow' завершена успешно с результатом: {entity_type}s={follows}, error={error}")
return {f"{entity_type}s": follows, "error": error} return {f"{entity_type}s": follows, "error": error}
@query.field("get_shout_followers") @query.field("get_shout_followers")
def get_shout_followers(_, _info, slug: str = "", shout_id: int | None = None) -> List[Author]: def get_shout_followers(_, _info, slug: str = "", shout_id: int | None = None) -> List[Author]:
logger.debug("Начало выполнения функции 'get_shout_followers'")
followers = [] followers = []
try:
with local_session() as session: with local_session() as session:
shout = None shout = None
if slug: if slug:
shout = session.query(Shout).filter(Shout.slug == slug).first() shout = session.query(Shout).filter(Shout.slug == slug).first()
logger.debug(f"Найден shout по slug: {slug} -> {shout}")
elif shout_id: elif shout_id:
shout = session.query(Shout).filter(Shout.id == shout_id).first() shout = session.query(Shout).filter(Shout.id == shout_id).first()
logger.debug(f"Найден shout по ID: {shout_id} -> {shout}")
if shout: if shout:
reactions = session.query(Reaction).filter(Reaction.shout == shout.id).all() reactions = session.query(Reaction).filter(Reaction.shout == shout.id).all()
logger.debug(f"Полученные реакции для shout ID {shout.id}: {reactions}")
for r in reactions: for r in reactions:
followers.append(r.created_by) followers.append(r.created_by)
logger.debug(f"Добавлен follower: {r.created_by}")
except Exception as _exc:
import traceback
traceback.print_exc()
logger.exception("Произошла ошибка в функции 'get_shout_followers'")
return []
logger.debug(f"Функция 'get_shout_followers' завершена с {len(followers)} подписчиками")
return followers return followers