main
emile 10 months ago
parent 3abf36da0d
commit b804c9dd8f

Binary file not shown.

@ -63,7 +63,7 @@ function app(socket) {
function initializeWebSocket() { function initializeWebSocket() {
const ticketId = document.getElementById('ticketId').textContent.trim(); const ticketId = document.getElementById('ticketId').textContent.trim();
const wsUrl = `wss://${window.location.host}/ws/ticketroom/${ticketId}/`; const wsUrl = `ws://${window.location.host}/ws/ticketroom/${ticketId}/`;
const socket = new WebSocket(wsUrl); const socket = new WebSocket(wsUrl);
socket.onopen = () => { socket.onopen = () => {

@ -11,65 +11,59 @@ import json
from django.template.loader import render_to_string from django.template.loader import render_to_string
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from channels.generic.websocket import AsyncWebsocketConsumer
from django.shortcuts import get_object_or_404
from datetime import datetime
from asgiref.sync import sync_to_async
import json
class TicketRoomConsumer(WebsocketConsumer): class TicketRoomConsumer(AsyncWebsocketConsumer):
def connect(self): async def connect(self):
self.user = self.scope['user'] self.user = self.scope['user']
self.ticket_id = self.scope['url_route']['kwargs']['ticket_id'] self.ticket_id = self.scope['url_route']['kwargs']['ticket_id']
self.ticket = get_object_or_404(Ticket, id=self.ticket_id) self.ticket = await sync_to_async(get_object_or_404)(Ticket, id=self.ticket_id)
self.ticket_number = self.ticket.ticket_number self.ticket_number = self.ticket.ticket_number
existing_connection = TicketConnection.objects.filter(ticket=self.ticket, user=self.user, terminated_at__isnull=True).delete() await sync_to_async(TicketConnection.objects.filter(ticket=self.ticket, user=self.user, terminated_at__isnull=True).delete)()
TicketConnection.objects.create( await sync_to_async(TicketConnection.objects.create)(
ticket=self.ticket, ticket=self.ticket,
user=self.user, user=self.user,
date=datetime.now() date=datetime.now()
) )
staff_profile = StaffProfile.objects.filter(user=self.user).first()
staff_profile = await sync_to_async(StaffProfile.objects.filter(user=self.user).first)()
if staff_profile: if staff_profile:
if not TicketStaff.objects.filter(staff=staff_profile, ticket=self.ticket).exists(): if not await sync_to_async(TicketStaff.objects.filter(staff=staff_profile, ticket=self.ticket).exists)():
TicketStaff.objects.create( await sync_to_async(TicketStaff.objects.create)(
staff=staff_profile, staff=staff_profile,
ticket=self.ticket, ticket=self.ticket,
date_added=datetime.now() date_added=datetime.now()
) )
async_to_sync(self.channel_layer.group_add)( await self.channel_layer.group_add(self.ticket_number, self.channel_name)
self.ticket_number, self.channel_name await self.accept()
) await self.modify_online_user()
self.accept()
self.modify_online_user()
async def disconnect(self, close_code):
await sync_to_async(TicketConnection.objects.filter(ticket=self.ticket, user=self.user).update)(terminated_at=datetime.now())
await self.channel_layer.group_discard(self.ticket_number, self.channel_name)
await self.modify_online_user()
def disconnect(self, close_code): async def receive(self, text_data):
TicketConnection.objects.filter(
ticket=self.ticket,
user=self.user,
).update(terminated_at=datetime.now())
async_to_sync(self.channel_layer.group_discard)(
self.ticket_number, self.channel_name
)
self.modify_online_user()
def receive(self, text_data):
text_data_json = json.loads(text_data) text_data_json = json.loads(text_data)
event_type = text_data_json.get('event_type') event_type = text_data_json.get('event_type')
if event_type == 'typing': if event_type == 'typing':
event = { event = {
'type': 'typing_handler', 'type': 'typing_handler',
'user': self.scope['user'] 'user': self.scope['user']
} }
async_to_sync(self.channel_layer.group_send)( await self.channel_layer.group_send(self.ticket_number, event)
self.ticket_number, event
)
elif event_type == 'stop_typing': elif event_type == 'stop_typing':
event = { event = {
'type': 'stop_typing_handler', 'type': 'stop_typing_handler',
} }
async_to_sync(self.channel_layer.group_send)( await self.channel_layer.group_send(self.ticket_number, event)
self.ticket_number, event
)
elif event_type == 'update_reaction': elif event_type == 'update_reaction':
reaction = text_data_json['reaction'] reaction = text_data_json['reaction']
update_id = text_data_json['update_id'] update_id = text_data_json['update_id']
@ -79,13 +73,11 @@ class TicketRoomConsumer(WebsocketConsumer):
'reaction': reaction, 'reaction': reaction,
'user': self.scope['user'] 'user': self.scope['user']
} }
async_to_sync(self.channel_layer.group_send)( await self.channel_layer.group_send(self.ticket_number, event)
self.ticket_number, event
)
else: else:
body = text_data_json['description'] body = text_data_json['description']
file_paths = text_data_json['filePath'] file_paths = text_data_json['filePath']
ticketupdate = TicketUpdate.objects.create( ticketupdate = await sync_to_async(TicketUpdate.objects.create)(
added_by=self.user, added_by=self.user,
description=body, description=body,
ticket=self.ticket, ticket=self.ticket,
@ -96,94 +88,88 @@ class TicketRoomConsumer(WebsocketConsumer):
ticket_update=ticketupdate, ticket_update=ticketupdate,
file_path=file_path file_path=file_path
) )
ticket_attachment.save() await sync_to_async(ticket_attachment.save)()
event = { event = {
'type': 'update_handler', 'type': 'update_handler',
'update_id': ticketupdate.id 'update_id': ticketupdate.id
} }
async_to_sync(self.channel_layer.group_send)( await self.channel_layer.group_send(self.ticket_number, event)
self.ticket_number, event
)
def update_handler(self, event): async def update_handler(self, event):
update_id = event['update_id'] update_id = event['update_id']
update = TicketUpdate.objects.get(id=update_id) update = await sync_to_async(TicketUpdate.objects.get)(id=update_id)
context = { context = {
'update': update, 'update': update,
'user': self.user 'user': self.user
} }
html = await sync_to_async(render_to_string)("details_templates/partials/new-ticket-message.html", context=context)
await self.send(text_data=json.dumps({
html = render_to_string("details_templates/partials/new-ticket-message.html", context=context)
self.send(text_data=json.dumps({
'event_type': 'update', 'event_type': 'update',
'html': html 'html': html
})) }))
def typing_handler(self, event): async def typing_handler(self, event):
context = { context = {
'user': event['user'] 'user': event['user']
} }
html = render_to_string("details_templates/partials/typing-message.html", context=context) html = await sync_to_async(render_to_string)("details_templates/partials/typing-message.html", context=context)
self.send(text_data=json.dumps({ await self.send(text_data=json.dumps({
'event_type': 'typing', 'event_type': 'typing',
'html': html 'html': html
})) }))
def stop_typing_handler(self, event): async def stop_typing_handler(self, event):
self.send(text_data=json.dumps({ await self.send(text_data=json.dumps({
'event_type': 'stop_typing' 'event_type': 'stop_typing'
})) }))
def reaction_handler(self, event): async def reaction_handler(self, event):
update_id = event['update_id'] update_id = event['update_id']
reaction = event['reaction'] reaction = event['reaction']
user = self.user user = self.user
update = TicketUpdate.objects.get(id=update_id) update = await sync_to_async(TicketUpdate.objects.get)(id=update_id)
existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=update, customer=user).first() existing_reaction = await sync_to_async(TicketUpdateReaction.objects.filter)(ticket_update=update, customer=user).first()
new_reaction = None new_reaction = None
if existing_reaction: if existing_reaction:
# If the existing reaction type is equal to the new reaction, delete it
if existing_reaction.reaction == reaction: if existing_reaction.reaction == reaction:
existing_reaction.delete() await sync_to_async(existing_reaction.delete)()
else: else:
# If not, delete all previous reactions and add a new one await sync_to_async(TicketUpdateReaction.objects.filter)(ticket_update=update, customer=user).delete()
TicketUpdateReaction.objects.filter(ticket_update=update, customer=user).delete() new_reaction = await sync_to_async(TicketUpdateReaction.objects.create)(
new_reaction = TicketUpdateReaction.objects.create(
ticket_update=update, ticket_update=update,
reaction=reaction, reaction=reaction,
customer=user customer=user
) )
else: else:
# If there's no existing reaction, simply add the new one new_reaction = await sync_to_async(TicketUpdateReaction.objects.create)(
new_reaction = TicketUpdateReaction.objects.create(
ticket_update=update, ticket_update=update,
reaction=reaction, reaction=reaction,
customer=user customer=user
) )
self.send(text_data=json.dumps({ await self.send(text_data=json.dumps({
'event_type': 'reaction', 'event_type': 'reaction',
'update_id': update_id, 'update_id': update_id,
'reaction': new_reaction.reaction if new_reaction else None 'reaction': new_reaction.reaction if new_reaction else None
})) }))
def modify_online_user(self): async def modify_online_user(self):
connections = await sync_to_async(TicketConnection.objects.filter)(ticket=self.ticket, terminated_at__isnull=True)
event = { event = {
'type': 'user_connection_handler', 'type': 'user_connection_handler',
'user': self.user, 'user': self.user,
'connections': connections,
} }
async_to_sync(self.channel_layer.group_send)( await self.channel_layer.group_send(self.ticket_number, event)
self.ticket_number, event
)
def user_connection_handler(self, event): async def user_connection_handler(self, event):
context = { context = {
'connections': event['connections'],
'user': event['user'] 'user': event['user']
} }
html = render_to_string("details_templates/partials/ticket-online-users.html", context=context) html = await sync_to_async(render_to_string)("details_templates/partials/ticket-online-users.html", context=context)
self.send(text_data=json.dumps({ await self.send(text_data=json.dumps({
'event_type': 'user_status', 'event_type': 'user_status',
'html': html 'html': html
})) }))

Loading…
Cancel
Save