import logging from django.db.models.signals import post_save from django.dispatch import receiver from apps.chat.models import ChatMessage, RoomMessage from apps.account.models import User from apps.account.notification_service import create_and_send_notification from apps.course.models.course import get_localized_field from apps.course.models.participant import Participant logger = logging.getLogger(__name__) def get_message_preview(instance, lang='fa'): if instance.content_type == 'text': preview = instance.content or '' if len(preview) > 50: return preview[:50] + '...' return preview elif instance.content_type == 'image': return 'تصویر' if lang == 'fa' else 'Image' elif instance.content_type == 'audio': return 'صدا' if lang == 'fa' else 'Audio' else: return 'فایل ضمیمه' if lang == 'fa' else 'Attachment' @receiver(post_save, sender=ChatMessage) def notify_on_new_chat_message(sender, instance, created, **kwargs): if not created: return sender_user = instance.sender room = instance.room # 1. Check if the message is in a PRIVATE room if room.room_type == RoomMessage.RoomTypeChoices.PRIVATE: # Determine the recipient user (the other person in the private room) recipient_user = None if room.initiator == sender_user: recipient_user = room.recipient else: recipient_user = room.initiator if not recipient_user: return # Case A: Teacher Replied in a Private Chat if sender_user.user_type == User.UserType.PROFESSOR: if recipient_user.user_type in [User.UserType.STUDENT, User.UserType.CLIENT]: preview_en = get_message_preview(instance, 'en') preview_fa = get_message_preview(instance, 'fa') create_and_send_notification( user=recipient_user, title_en="New Message from Teacher", body_en=f"Teacher {sender_user.fullname or sender_user.username} replied: {preview_en}", title_fa="پیام جدید از طرف استاد", body_fa=f"استاد {sender_user.fullname or sender_user.username} پاسخ داد: {preview_fa}", service='imam-javad', data={'type': 'teacher_reply_private', 'message_id': instance.id, 'room_id': room.id} ) # Case B: Support Replied elif sender_user.user_type in [User.UserType.ADMIN, User.UserType.SUPER_ADMIN, User.UserType.CONSULTANT]: if recipient_user.user_type in [User.UserType.STUDENT, User.UserType.CLIENT]: preview_en = get_message_preview(instance, 'en') preview_fa = get_message_preview(instance, 'fa') create_and_send_notification( user=recipient_user, title_en="New Message from Support", body_en=f"Support agent {sender_user.fullname or sender_user.username} replied to your message: {preview_en}", title_fa="پاسخ پشتیبانی", body_fa=f"پشتیبان {sender_user.fullname or sender_user.username} به پیام شما پاسخ داد: {preview_fa}", service='imam-javad', data={'type': 'support_reply', 'message_id': instance.id, 'room_id': room.id} ) # 2. Check if the message is in a GROUP/COURSE room elif room.room_type == RoomMessage.RoomTypeChoices.GROUP and room.course: # Case A: Teacher Replied in a Course Chat if sender_user.user_type == User.UserType.PROFESSOR: course = room.course course_title_en = get_localized_field('en', course.title) course_title_fa = get_localized_field('fa', course.title) preview_en = get_message_preview(instance, 'en') preview_fa = get_message_preview(instance, 'fa') # Notify all active enrolled students of the course participants = Participant.objects.filter(course=course, is_active=True).select_related('student') for p in participants: if p.student != sender_user: # Exclude sender create_and_send_notification( user=p.student, title_en="Teacher Post in Course Chat", body_en=f"Teacher {sender_user.fullname or sender_user.username} posted in '{course_title_en}': {preview_en}", title_fa="پیام استاد در گفتگوی دوره", body_fa=f"استاد {sender_user.fullname or sender_user.username} در گفتگوی دوره «{course_title_fa}» پیام جدیدی فرستاد: {preview_fa}", service='imam-javad', data={'type': 'teacher_reply_course', 'message_id': instance.id, 'room_id': room.id} )