From 488c87ec3974ab23654466eafd45ce1dfa34105f Mon Sep 17 00:00:00 2001 From: Cal Ellowitz Date: Sun, 15 Dec 2024 15:39:29 -0500 Subject: [PATCH] add message direction and fix message retrieval --- .../migrations/0003_message_direction.py | 19 +++++++++++++++++++ messaging/models.py | 7 +++++++ messaging/views.py | 14 ++++++++++---- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 messaging/migrations/0003_message_direction.py diff --git a/messaging/migrations/0003_message_direction.py b/messaging/migrations/0003_message_direction.py new file mode 100644 index 0000000..71e026d --- /dev/null +++ b/messaging/migrations/0003_message_direction.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.7 on 2024-12-15 20:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("messaging", "0002_remove_messageserver_oauth_application_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="message", + name="direction", + field=models.CharField( + choices=[("M", "Mobile"), ("S", "Server")], default="M", max_length=4 + ), + ), + ] diff --git a/messaging/models.py b/messaging/models.py index 985aa44..2ee8b08 100644 --- a/messaging/models.py +++ b/messaging/models.py @@ -36,6 +36,11 @@ class MessageStatus(models.TextChoices): CONFIRMED_RECEIVED = "CONFIRMED_RECEIVED" # when message is mark received on service +class MessageDirection(models.TextChoices): + MOBILE = "M" # sent to mobile + SERVER = "S" # sent to server + + class Message(models.Model): message_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) channel = models.ForeignKey(Channel, on_delete=models.CASCADE) @@ -44,3 +49,5 @@ class Message(models.Model): received = models.DateTimeField(null=True, blank=True) status = models.CharField( max_length=50, choices=MessageStatus.choices, default=MessageStatus.PENDING) + # represents the direction the message is sent toward + direction = models.CharField(max_length=4, choices=MessageDirection.choices) diff --git a/messaging/views.py b/messaging/views.py index b6dd1ed..10389bd 100644 --- a/messaging/views.py +++ b/messaging/views.py @@ -12,7 +12,7 @@ from rest_framework.exceptions import ValidationError from rest_framework.views import APIView -from messaging.models import Channel, Message, MessageStatus, MessageServer +from messaging.models import Channel, Message, MessageDirection, MessageStatus, MessageServer from messaging.serializers import SingleMessageSerializer, BulkMessageSerializer, MessageSerializer, \ MessageData from messaging.task import make_request, send_messages_to_service_and_mark_status @@ -201,7 +201,8 @@ def post(self, request, *args, **kwargs): message_data = { "channel_id": data["channel"], "content": data["content"], - "message_id": data["message_id"] + "message_id": data["message_id"], + "direction": MessageDirection.MOBILE } message = Message(**message_data) message.save() @@ -242,7 +243,8 @@ def post(self, request, *args, **kwargs): message_data = { "message_id": message["message_id"], "content": message["content"], - "channel_id": message["channel"] + "channel_id": message["channel"], + "direction": MessageDirection.SERVER } messages.append(Message(**message_data)) @@ -298,7 +300,11 @@ def get(self, request, *args, **kwargs): for channel in channels: channels_data.append({"channel_source": channel.channel_source, "channel_id": str(channel.channel_id), "key_url": channel.server.key_url, "consent": channel.user_consent}) - channel_messages = channel.message_set.all() + channel_messages = Message.objects.filter( + channel=channel, + direction=MessageDirection.MOBILE, + status=MessageStatus.PENDING + ) messages.extend(channel_messages) messages_data = MessageSerializer(messages, many=True).data