Browse Source

lvesessions logic updated , avoids overwriting participants and session details

master
Mohsen Taba 2 months ago
parent
commit
800a2dd2b4
  1. 57
      apps/course/views/live_session.py

57
apps/course/views/live_session.py

@ -51,47 +51,32 @@ class CourseLiveSessionRoomCreateAPIView(GenericAPIView):
raise AppAPIException({'message': 'Permission denied'}, status_code=403) raise AppAPIException({'message': 'Permission denied'}, status_code=403)
# 2. Setup ID and Metadata # 2. Setup ID and Metadata
room_id = f"room-{course.id}-imamjavad"
subject = f"{course.title} Live Session" subject = f"{course.title} Live Session"
# 3. Database Logic - Check FIRST before calling PlugNMeet # 3. Database Logic - Check FIRST before calling PlugNMeet
# Strategy:
# 1. Try to find active session (ended_at is NULL)
# 2. If not found, try to find ended session with same room_id and reactivate it
# 3. If not found, create new session
session = None
session = CourseLiveSession.objects.filter(
course=course, ended_at__isnull=True
).first()
needs_room_creation = False needs_room_creation = False
try:
# Try to get active session first
session = CourseLiveSession.objects.get(
course=course, room_id=room_id, ended_at__isnull=True
)
needs_room_creation = False
if session:
room_id = session.room_id
logger.info(f"[LiveSession Create] Found active session - session_id={session.id} room_id={room_id}") logger.info(f"[LiveSession Create] Found active session - session_id={session.id} room_id={room_id}")
except CourseLiveSession.DoesNotExist:
# No active session, check if there's an old one with same room_id
try:
session = CourseLiveSession.objects.get(
course=course, room_id=room_id
)
# Reactivate the old session and mark for room recreation
session.ended_at = None
session.started_at = timezone.now()
session.subject = subject
session.save(update_fields=['ended_at', 'started_at', 'subject', 'updated_at'])
needs_room_creation = True
logger.info(f"[LiveSession Create] Reactivated ended session - session_id={session.id} room_id={room_id}")
except CourseLiveSession.DoesNotExist:
# No session exists at all, create new one and mark for room creation
session = CourseLiveSession.objects.create(
course=course,
room_id=room_id,
subject=subject,
started_at=timezone.now()
)
needs_room_creation = True
logger.info(f"[LiveSession Create] Created new session - session_id={session.id} room_id={room_id}")
else:
# No active session, always create a new one with a dynamic room_id
import time
timestamp = int(time.time())
room_id = f"room-{course.id}-{timestamp}"
session = CourseLiveSession.objects.create(
course=course,
room_id=room_id,
subject=subject,
started_at=timezone.now()
)
needs_room_creation = True
logger.info(f"[LiveSession Create] Created new session - session_id={session.id} room_id={room_id}")
# 4. Create room in PlugNMeet ONLY if needed # 4. Create room in PlugNMeet ONLY if needed
if needs_room_creation: if needs_room_creation:
@ -100,7 +85,7 @@ class CourseLiveSessionRoomCreateAPIView(GenericAPIView):
client = PlugNMeetClient() client = PlugNMeetClient()
plugnmeet_response = client.create_room({ plugnmeet_response = client.create_room({
'room_id': room_id, 'room_id': room_id,
'empty_timeout':90,
'empty_timeout': 90,
'metadata': metadata, 'metadata': metadata,
}) })
logger.info(f"[LiveSession Create] Room created in PlugNMeet - room_id={room_id}") logger.info(f"[LiveSession Create] Room created in PlugNMeet - room_id={room_id}")

Loading…
Cancel
Save