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.contrib import admin
from django_admin_relation_links import AdminChangeLinksMixin from django_admin_relation_links import AdminChangeLinksMixin
from chat.models import ChatRoom from chat.models import ChatRoom, Message
# Register your models here. # Register your models here.
@ -19,3 +19,18 @@ class ChatRoomAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
) )
change_links = ["order","maker","taker"] 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.generic.websocket import AsyncWebsocketConsumer
from channels.db import database_sync_to_async from channels.db import database_sync_to_async
from api.models import Order from api.models import Order
from chat.models import ChatRoom from chat.models import ChatRoom, Message
from django.utils import timezone from django.utils import timezone
import json import json
@ -46,6 +46,30 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
return None 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 @database_sync_to_async
def save_disconnect_user(self): def save_disconnect_user(self):
'''Creates or updates the ChatRoom object''' '''Creates or updates the ChatRoom object'''
@ -69,7 +93,7 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
@database_sync_to_async @database_sync_to_async
def is_peer_connected(self): 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) chatroom = ChatRoom.objects.get(id=self.order_id)
@ -112,6 +136,11 @@ class ChatRoomConsumer(AsyncWebsocketConsumer):
text_data_json = json.loads(text_data) text_data_json = json.loads(text_data)
message = text_data_json["message"] 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() peer_connected = await self.is_peer_connected()
await self.channel_layer.group_send( await self.channel_layer.group_send(
self.room_group_name, self.room_group_name,

View File

@ -1,6 +1,7 @@
from django.db import models from django.db import models
from api.models import User, Order from api.models import User, Order
from django.utils import timezone
import uuid
class ChatRoom(models.Model): class ChatRoom(models.Model):
''' '''
@ -10,7 +11,7 @@ class ChatRoom(models.Model):
id = models.PositiveBigIntegerField(primary_key=True, null=False,default=None, blank=True) id = models.PositiveBigIntegerField(primary_key=True, null=False,default=None, blank=True)
order = models.ForeignKey( order = models.ForeignKey(
Order, Order,
related_name="order", related_name="chatroom",
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
null=True, null=True,
default=None) default=None)
@ -41,3 +42,38 @@ class ChatRoom(models.Model):
default=None, default=None,
blank=True, 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}"