Create message model and save function

This commit is contained in:
Reckless_Satoshi 2022-05-28 06:01:50 -07:00
parent b5ed58e850
commit b67e671b89
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
3 changed files with 87 additions and 7 deletions

View File

@ -1,6 +1,6 @@
from django.contrib import admin
from django_admin_relation_links import AdminChangeLinksMixin
from chat.models import ChatRoom
from chat.models import ChatRoom, Message
# Register your models here.
@ -18,4 +18,19 @@ class ChatRoomAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
"room_group_name",
)
change_links = ["order","maker","taker"]
search_fields = ["id"]
search_fields = ["id"]
@admin.register(Message)
class MessageAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
list_display = (
"id",
"chatroom_link",
"order_link",
"sender_link",
"receiver_link",
"created_at",
)
change_links = ["chatroom","order","sender","receiver"]
search_fields = ["id","order"]
ordering = ("-index", )
list_filter = ("chatroom",)

View File

@ -1,7 +1,7 @@
from channels.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async
from api.models import Order
from chat.models import ChatRoom
from chat.models import ChatRoom, Message
from django.utils import timezone
import json
@ -46,6 +46,30 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
return None
@database_sync_to_async
def save_new_message(self):
'''Creates a Message object'''
order = Order.objects.get(id=self.order_id)
chatroom = ChatRoom.objects.get(order=order)
index = 0
last_message = Message.objects.filter(order=order).latest()
if last_message:
index = last_message.index + 1
sender = self.scope["user"]
Message.objects.create(
order=order,
chatroom=chatroom,
index=index,
sender = sender,
PGP_message=self.PGP_message,
)
return None
@database_sync_to_async
def save_disconnect_user(self):
'''Creates or updates the ChatRoom object'''
@ -69,7 +93,7 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
@database_sync_to_async
def is_peer_connected(self):
'''Creates or updates the ChatRoom object'''
'''Returns whether the consumer's peer is connected'''
chatroom = ChatRoom.objects.get(id=self.order_id)
@ -111,6 +135,11 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
# Encrypted messages are stored. They are served later when a user reconnects.
if message[0,27] == '-----BEGIN PGP MESSAGE-----':
self.PGP_message = message
await self.save_new_message()
peer_connected = await self.is_peer_connected()
await self.channel_layer.group_send(

View File

@ -1,6 +1,7 @@
from django.db import models
from api.models import User, Order
from django.utils import timezone
import uuid
class ChatRoom(models.Model):
'''
@ -10,7 +11,7 @@ class ChatRoom(models.Model):
id = models.PositiveBigIntegerField(primary_key=True, null=False,default=None, blank=True)
order = models.ForeignKey(
Order,
related_name="order",
related_name="chatroom",
on_delete=models.SET_NULL,
null=True,
default=None)
@ -40,4 +41,39 @@ class ChatRoom(models.Model):
null=True,
default=None,
blank=True,
)
)
def __str__(self):
return f"Chat:{str(self.order.id)}"
class Message(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
order = models.ForeignKey(
Order,
related_name="message",
on_delete=models.CASCADE,
null=True,
default=None)
chatroom = models.ForeignKey(
ChatRoom,
related_name="chatroom",
on_delete=models.CASCADE,
null=True,
default=None)
index = models.PositiveIntegerField(null=False,default=None, blank=True)
sender = models.ForeignKey(
User,
related_name="message_sender",
on_delete=models.SET_NULL,
null=True,
default=None)
PGP_message = models.TextField(max_length=5000,
null=True,
default=None,
blank=True)
created_at = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"Chat:{str(self.order.id)}-Index:{self.order.index}"