Skip to content

Commit

Permalink
add message direction and fix message retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
calellowitz committed Dec 15, 2024
1 parent 73d8476 commit 488c87e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
19 changes: 19 additions & 0 deletions messaging/migrations/0003_message_direction.py
Original file line number Diff line number Diff line change
@@ -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
),
),
]
7 changes: 7 additions & 0 deletions messaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
14 changes: 10 additions & 4 deletions messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 488c87e

Please sign in to comment.