refactor(communications): Сделать зависимость от notifications опциональной

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()
This commit is contained in:
lizerk 2026-03-11 17:25:15 +03:00
parent 5ebf9dd71a
commit 2e1f413264

View File

@ -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,