|
|
|
@ -10,7 +10,14 @@ import threading
|
|
|
|
|
class OnlineUserConsumer(WebsocketConsumer):
|
|
|
|
|
def connect(self):
|
|
|
|
|
self.user = self.scope['user']
|
|
|
|
|
|
|
|
|
|
existing_connection = Connection.objects.filter(user=self.user).last()
|
|
|
|
|
if existing_connection:
|
|
|
|
|
self.connection = existing_connection
|
|
|
|
|
self.connection.online = True
|
|
|
|
|
self.connection.disconnected = False
|
|
|
|
|
self.connection.save()
|
|
|
|
|
else:
|
|
|
|
|
self.connection = Connection.objects.create(user=self.user, online=True)
|
|
|
|
|
|
|
|
|
|
async_to_sync(self.channel_layer.group_add)(
|
|
|
|
|
'online_users', self.channel_name
|
|
|
|
@ -34,11 +41,17 @@ class OnlineUserConsumer(WebsocketConsumer):
|
|
|
|
|
self.modify_online_user()
|
|
|
|
|
|
|
|
|
|
def modify_online_user(self):
|
|
|
|
|
|
|
|
|
|
connections = Connection.objects.all()
|
|
|
|
|
online_connections = connections.filter(online=True)
|
|
|
|
|
offline_connections = connections.filter(online=False, last_seen__isnull=False).order_by('-last_seen')[:5]
|
|
|
|
|
sorted_connections = list(online_connections) + list(offline_connections)
|
|
|
|
|
online_users_ids = [connection.user.id for connection in online_connections]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event = {
|
|
|
|
|
'type': 'online_user_connection_handler',
|
|
|
|
|
|
|
|
|
|
'online_users_ids': online_users_ids
|
|
|
|
|
}
|
|
|
|
|
async_to_sync(self.channel_layer.group_send)(
|
|
|
|
|
'online_users', event
|
|
|
|
@ -46,13 +59,13 @@ class OnlineUserConsumer(WebsocketConsumer):
|
|
|
|
|
|
|
|
|
|
def online_user_connection_handler(self, event):
|
|
|
|
|
context = {
|
|
|
|
|
|
|
|
|
|
'customer_connections': event['customer_connections'],
|
|
|
|
|
}
|
|
|
|
|
html = render_to_string("details_templates/partials/recently-online.html", context=context)
|
|
|
|
|
self.send(text_data=json.dumps({
|
|
|
|
|
'event_type': 'online_user_status',
|
|
|
|
|
'html': html,
|
|
|
|
|
|
|
|
|
|
'online_users_ids': event.get('online_users_ids', [])
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|