diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 8f8a2ac5..2491d07f 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index cf94d1e0..5bd69a3d 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/models.cpython-310.pyc b/osinaweb/osichat/__pycache__/models.cpython-310.pyc index a705f211..0f665e1b 100644 Binary files a/osinaweb/osichat/__pycache__/models.cpython-310.pyc and b/osinaweb/osichat/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc index 7f41bd73..18b6a6f5 100644 Binary files a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc and b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc differ diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 439f2172..e9f03e1a 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -29,13 +29,6 @@ class OsitcomVisitor(WebsocketConsumer): def receive(self, text_data): text_data_json = json.loads(text_data) event_type = text_data_json.get('event_type') - - - if event_type == 'set_client_type': - self.client_type = text_data_json.get('client_type') - self.user_id = text_data_json.get('user_id') - self.get_vistors_handler() - if event_type == 'visitor_ping': session_id = text_data_json.get('session_id') if Visitor.objects.filter(session_id=session_id).last(): @@ -54,42 +47,22 @@ class OsitcomVisitor(WebsocketConsumer): visit_date = datetime.now() ) - def get_vistors_handler(self): - visitors = Visitor.objects.all().order_by('-id') - context = { - 'visitors': visitors, - } - if self.client_type == 'mobile_admin': - visitors_data = [] - for visitor in visitors: - visitor_dict = model_to_dict(visitor) - visitor_logs = VisitorLog.objects.filter(visitor=visitor) - - visitor_logs_data = [model_to_dict(log) for log in visitor_logs] - visitor_dict['visitor_logs'] = visitor_logs_data - - visitors_data.append(visitor_dict) - - self.send(text_data=json.dumps({ - 'event_type': 'get_visitors', - 'visitors_data': visitors_data, - }, cls=DjangoJSONEncoder)) -class OsitcomChatRooms(WebsocketConsumer): +class Osichat(WebsocketConsumer): def connect(self): async_to_sync(self.channel_layer.group_add)( - 'ositcom_chats', self.channel_name + 'osichat', self.channel_name ) self.accept() def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)( - 'ositcom_chats', self.channel_name + 'osichat', self.channel_name ) def receive(self, text_data): @@ -99,11 +72,30 @@ class OsitcomChatRooms(WebsocketConsumer): if event_type == 'set_client_type': self.client_type = data.get('client_type') self.user_id = data.get('user_id') - self.get_chats_handler() + event = { + 'type': 'get_chats_handler', + } + self.get_chats_handler(event) + + if event_type == 'get_chats': + event = { + 'type': 'get_chats_handler', + } + async_to_sync(self.channel_layer.group_send)( + 'osichat', event + ) + + if event_type == 'get_visitors': + print('eeewew') + event = { + 'type': 'get_visitors_handler', + } + async_to_sync(self.channel_layer.group_send)( + 'osichat', event + ) - def get_chats_handler(self): - + def get_chats_handler(self, event): chat_rooms = ChatRoom.objects.annotate(last_update=Max('chatmessage__date_sent'), order_key=Case( When(last_update__isnull=True, then=F('date_created')), @@ -149,7 +141,7 @@ class OsitcomChatRooms(WebsocketConsumer): })) - def new_update_handler(self, event): + def new_chat_update_handler(self, event): chat_room = get_object_or_404(ChatRoom, id=event['chatroom_id']) user = get_object_or_404(User, id=self.user_id) number_of_unread = ChatMessage.objects.filter(room=chat_room).exclude(member=user).exclude(chatmessageseen__member=user).count() @@ -168,13 +160,43 @@ class OsitcomChatRooms(WebsocketConsumer): else: html = render_to_string("chat_templates/partials/new-chat-room.html", context=context) self.send(text_data=json.dumps({ - 'event_type': 'new_update', + 'event_type': 'new_chat_update', 'chatroom_id': chat_room.id, 'user': last_message.member.id if last_message and last_message.member else None, 'html': html, })) + def get_visitors_handler(self, event): + visitors = Visitor.objects.all().order_by('-id') + context = { + 'visitors': visitors, + } + if self.client_type == 'mobile_admin': + visitors_data = [] + for visitor in visitors: + visitor_dict = model_to_dict(visitor) + visitor_logs = VisitorLog.objects.filter(visitor=visitor) + + visitor_logs_data = [model_to_dict(log) for log in visitor_logs] + visitor_dict['visitor_logs'] = visitor_logs_data + + visitors_data.append(visitor_dict) + + self.send(text_data=json.dumps({ + 'event_type': 'get_visitors', + 'visitors_data': visitors_data, + }, cls=DjangoJSONEncoder)) + + else: + html = render_to_string("chat_templates/partials/visitors.html", context=context) + self.send(text_data=json.dumps({ + 'event_type': 'get_visitors', + 'html': html, + })) + + + @@ -244,21 +266,20 @@ class OsitcomChatRoom(WebsocketConsumer): visitor=self.visitor ) self.chat_room = chat_room - self.group = f"{self.session_id}_{self.chat_room.id}" 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 ) - async_to_sync(self.channel_layer.group_send)( - self.group, event - ) - event = { 'type': 'start_conversation_handler', 'chat_room_id': chat_room.id } + async_to_sync(self.channel_layer.group_send)( + self.group, event + ) if event_type == 'typing': event = { diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index dcdcd33a..2c4db2d8 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -58,10 +58,10 @@ class ChatRoom(models.Model): super().save(*args, **kwargs) channel_layer = get_channel_layer() event = { - 'type': 'new_update_handler', + 'type': 'new_chat_update_handler', 'chatroom_id': self.id, } - async_to_sync(channel_layer.group_send)("ositcom_chats", event) + async_to_sync(channel_layer.group_send)("osichat", event) @@ -102,10 +102,10 @@ class ChatMessage(models.Model): super().save(*args, **kwargs) channel_layer = get_channel_layer() event = { - 'type': 'new_update_handler', + 'type': 'new_chat_update_handler', 'chatroom_id': self.room.id, } - async_to_sync(channel_layer.group_send)("ositcom_chats", event) + async_to_sync(channel_layer.group_send)("osichat", event) class ChatMessageAttachment(models.Model): diff --git a/osinaweb/osichat/routing.py b/osinaweb/osichat/routing.py index 05e2bcb1..21d897e3 100644 --- a/osinaweb/osichat/routing.py +++ b/osinaweb/osichat/routing.py @@ -2,8 +2,7 @@ from django.urls import path from .consumers import * websocket_urlpatterns = [ - path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()), - path("ws/osichat/rooms/", OsitcomChatRooms.as_asgi()), + path("ws/osichat/", Osichat.as_asgi()), path("ws/osichat//", OsitcomChatRoom.as_asgi()), path("ws/osichat-admin///", OsitcomChatRoom.as_asgi()), diff --git a/osinaweb/osinacore/templates/chat_templates/chat-widget.html b/osinaweb/osinacore/templates/chat_templates/chat-widget.html index 838d79cd..189eb5b8 100644 --- a/osinaweb/osinacore/templates/chat_templates/chat-widget.html +++ b/osinaweb/osinacore/templates/chat_templates/chat-widget.html @@ -1,10 +1,57 @@ {% load static %} -