|
|
@ -59,6 +59,7 @@ class OsitcomVisitor(WebsocketConsumer):
|
|
|
|
|
|
|
|
|
|
|
|
class Osichat(WebsocketConsumer):
|
|
|
|
class Osichat(WebsocketConsumer):
|
|
|
|
def connect(self):
|
|
|
|
def connect(self):
|
|
|
|
|
|
|
|
self.user = self.scope["user"]
|
|
|
|
async_to_sync(self.channel_layer.group_add)(
|
|
|
|
async_to_sync(self.channel_layer.group_add)(
|
|
|
|
'osichat', self.channel_name
|
|
|
|
'osichat', self.channel_name
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -75,7 +76,6 @@ class Osichat(WebsocketConsumer):
|
|
|
|
|
|
|
|
|
|
|
|
if event_type == 'set_client_type':
|
|
|
|
if event_type == 'set_client_type':
|
|
|
|
self.client_type = data.get('client_type')
|
|
|
|
self.client_type = data.get('client_type')
|
|
|
|
self.user_id = data.get('user_id')
|
|
|
|
|
|
|
|
event = {
|
|
|
|
event = {
|
|
|
|
'type': 'get_chats_handler',
|
|
|
|
'type': 'get_chats_handler',
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -112,10 +112,8 @@ class Osichat(WebsocketConsumer):
|
|
|
|
default=F('last_update'),
|
|
|
|
default=F('last_update'),
|
|
|
|
output_field=DateTimeField(),)).filter(chatroomguest__isnull=False).order_by('-order_key')
|
|
|
|
output_field=DateTimeField(),)).filter(chatroomguest__isnull=False).order_by('-order_key')
|
|
|
|
|
|
|
|
|
|
|
|
user = get_object_or_404(User, id=self.user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for room in chat_rooms:
|
|
|
|
for room in chat_rooms:
|
|
|
|
room.number_of_unread = room.unread_messages(user)
|
|
|
|
room.number_of_unread = room.unread_messages(self.user)
|
|
|
|
context = {
|
|
|
|
context = {
|
|
|
|
'chat_rooms': chat_rooms,
|
|
|
|
'chat_rooms': chat_rooms,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -125,7 +123,7 @@ class Osichat(WebsocketConsumer):
|
|
|
|
for chat_room in chat_rooms:
|
|
|
|
for chat_room in chat_rooms:
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
room_data = model_to_dict(chat_room)
|
|
|
|
room_data = model_to_dict(chat_room)
|
|
|
|
room_data['number_of_unread'] = chat_room.unread_messages(user)
|
|
|
|
room_data['number_of_unread'] = chat_room.unread_messages(self.user)
|
|
|
|
if last_message:
|
|
|
|
if last_message:
|
|
|
|
room_data['last_message'] = model_to_dict(last_message)
|
|
|
|
room_data['last_message'] = model_to_dict(last_message)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -158,8 +156,7 @@ class Osichat(WebsocketConsumer):
|
|
|
|
|
|
|
|
|
|
|
|
def new_chat_update_handler(self, event):
|
|
|
|
def new_chat_update_handler(self, event):
|
|
|
|
chat_room = get_object_or_404(ChatRoom, id=event['chatroom_id'])
|
|
|
|
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=self.user).exclude(chatmessageseen__member=self.user).count()
|
|
|
|
number_of_unread = ChatMessage.objects.filter(room=chat_room).exclude(member=user).exclude(chatmessageseen__member=user).count()
|
|
|
|
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
|
|
|
|
|
|
|
|
context = {
|
|
|
|
context = {
|
|
|
@ -189,18 +186,16 @@ class Osichat(WebsocketConsumer):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_dms_handler(self, event):
|
|
|
|
def get_dms_handler(self, event):
|
|
|
|
if self.scope["user"].is_authenticated:
|
|
|
|
|
|
|
|
chat_rooms = ChatRoom.objects.annotate(last_update=Max('chatmessage__date_sent'),order_key=Case(
|
|
|
|
chat_rooms = ChatRoom.objects.annotate(last_update=Max('chatmessage__date_sent'),order_key=Case(
|
|
|
|
When(last_update__isnull=True, then=F('date_created')),
|
|
|
|
When(last_update__isnull=True, then=F('date_created')),
|
|
|
|
default=F('last_update'),
|
|
|
|
default=F('last_update'),
|
|
|
|
output_field=DateTimeField(),)).filter(chatroomguest__isnull=True, chatmember__member=self.scope["user"]).order_by('-order_key')
|
|
|
|
output_field=DateTimeField(),)).filter(chatroomguest__isnull=True, chatmember__member=self.user).order_by('-order_key')
|
|
|
|
else:
|
|
|
|
|
|
|
|
chat_rooms = None
|
|
|
|
|
|
|
|
context = {
|
|
|
|
context = {
|
|
|
|
'chat_rooms': chat_rooms,
|
|
|
|
'chat_rooms': chat_rooms,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.client_type == 'mobile_admin':
|
|
|
|
if self.client_type == 'mobile_admin':
|
|
|
|
chat_rooms_data = []
|
|
|
|
chat_rooms_data = []
|
|
|
|
|
|
|
|
if chat_rooms:
|
|
|
|
for chat_room in chat_rooms:
|
|
|
|
for chat_room in chat_rooms:
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
last_message = ChatMessage.objects.filter(room=chat_room).last()
|
|
|
|
chat_room_data = {
|
|
|
|
chat_room_data = {
|
|
|
@ -208,7 +203,7 @@ class Osichat(WebsocketConsumer):
|
|
|
|
'name': chat_room.name,
|
|
|
|
'name': chat_room.name,
|
|
|
|
'last_update': chat_room.last_updated,
|
|
|
|
'last_update': chat_room.last_updated,
|
|
|
|
'date_created': chat_room.date_created,
|
|
|
|
'date_created': chat_room.date_created,
|
|
|
|
'unread_messages': chat_room.unread_messages(self.scope["user"]),
|
|
|
|
'unread_messages': chat_room.unread_messages(self.user),
|
|
|
|
'last_message': model_to_dict(last_message) if last_message else None
|
|
|
|
'last_message': model_to_dict(last_message) if last_message else None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
members = chat_room.chatmember_set.all()
|
|
|
|
members = chat_room.chatmember_set.all()
|
|
|
|