From 2e1f413264d9dec2aa968ac67d702cab31ff5a77 Mon Sep 17 00:00:00 2001 From: lizerk Date: Wed, 11 Mar 2026 17:25:15 +0300 Subject: [PATCH] =?UTF-8?q?refactor(communications):=20=D0=A1=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D1=81=D1=82=D1=8C=20=D0=BE=D1=82=20notifications?= =?UTF-8?q?=20=D0=BE=D0=BF=D1=86=D0=B8=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: notifications теперь опциональная зависимость. Проблема: - communications имел жесткую зависимость от notifications модуля - from notifications.signals import notify - обязательный импорт - Нарушало независимость модуля Решение: - Импорт notifications обернут в try/except ImportError - Создана функция _send_notification() - обертка над notify.send() - Если notifications не установлен - уведомления тихо игнорируются - Модуль работает БЕЗ notifications (graceful degradation) Изменения: - notifications/handlers.py: * Опциональный импорт: try/except ImportError * Флаг NOTIFICATIONS_AVAILABLE для проверки доступности * Функция _send_notification() вместо прямых вызовов * 4 вызова notify.send() → _send_notification() Результат: ✅ communications работает БЕЗ notifications модуля ✅ Если notifications установлен - все работает как раньше ✅ Полная независимость для standalone использования ✅ System check: 0 errors Теперь можно использовать communications как: 1. С notifications - полная функциональность 2. Без notifications - только чаты без уведомлений 3. С собственной системой уведомлений - переопределить _send_notification() --- .../communications/notifications/handlers.py | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/communications/notifications/handlers.py b/backend/communications/notifications/handlers.py index 97712954..2da5b768 100644 --- a/backend/communications/notifications/handlers.py +++ b/backend/communications/notifications/handlers.py @@ -5,10 +5,20 @@ - get_users_with_notifications_enabled - проверка настроек уведомлений - notify_new_message - уведомление о новом сообщении (с упоминаниями и ответами) - notify_chat_added - уведомление о добавлении в чат + +ПРИМЕЧАНИЕ: Опциональная зависимость от notifications модуля. +Если notifications не установлен - уведомления просто не отправляются. """ from django.contrib.auth import get_user_model -from notifications.signals import notify + +# Опциональная зависимость от notifications (graceful degradation) +try: + from notifications.signals import notify + NOTIFICATIONS_AVAILABLE = True +except ImportError: + NOTIFICATIONS_AVAILABLE = False + notify = None from ..models import ChatUserSettings from .config import ( @@ -23,6 +33,16 @@ from .config import ( User = get_user_model() +def _send_notification(**kwargs): + """ + Обертка для отправки уведомлений. + Если notifications модуль не доступен - ничего не делает. + """ + if NOTIFICATIONS_AVAILABLE and notify: + notify.send(**kwargs) + # Иначе тихо игнорируем - модуль работает без уведомлений + + def get_users_with_notifications_enabled(chat, users) -> dict[int, bool]: """ Получает словарь пользователей с их настройками уведомлений. @@ -95,7 +115,7 @@ def notify_new_message(message): user = User.objects.get(email=email) if user in participants_list and user.id != author.id: if notification_settings.get(user.id, True): - notify.send( + _send_notification( sender=author, recipient=user, verb=NotificationVerbs.MENTION, @@ -120,7 +140,7 @@ def notify_new_message(message): if original_author.id not in notified_user_ids: if notification_settings.get(original_author.id, True): - notify.send( + _send_notification( sender=author, recipient=original_author, verb=NotificationVerbs.REPLY, @@ -171,7 +191,7 @@ def notify_new_message(message): if is_announcement: metadata['is_announcement'] = True - notify.send( + _send_notification( sender=author, recipient=recipient, verb=notification_verb, @@ -204,7 +224,7 @@ def notify_chat_added(chat, new_users, added_by=None): if not notification_settings.get(user.id, True): continue - notify.send( + _send_notification( sender=added_by, recipient=user, verb=NotificationVerbs.ADDED_TO_CHAT,