def post(self, request, chat_uuid): if 'message_content' not in request.data or not chat_uuid: return NotFound('Required parameter missing') user = request.user try: chat = MessageParticipants.objects.get(chat_uuid=chat_uuid) Participant.objects.get(user=user, chat=chat, is_active=True) except Participant.DoesNotExist: raise NotFound('You are not a participant of this chat.') if chat: receiver_user_ids = Participant.objects.filter( chat=chat, is_active=True).values_list('user', flat=True) receiver_users = User.objects.filter(id__in=receiver_user_ids) message = Message.objects.create( content=request.data['message_content'], sender=user, message_participant=chat, sent_at=timezone.now()) chat.last_message_at = timezone.now() chat.save() notification = create_chat_message_notification(chat) for receiver in receiver_users: if not receiver.id == user.id: MessageReceiver.objects.create(receiver=receiver, message=message) create_email_notification(receiver, chat, request.data['message_content'], user, notification) create_user_notification(receiver, notification) return Response({'message': 'Message sent'}, status=status.HTTP_201_CREATED)
async def new_message(self, chat_uuid, user, message_content): try: chat = MessageParticipants.objects.get( chat_uuid=chat_uuid ) except MessageParticipants.DoesNotExist: chat = None receiver_user_ids = Participant.objects.filter(chat=chat).values_list('user', flat=True) receiver_users = User.objects.filter(id__in=receiver_user_ids) message = Message.objects.create( content=message_content, sender=user, message_participant=chat, sent_at=timezone.now() ) chat.last_message_at = timezone.now() chat.save() notification = create_chat_message_notification(chat) for receiver in receiver_users: if not receiver.id == user.id: MessageReceiver.objects.create( receiver=receiver, message=message ) create_email_notification(receiver, chat, message_content, user, notification) create_user_notification(receiver, notification) return { "message": message, "receivers": receiver_users }
async def new_message(self, chat_uuid, user, message_content): try: message_participant = MessageParticipants.objects.get( chat_uuid=chat_uuid ) except MessageParticipants.DoesNotExist: message_participant = None receivers = message_participant.participants.all().exclude(id=user.id) message = Message.objects.create( content=message_content, sender=user, message_participant=message_participant, sent_at=timezone.now() ) message_participant.last_message_at = timezone.now() message_participant.save() notification = create_chat_message_notification(message_participant) for receiver in receivers: MessageReceiver.objects.create( receiver=receiver, message=message ) create_user_notification(receiver, notification) return message