emile 8 months ago
parent e638e29314
commit 4ad25f85d7

@ -8,6 +8,7 @@ import requests
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Case, When, F, Max, DateTimeField from django.db.models import Case, When, F, Max, DateTimeField
from django.db import transaction
@ -331,7 +332,7 @@ class OsitcomChatRoom(WebsocketConsumer):
if not self.visitor: if not self.visitor:
self.close() self.close()
if self.scope['url_route']['kwargs'].get('chat_id'): #Case where admin is accessing a specific conversation between the conversations of this visior if self.scope['url_route']['kwargs'].get('chat_id'): #Case where admin is accessing a specific conversation between the conversations of this visitor
self.chat_room = get_object_or_404(ChatRoom, id=self.scope['url_route']['kwargs'].get('chat_id')) self.chat_room = get_object_or_404(ChatRoom, id=self.scope['url_route']['kwargs'].get('chat_id'))
else: else:
chat_room_guest = ChatRoomGuest.objects.filter(visitor=self.visitor).last() #Case where the visitor will always acesss his last conversation chat_room_guest = ChatRoomGuest.objects.filter(visitor=self.visitor).last() #Case where the visitor will always acesss his last conversation
@ -371,35 +372,40 @@ class OsitcomChatRoom(WebsocketConsumer):
self.load_chat_handler(event) self.load_chat_handler(event)
if event_type == 'start_conversation': if event_type == 'start_conversation':
chat_room = ChatRoom.objects.create( try:
name=f"Support: {self.session_id}", with transaction.atomic():
date_created = datetime.now() chat_room = ChatRoom.objects.create(
) name=f"Support: {self.session_id}",
if text_data_json.get('guest_name'): date_created = datetime.now()
self.visitor.name = text_data_json.get('guest_name') )
self.visitor.save() if text_data_json.get('guest_name'):
if text_data_json.get('guest_mobile_number'): self.visitor.name = text_data_json.get('guest_name')
self.visitor.mobile_number = text_data_json.get('guest_mobile_number') self.visitor.save()
self.visitor.save() if text_data_json.get('guest_mobile_number'):
chat_room_guest = ChatRoomGuest.objects.create( self.visitor.mobile_number = text_data_json.get('guest_mobile_number')
room=chat_room, self.visitor.save()
visitor=self.visitor chat_room_guest = ChatRoomGuest.objects.create(
) room=chat_room,
self.chat_room = chat_room visitor=self.visitor
async_to_sync(self.channel_layer.group_discard)( )
self.group, self.channel_name self.chat_room = chat_room
) async_to_sync(self.channel_layer.group_discard)(
self.group = f"{self.session_id}_{self.chat_room.id}" self.group, self.channel_name
async_to_sync(self.channel_layer.group_add)( )
self.group, self.channel_name self.group = f"{self.session_id}_{self.chat_room.id}"
) async_to_sync(self.channel_layer.group_add)(
event = { self.group, self.channel_name
'type': 'start_conversation_handler', )
'chat_room_id': chat_room.id event = {
} 'type': 'start_conversation_handler',
async_to_sync(self.channel_layer.group_send)( 'chat_room_id': chat_room.id
self.group, event }
) async_to_sync(self.channel_layer.group_send)(
self.group, event
)
except Exception as e:
print('Error starting conversation')
if event_type == 'typing': if event_type == 'typing':
event = { event = {

@ -242,7 +242,18 @@ class ChatRoom(models.Model):
class ChatRoomGuest(models.Model): class ChatRoomGuest(models.Model):
room = models.OneToOneField(ChatRoom, on_delete=models.CASCADE, null=True) room = models.OneToOneField(ChatRoom, on_delete=models.CASCADE, null=True)
visitor = models.ForeignKey(Visitor, null=True, on_delete=models.CASCADE) visitor = models.ForeignKey(Visitor, null=True, on_delete=models.CASCADE)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.send_chat_notification()
def send_chat_notification(self):
title = "New chat on Ositcom!"
body = f"Visitor {self.visitor.ip_address} started a new chat on Ositcom"
notification = ChatNotification.objects.create(
title=title,
message = body,
image = self.visitor.notification_flag_image_url,
type = "Chat"
)
class ChatRoomReview(models.Model): class ChatRoomReview(models.Model):
REACTION_CHOICES = ( REACTION_CHOICES = (
@ -279,6 +290,17 @@ class ChatMessage(models.Model):
'chatroom_id': self.room.id, 'chatroom_id': self.room.id,
} }
async_to_sync(channel_layer.group_send)("osichat", event) async_to_sync(channel_layer.group_send)("osichat", event)
if not self.member:
self.send_message_notification()
def send_message_notification(self):
title = f"Visitor {self.room.chatroomguest.visitor.ip_address} sent a new message on Ositcom!"
body = f"{self.content}"
notification = ChatNotification.objects.create(
title=title,
message = body,
image = self.room.chatroomguest.visitor.notification_flag_image_url,
type = "Chat"
)
class ChatMessageAttachment(models.Model): class ChatMessageAttachment(models.Model):

Loading…
Cancel
Save