diff --git a/osinaweb/support/__pycache__/consumers.cpython-310.pyc b/osinaweb/support/__pycache__/consumers.cpython-310.pyc index c5d5339f..39ccef52 100644 Binary files a/osinaweb/support/__pycache__/consumers.cpython-310.pyc and b/osinaweb/support/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/support/consumers.py b/osinaweb/support/consumers.py index f146a2ce..dfda7ed2 100644 --- a/osinaweb/support/consumers.py +++ b/osinaweb/support/consumers.py @@ -50,14 +50,138 @@ class TicketRoomConsumer(WebsocketConsumer): ) self.modify_online_user() + def receive(self, text_data): + text_data_json = json.loads(text_data) + event_type = text_data_json.get('event_type') + + if event_type == 'typing': + event = { + 'type': 'typing_handler', + 'user': self.scope['user'] + } + async_to_sync(self.channel_layer.group_send)( + self.ticket_number, event + ) + elif event_type == 'stop_typing': + event = { + 'type': 'stop_typing_handler', + } + async_to_sync(self.channel_layer.group_send)( + self.ticket_number, event + ) + elif event_type == 'update_reaction': + reaction = text_data_json['reaction'] + update_id = text_data_json['update_id'] + event = { + 'type': 'reaction_handler', + 'update_id': update_id, + 'reaction': reaction, + 'user': self.scope['user'] + } + async_to_sync(self.channel_layer.group_send)( + self.ticket_number, event + ) + else: + body = text_data_json['description'] + file_paths = text_data_json['filePath'] + ticketupdate = TicketUpdate.objects.create( + added_by=self.user, + description=body, + ticket=self.ticket, + date_added=datetime.now() + ) + for file_path in file_paths: + ticket_attachment = TicketAttachment( + ticket_update=ticketupdate, + file_path=file_path + ) + ticket_attachment.save() + event = { + 'type': 'update_handler', + 'update_id': ticketupdate.id + } + async_to_sync(self.channel_layer.group_send)( + self.ticket_number, event + ) + + def update_handler(self, event): + update_id = event['update_id'] + update = TicketUpdate.objects.get(id=update_id) + context = { + 'update': update, + 'user': self.user + } + html = render_to_string("details_templates/partials/new-ticket-message.html", context=context) + self.send(text_data=json.dumps({ + 'event_type': 'update', + 'html': html + })) + + def typing_handler(self, event): + context = { + 'user': event['user'] + } + html = render_to_string("details_templates/partials/typing-message.html", context=context) + self.send(text_data=json.dumps({ + 'event_type': 'typing', + 'html': html + })) + + def stop_typing_handler(self, event): + self.send(text_data=json.dumps({ + 'event_type': 'stop_typing' + })) + + def reaction_handler(self, event): + update_id = event['update_id'] + reaction = event['reaction'] + user = self.user + + update = TicketUpdate.objects.get(id=update_id) + existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=update, customer=user).first() + new_reaction = None + if existing_reaction: + # If the existing reaction type is equal to the new reaction, delete it + if existing_reaction.reaction == reaction: + existing_reaction.delete() + else: + # If not, delete all previous reactions and add a new one + TicketUpdateReaction.objects.filter(ticket_update=update, customer=user).delete() + new_reaction = TicketUpdateReaction.objects.create( + ticket_update=update, + reaction=reaction, + customer=user + ) + else: + # If there's no existing reaction, simply add the new one + new_reaction = TicketUpdateReaction.objects.create( + ticket_update=update, + reaction=reaction, + customer=user + ) + + self.send(text_data=json.dumps({ + 'event_type': 'reaction', + 'update_id': update_id, + 'reaction': new_reaction.reaction if new_reaction else None + })) + def modify_online_user(self): connections = TicketConnection.objects.filter(ticket=self.ticket, terminated_at__isnull=True) + online_connections = [] + for connection in connections: + connection_data = { + 'first_name': connection.user.first_name, + 'last_name': connection.user.last_name, + } + online_connections.append(connection_data) + event = { 'type': 'user_connection_handler', 'user': self.user, - 'connections': list(connections.values()), + 'connections': online_connections, } async_to_sync(self.channel_layer.group_send)( self.ticket_number, event