add more multiuser support
This commit is contained in:
+26
-2
@@ -2,21 +2,39 @@ import logging
|
||||
from channels.generic.websocket import AsyncJsonWebsocketConsumer
|
||||
from django.core.cache import cache
|
||||
from asgiref.sync import sync_to_async
|
||||
from channels.db import database_sync_to_async
|
||||
from django.core.exceptions import ValidationError
|
||||
from atlas.models import MultiuserSession
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class MultiuserConsumer(AsyncJsonWebsocketConsumer):
|
||||
@database_sync_to_async
|
||||
def get_session(self, session_id):
|
||||
try:
|
||||
return MultiuserSession.objects.select_related('creator').get(id=session_id)
|
||||
except (MultiuserSession.DoesNotExist, ValidationError):
|
||||
return None
|
||||
|
||||
async def connect(self):
|
||||
self.room_name = self.scope['url_route']['kwargs']['room_name']
|
||||
self.room_group_name = f'multiuser_{self.room_name}'
|
||||
self.user = self.scope.get('user')
|
||||
|
||||
# Get session from DB using self.room_name
|
||||
session = await self.get_session(self.room_name)
|
||||
if not session:
|
||||
# Reject connection if session does not exist
|
||||
await self.close()
|
||||
return
|
||||
|
||||
# Determine username and staff privilege
|
||||
if self.user and self.user.is_authenticated:
|
||||
self.username = self.user.username
|
||||
self.is_staff = self.user.is_staff
|
||||
# Manager is the creator or a site admin/staff
|
||||
self.is_staff = (self.user == session.creator) or self.user.is_staff
|
||||
else:
|
||||
self.username = 'Anonymous'
|
||||
self.username = f"Guest_{self.channel_name[-4:]}"
|
||||
self.is_staff = False
|
||||
|
||||
# Add to channels group
|
||||
@@ -129,6 +147,12 @@ class MultiuserConsumer(AsyncJsonWebsocketConsumer):
|
||||
'controller_username': event['controller_username']
|
||||
})
|
||||
|
||||
async def change_case(self, event):
|
||||
await self.send_json({
|
||||
'type': 'change_case',
|
||||
'case_id': event['case_id']
|
||||
})
|
||||
|
||||
# Cache state helpers (using sync_to_async for thread-safe Memcached/Redis interactions)
|
||||
def get_cache_key(self):
|
||||
return f"multiuser_room_{self.room_name}"
|
||||
|
||||
+1
-1
@@ -2,5 +2,5 @@ from django.urls import re_path
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r'^ws/multiuser/(?P<room_name>\w+)/$', consumers.MultiuserConsumer.as_asgi()),
|
||||
re_path(r'^ws/multiuser/(?P<room_name>[\w-]+)/$', consumers.MultiuserConsumer.as_asgi()),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user