2024-08-06 18:44:33 +00:00
|
|
|
from sqlalchemy import event
|
2024-08-09 06:37:06 +00:00
|
|
|
|
|
|
|
from cache.revalidator import revalidation_manager
|
2024-04-09 08:17:32 +00:00
|
|
|
from orm.author import Author, AuthorFollower
|
2025-02-03 20:16:50 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2024-08-07 06:51:09 +00:00
|
|
|
from orm.shout import Shout, ShoutAuthor, ShoutReactionsFollower
|
2024-04-19 15:22:07 +00:00
|
|
|
from orm.topic import Topic, TopicFollower
|
2024-08-07 05:57:56 +00:00
|
|
|
from utils.logger import root_logger as logger
|
2025-02-03 21:08:25 +00:00
|
|
|
from services.db import local_session
|
2024-04-09 08:17:32 +00:00
|
|
|
|
|
|
|
|
2024-08-07 06:51:09 +00:00
|
|
|
def mark_for_revalidation(entity, *args):
|
|
|
|
"""Отметка сущности для ревалидации."""
|
|
|
|
entity_type = (
|
|
|
|
"authors"
|
|
|
|
if isinstance(entity, Author)
|
|
|
|
else "topics"
|
|
|
|
if isinstance(entity, Topic)
|
|
|
|
else "reactions"
|
|
|
|
if isinstance(entity, Reaction)
|
|
|
|
else "shouts"
|
|
|
|
if isinstance(entity, Shout)
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
if entity_type:
|
|
|
|
revalidation_manager.mark_for_revalidation(entity.id, entity_type)
|
2024-08-06 18:44:33 +00:00
|
|
|
|
|
|
|
|
2024-08-07 06:51:09 +00:00
|
|
|
def after_follower_handler(mapper, connection, target, is_delete=False):
|
|
|
|
"""Обработчик добавления, обновления или удаления подписки."""
|
|
|
|
entity_type = None
|
2024-08-06 18:44:33 +00:00
|
|
|
if isinstance(target, AuthorFollower):
|
2024-08-07 06:51:09 +00:00
|
|
|
entity_type = "authors"
|
2024-08-06 18:44:33 +00:00
|
|
|
elif isinstance(target, TopicFollower):
|
2024-08-07 06:51:09 +00:00
|
|
|
entity_type = "topics"
|
|
|
|
elif isinstance(target, ShoutReactionsFollower):
|
|
|
|
entity_type = "shouts"
|
2024-08-06 18:44:33 +00:00
|
|
|
|
2024-08-07 06:51:09 +00:00
|
|
|
if entity_type:
|
|
|
|
revalidation_manager.mark_for_revalidation(
|
2024-11-02 08:40:02 +00:00
|
|
|
target.author if entity_type == "authors" else target.topic, entity_type
|
2024-08-07 06:51:09 +00:00
|
|
|
)
|
|
|
|
if not is_delete:
|
2024-11-02 08:40:02 +00:00
|
|
|
revalidation_manager.mark_for_revalidation(target.follower, "authors")
|
2024-08-06 15:53:25 +00:00
|
|
|
|
|
|
|
|
2025-02-03 20:16:50 +00:00
|
|
|
def after_shout_handler(mapper, connection, target):
|
|
|
|
"""Обработчик изменения статуса публикации"""
|
|
|
|
if not isinstance(target, Shout):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Проверяем изменение статуса публикации
|
|
|
|
was_published = target.published_at is not None and target.deleted_at is None
|
|
|
|
|
2025-02-03 20:22:45 +00:00
|
|
|
# Всегда обновляем счетчики для авторов и тем при любом изменении поста
|
|
|
|
for author in target.authors:
|
|
|
|
revalidation_manager.mark_for_revalidation(author.id, "authors")
|
|
|
|
|
|
|
|
for topic in target.topics:
|
|
|
|
revalidation_manager.mark_for_revalidation(topic.id, "topics")
|
|
|
|
|
|
|
|
# Обновляем сам пост
|
|
|
|
revalidation_manager.mark_for_revalidation(target.id, "shouts")
|
2025-02-03 20:16:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def after_reaction_handler(mapper, connection, target):
|
|
|
|
"""Обработчик для комментариев"""
|
2025-02-03 20:22:45 +00:00
|
|
|
if not isinstance(target, Reaction):
|
2025-02-03 20:16:50 +00:00
|
|
|
return
|
|
|
|
|
2025-02-03 20:22:45 +00:00
|
|
|
# Проверяем что это комментарий
|
|
|
|
is_comment = target.kind == ReactionKind.COMMENT.value
|
|
|
|
|
|
|
|
# Получаем связанный пост
|
2025-02-03 21:08:25 +00:00
|
|
|
shout_id = target.shout if isinstance(target.shout, int) else target.shout.id
|
|
|
|
if not shout_id:
|
2025-02-03 20:22:45 +00:00
|
|
|
return
|
2025-02-03 20:16:50 +00:00
|
|
|
|
2025-02-03 20:22:45 +00:00
|
|
|
# Обновляем счетчики для автора комментария
|
|
|
|
if target.created_by:
|
2025-02-03 21:01:54 +00:00
|
|
|
revalidation_manager.mark_for_revalidation(target.created_by, "authors")
|
2025-02-03 20:22:45 +00:00
|
|
|
|
2025-02-03 21:08:25 +00:00
|
|
|
# Обновляем счетчики для поста
|
|
|
|
revalidation_manager.mark_for_revalidation(shout_id, "shouts")
|
2025-02-03 20:22:45 +00:00
|
|
|
|
2025-02-03 21:08:25 +00:00
|
|
|
if is_comment:
|
|
|
|
# Для комментариев обновляем также авторов и темы
|
|
|
|
with local_session() as session:
|
|
|
|
shout = session.query(Shout).filter(
|
|
|
|
Shout.id == shout_id,
|
|
|
|
Shout.published_at.is_not(None),
|
|
|
|
Shout.deleted_at.is_(None)
|
|
|
|
).first()
|
2025-02-03 20:22:45 +00:00
|
|
|
|
2025-02-03 21:08:25 +00:00
|
|
|
if shout:
|
|
|
|
for author in shout.authors:
|
|
|
|
revalidation_manager.mark_for_revalidation(author.id, "authors")
|
|
|
|
|
|
|
|
for topic in shout.topics:
|
|
|
|
revalidation_manager.mark_for_revalidation(topic.id, "topics")
|
2025-02-03 20:16:50 +00:00
|
|
|
|
|
|
|
|
2024-08-06 18:44:33 +00:00
|
|
|
def events_register():
|
|
|
|
"""Регистрация обработчиков событий для всех сущностей."""
|
2024-08-07 06:51:09 +00:00
|
|
|
event.listen(ShoutAuthor, "after_insert", mark_for_revalidation)
|
|
|
|
event.listen(ShoutAuthor, "after_update", mark_for_revalidation)
|
|
|
|
event.listen(ShoutAuthor, "after_delete", mark_for_revalidation)
|
|
|
|
|
|
|
|
event.listen(AuthorFollower, "after_insert", after_follower_handler)
|
|
|
|
event.listen(AuthorFollower, "after_update", after_follower_handler)
|
|
|
|
event.listen(AuthorFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
|
|
|
|
|
|
|
event.listen(TopicFollower, "after_insert", after_follower_handler)
|
|
|
|
event.listen(TopicFollower, "after_update", after_follower_handler)
|
|
|
|
event.listen(TopicFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
|
|
|
|
|
|
|
event.listen(ShoutReactionsFollower, "after_insert", after_follower_handler)
|
|
|
|
event.listen(ShoutReactionsFollower, "after_update", after_follower_handler)
|
|
|
|
event.listen(ShoutReactionsFollower, "after_delete", lambda *args: after_follower_handler(*args, is_delete=True))
|
|
|
|
|
|
|
|
event.listen(Reaction, "after_update", mark_for_revalidation)
|
|
|
|
event.listen(Author, "after_update", mark_for_revalidation)
|
|
|
|
event.listen(Topic, "after_update", mark_for_revalidation)
|
2025-02-03 20:16:50 +00:00
|
|
|
event.listen(Shout, "after_update", after_shout_handler)
|
2025-02-03 20:22:45 +00:00
|
|
|
event.listen(Shout, "after_delete", after_shout_handler)
|
2025-02-03 20:16:50 +00:00
|
|
|
|
|
|
|
event.listen(Reaction, "after_insert", after_reaction_handler)
|
2025-02-03 20:22:45 +00:00
|
|
|
event.listen(Reaction, "after_update", after_reaction_handler)
|
2025-02-03 20:16:50 +00:00
|
|
|
event.listen(Reaction, "after_delete", after_reaction_handler)
|
2024-08-06 17:55:19 +00:00
|
|
|
|
|
|
|
logger.info("Event handlers registered successfully.")
|