emile 8 months ago
parent 30a644dc33
commit 444ca94669

Binary file not shown.

@ -14,7 +14,6 @@ def get_chat_rooms(request):
@api_view(['GET']) @api_view(['GET'])
def get_visitors(request): def get_visitors(request):
start_date = request.query_params.get("start_date") 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))

@ -328,8 +328,9 @@ class OsitcomChatRoom(WebsocketConsumer):
def connect(self): def connect(self):
self.domain = 'https://osina.ositcom.com' self.domain = 'https://osina.ositcom.com'
self.session_id = self.scope['url_route']['kwargs']['session_id'] self.session_id = self.scope['url_route']['kwargs']['session_id']
self.visitor = Visitor.objects.filter(session_id=self.session_id).last() if self.session_id:
if not self.visitor: self.visitor = Visitor.objects.filter(session_id=self.session_id).last()
if self.session_id and not self.visitor:
self.close() 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 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: else:
self.chat_room = None 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: if self.chat_room:
self.group = f"{self.session_id}_{self.chat_room.id}" self.group = f"{self.session_id}_{self.chat_room.id}"
else: 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)( async_to_sync(self.channel_layer.group_add)(
self.group, self.channel_name self.group, self.channel_name

@ -5,7 +5,7 @@ websocket_urlpatterns = [
path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()), path("ws/osichat/visitors/", OsitcomVisitor.as_asgi()),
path("ws/osichat/", Osichat.as_asgi()), path("ws/osichat/", Osichat.as_asgi()),
path("ws/osichat/<str:session_id>/", OsitcomChatRoom.as_asgi()), path("ws/osichat/<str:session_id>/", OsitcomChatRoom.as_asgi()),
path("ws/osichat-admin/<str:session_id>/<int:chat_id>/", OsitcomChatRoom.as_asgi()), path("ws/osichat-admin/<int:chat_id>/", OsitcomChatRoom.as_asgi()),
path("ws/visitors/osichat-admin/<int:visitor_id>/", OsichatVisitor.as_asgi()), path("ws/visitors/osichat-admin/<int:visitor_id>/", OsichatVisitor.as_asgi()),
] ]

@ -166,7 +166,6 @@ def add_staff(request):
user=user, user=user,
image=request.FILES.get('image'), image=request.FILES.get('image'),
mobile_number=request.POST.get('mobile_number'), mobile_number=request.POST.get('mobile_number'),
active=active,
intern=intern, intern=intern,
) )

@ -119,7 +119,6 @@ def edit_staff(request, staff_id):
staff.mobile_number = request.POST.get('mobile_number') staff.mobile_number = request.POST.get('mobile_number')
staff.active = active
new_image = request.FILES.get('image') new_image = request.FILES.get('image')
if new_image: if new_image:

@ -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',
),
]

@ -137,7 +137,6 @@ class StaffProfile(models.Model):
image = models.ImageField(upload_to='uploaded_images', null=True, blank=True) image = models.ImageField(upload_to='uploaded_images', null=True, blank=True)
mobile_number = models.CharField(max_length=50) mobile_number = models.CharField(max_length=50)
intern = models.BooleanField(default=False) 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 staff_id = models.CharField(max_length=20, null=True, blank=True) # Allow null and blank for initial creation
def __str__(self): def __str__(self):
return self.user.username 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')}" return f"last seen yesterday at {last_seen_time.strftime('%I:%M %p')}"
else: else:
return f"last seen on {last_seen_time.strftime('%b %d at %I:%M %p')}" 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()

@ -1,8 +1,7 @@
(function() { (function() {
function handleChatRoomClick(event) { function handleChatRoomClick(event) {
const sessionId = event.currentTarget.getAttribute('data-session');
const chatId = event.currentTarget.getAttribute('data-roomid'); const chatId = event.currentTarget.getAttribute('data-roomid');
if (sessionId && chatId && chatId !== currentChatId) { if (chatId && chatId !== currentChatId) {
showLoader(); showLoader();
openConversation(sessionId, chatId); openConversation(sessionId, chatId);
currentChatId = chatId; currentChatId = chatId;
@ -69,12 +68,12 @@
} }
function openConversation(sessionId, chatId) { function openConversation(chatId) {
if (osichatadminroomSocket) { if (osichatadminroomSocket) {
osichatadminroomSocket.close(); 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 () { osichatadminroomSocket.onopen = function () {
hideLoader(); hideLoader();
@ -140,10 +139,10 @@
osichatadminroomSocket.onclose = function () { osichatadminroomSocket.onclose = function () {
console.log('WebSocket connection closed'); console.log('WebSocket connection closed');
if (currentChatId === chatId) { if (currentChatId === chatId) { //Attempt to reconnect only if same chat
setTimeout(() => { setTimeout(() => {
console.log('Attempting to reconnect to WebSocket...'); console.log('Attempting to reconnect to WebSocket...');
openConversation(sessionId, chatId); openConversation(chatId);
}, 2000); }, 2000);
} }
}; };

Loading…
Cancel
Save