diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 08fad1ba..cf10a276 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 fa12059b..1168c135 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__/routing.cpython-310.pyc b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc index 90123d1e..3b115dfd 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/api/__pycache__/views.cpython-310.pyc b/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc index 2a0e81a6..8e83b663 100644 Binary files a/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc and b/osinaweb/osichat/api/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osichat/api/views.py b/osinaweb/osichat/api/views.py index cb5eded2..878a28ce 100644 --- a/osinaweb/osichat/api/views.py +++ b/osinaweb/osichat/api/views.py @@ -14,7 +14,6 @@ def get_chat_rooms(request): - @api_view(['GET']) def get_visitors(request): start_date = request.query_params.get("start_date") @@ -57,6 +56,57 @@ def get_visitors(request): - +@api_view(['GET']) +def get_staffs(request): + staffs = StaffProfile.objects.filter(user__is_active=True).all().order_by('user__first_name') + for staff in staffs: + staff_data = { + "id": staff.id, + "first_name": staff.user.first_name, + "last_name": staff.user.last_name, + "image": staff.image.url, + "last_seen": staff.get_last_seen + } + + return successRes(staff_data.data) + + +@api_view(['GET']) +def start_conversation(request): + try: + token_data = verify(request.headers.get("Authorization")) + user_id = token_data['userid'] + user = User.objects.get(id=user_id) + + start_with_id = request.data.get('start_with') + start_with = get_object_or_404(User, id=start_with_id) + chat_title = ( + f"Chat created by {user.first_name} {user.last_name} " + f"with {start_with.first_name} {start_with.last_name} " + f"on {timezone.now()}" + ) + + room = ChatRoom.objects.create( + name=chat_title, + created_by=user, + date_created=timezone.now(), + ) + ChatMember.objects.create( + member=user, + room=room, + date_joined=timezone.now() + ) + ChatMember.objects.create( + member=start_with, + room=room, + date_joined=timezone.now() + ) + return successRes({'room_id': room.id}) + except TokenError as terr: + return errorRes(msg=str(terr), status=450) + except Exception as e: + return errorRes(str(e)) + + diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 6e1d942a..63a42aaa 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -328,8 +328,9 @@ class OsitcomChatRoom(WebsocketConsumer): def connect(self): self.domain = 'https://osina.ositcom.com' self.session_id = self.scope['url_route']['kwargs']['session_id'] - self.visitor = Visitor.objects.filter(session_id=self.session_id).last() - if not self.visitor: + if self.session_id: + self.visitor = Visitor.objects.filter(session_id=self.session_id).last() + if self.session_id and 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 visitor @@ -341,11 +342,19 @@ class OsitcomChatRoom(WebsocketConsumer): else: self.chat_room = None + if self.scope["user"].is_authenticated: #If a user add him as a chat_member + self.chat_member = ChatMember.objects.filter(member=self.scope["user"]).last() + if not self.chat_member: + self.chat_member = ChatMember.objects.create( + room=self.chat_room, + member=self.scope["user"], + date_joined = datetime.now() + ) if self.chat_room: self.group = f"{self.session_id}_{self.chat_room.id}" else: - self.group = self.session_id + self.group = self.session_id #Visitor hasn't started a chat yet, when he will we will remove him from this group and add it to the chat group async_to_sync(self.channel_layer.group_add)( self.group, self.channel_name diff --git a/osinaweb/osichat/routing.py b/osinaweb/osichat/routing.py index 98c64466..c2db6e69 100644 --- a/osinaweb/osichat/routing.py +++ b/osinaweb/osichat/routing.py @@ -5,7 +5,7 @@ websocket_urlpatterns = [ path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()), path("ws/osichat/", Osichat.as_asgi()), path("ws/osichat//", OsitcomChatRoom.as_asgi()), - path("ws/osichat-admin///", OsitcomChatRoom.as_asgi()), + path("ws/osichat-admin//", OsitcomChatRoom.as_asgi()), path("ws/visitors/osichat-admin//", OsichatVisitor.as_asgi()), ] \ No newline at end of file diff --git a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc index 7628bd7b..c7a34dbb 100644 Binary files a/osinaweb/osinacore/__pycache__/models.cpython-310.pyc and b/osinaweb/osinacore/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osinacore/add/__pycache__/views.cpython-310.pyc b/osinaweb/osinacore/add/__pycache__/views.cpython-310.pyc index f41edb73..d8a526bc 100644 Binary files a/osinaweb/osinacore/add/__pycache__/views.cpython-310.pyc and b/osinaweb/osinacore/add/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osinacore/add/views.py b/osinaweb/osinacore/add/views.py index ac42dfd3..6eb9537c 100644 --- a/osinaweb/osinacore/add/views.py +++ b/osinaweb/osinacore/add/views.py @@ -166,7 +166,6 @@ def add_staff(request): user=user, image=request.FILES.get('image'), mobile_number=request.POST.get('mobile_number'), - active=active, intern=intern, ) diff --git a/osinaweb/osinacore/edit/__pycache__/views.cpython-310.pyc b/osinaweb/osinacore/edit/__pycache__/views.cpython-310.pyc index 8f69e5d5..f2415a90 100644 Binary files a/osinaweb/osinacore/edit/__pycache__/views.cpython-310.pyc and b/osinaweb/osinacore/edit/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/osinacore/edit/views.py b/osinaweb/osinacore/edit/views.py index 6550a014..e4dd7c4b 100644 --- a/osinaweb/osinacore/edit/views.py +++ b/osinaweb/osinacore/edit/views.py @@ -118,8 +118,7 @@ def edit_staff(request, staff_id): staff.user.save() staff.mobile_number = request.POST.get('mobile_number') - - staff.active = active + new_image = request.FILES.get('image') if new_image: diff --git a/osinaweb/osinacore/migrations/0107_remove_staffprofile_active.py b/osinaweb/osinacore/migrations/0107_remove_staffprofile_active.py new file mode 100644 index 00000000..de99b117 --- /dev/null +++ b/osinaweb/osinacore/migrations/0107_remove_staffprofile_active.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.5 on 2024-09-11 12:16 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('osinacore', '0106_alter_projectfile_file'), + ] + + operations = [ + migrations.RemoveField( + model_name='staffprofile', + name='active', + ), + ] diff --git a/osinaweb/osinacore/migrations/__pycache__/0107_remove_staffprofile_active.cpython-310.pyc b/osinaweb/osinacore/migrations/__pycache__/0107_remove_staffprofile_active.cpython-310.pyc new file mode 100644 index 00000000..f2682b9d Binary files /dev/null and b/osinaweb/osinacore/migrations/__pycache__/0107_remove_staffprofile_active.cpython-310.pyc differ diff --git a/osinaweb/osinacore/models.py b/osinaweb/osinacore/models.py index 8a288d62..16337aab 100644 --- a/osinaweb/osinacore/models.py +++ b/osinaweb/osinacore/models.py @@ -137,7 +137,6 @@ class StaffProfile(models.Model): image = models.ImageField(upload_to='uploaded_images', null=True, blank=True) mobile_number = models.CharField(max_length=50) intern = models.BooleanField(default=False) - active = models.BooleanField(default=True) staff_id = models.CharField(max_length=20, null=True, blank=True) # Allow null and blank for initial creation def __str__(self): return self.user.username @@ -168,6 +167,9 @@ class StaffProfile(models.Model): return f"last seen yesterday at {last_seen_time.strftime('%I:%M %p')}" else: return f"last seen on {last_seen_time.strftime('%b %d at %I:%M %p')}" + @property + def active(self): + return self.staffposition_set.filter(end_date__isnull=True).exists() diff --git a/osinaweb/static/js/osichat-admin/inner-conversation.js b/osinaweb/static/js/osichat-admin/inner-conversation.js index d4a76092..155a25dd 100644 --- a/osinaweb/static/js/osichat-admin/inner-conversation.js +++ b/osinaweb/static/js/osichat-admin/inner-conversation.js @@ -1,8 +1,7 @@ (function() { function handleChatRoomClick(event) { - const sessionId = event.currentTarget.getAttribute('data-session'); const chatId = event.currentTarget.getAttribute('data-roomid'); - if (sessionId && chatId && chatId !== currentChatId) { + if (chatId && chatId !== currentChatId) { showLoader(); openConversation(sessionId, chatId); currentChatId = chatId; @@ -69,12 +68,12 @@ } - function openConversation(sessionId, chatId) { + function openConversation(chatId) { if (osichatadminroomSocket) { osichatadminroomSocket.close(); } - osichatadminroomSocket = new WebSocket(`${admin_chat_ws_scheme}://${admin_chat_domain}/ws/osichat-admin/${sessionId}/${chatId}/`); + osichatadminroomSocket = new WebSocket(`${admin_chat_ws_scheme}://${admin_chat_domain}/ws/osichat-admin/${chatId}/`); osichatadminroomSocket.onopen = function () { hideLoader(); @@ -140,10 +139,10 @@ osichatadminroomSocket.onclose = function () { console.log('WebSocket connection closed'); - if (currentChatId === chatId) { + if (currentChatId === chatId) { //Attempt to reconnect only if same chat setTimeout(() => { console.log('Attempting to reconnect to WebSocket...'); - openConversation(sessionId, chatId); + openConversation(chatId); }, 2000); } };