emile 8 months ago
parent e638e29314
commit 4ad25f85d7

@ -8,6 +8,7 @@ import requests
from django.forms.models import model_to_dict
from django.core.serializers.json import DjangoJSONEncoder
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:
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'))
else:
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)
if event_type == 'start_conversation':
chat_room = ChatRoom.objects.create(
name=f"Support: {self.session_id}",
date_created = datetime.now()
)
if text_data_json.get('guest_name'):
self.visitor.name = text_data_json.get('guest_name')
self.visitor.save()
if text_data_json.get('guest_mobile_number'):
self.visitor.mobile_number = text_data_json.get('guest_mobile_number')
self.visitor.save()
chat_room_guest = ChatRoomGuest.objects.create(
room=chat_room,
visitor=self.visitor
)
self.chat_room = chat_room
async_to_sync(self.channel_layer.group_discard)(
self.group, self.channel_name
)
self.group = f"{self.session_id}_{self.chat_room.id}"
async_to_sync(self.channel_layer.group_add)(
self.group, self.channel_name
)
event = {
'type': 'start_conversation_handler',
'chat_room_id': chat_room.id
}
async_to_sync(self.channel_layer.group_send)(
self.group, event
)
try:
with transaction.atomic():
chat_room = ChatRoom.objects.create(
name=f"Support: {self.session_id}",
date_created = datetime.now()
)
if text_data_json.get('guest_name'):
self.visitor.name = text_data_json.get('guest_name')
self.visitor.save()
if text_data_json.get('guest_mobile_number'):
self.visitor.mobile_number = text_data_json.get('guest_mobile_number')
self.visitor.save()
chat_room_guest = ChatRoomGuest.objects.create(
room=chat_room,
visitor=self.visitor
)
self.chat_room = chat_room
async_to_sync(self.channel_layer.group_discard)(
self.group, self.channel_name
)
self.group = f"{self.session_id}_{self.chat_room.id}"
async_to_sync(self.channel_layer.group_add)(
self.group, self.channel_name
)
event = {
'type': 'start_conversation_handler',
'chat_room_id': chat_room.id
}
async_to_sync(self.channel_layer.group_send)(
self.group, event
)
except Exception as e:
print('Error starting conversation')
if event_type == 'typing':
event = {

@ -242,7 +242,18 @@ class ChatRoom(models.Model):
class ChatRoomGuest(models.Model):
room = models.OneToOneField(ChatRoom, on_delete=models.CASCADE, null=True)
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):
REACTION_CHOICES = (
@ -279,6 +290,17 @@ class ChatMessage(models.Model):
'chatroom_id': self.room.id,
}
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):

Loading…
Cancel
Save