|
20 | 20 | userId: string; |
21 | 21 | name: string; |
22 | 22 | image?: string; |
| 23 | + isHost?: boolean; // Add flag to track host status |
| 24 | + email?: string; // Add email for stable identification |
23 | 25 | } |
24 | 26 |
|
25 | 27 | let roomId = page.params.roomId; |
|
28 | 30 | let error = $state<string | null>(null); |
29 | 31 | let loading = $state(true); |
30 | 32 | let creatorId = $state<string | null>(null); |
| 33 | + let roomCreationTime = $state<number>(Date.now()); // Track room creation time |
31 | 34 |
|
32 | 35 | // Agora related variables - only need client and tracks |
33 | 36 | let agoraClient = $state<IAgoraRTCClient | null>(null); |
|
226 | 229 | } |
227 | 230 | }); |
228 | 231 |
|
229 | | - // Determine host (first user or lowest UID) |
| 232 | + // Determine host - first joiner or based on email if authentication is used |
| 233 | + let isCurrentUserHost = false; |
| 234 | + const userEmail = $session.data?.user?.email; |
| 235 | +
|
230 | 236 | if (remoteUsersList.length === 0) { |
231 | | - creatorId = uid.toString(); |
232 | | - console.log(`[AGORA] No other users found, setting self as host with UID ${uid}`); |
| 237 | + // First user to join becomes host |
| 238 | + isCurrentUserHost = true; |
| 239 | + console.log(`[AGORA] No other users found, setting self as host`); |
233 | 240 | } else { |
234 | | - // If there are remote users, use the lowest UID as creator |
235 | | - const allUids = [uid.toString(), ...remoteUsersList.map((u) => u.uid.toString())]; |
236 | | - const lowestUid = allUids.sort()[0]; |
237 | | - creatorId = lowestUid; |
238 | | - console.log(`[AGORA] Setting ${lowestUid} as host`); |
| 241 | + // Check participants to see if there's already a host |
| 242 | + const existingHost = participants.find(p => p.isHost); |
| 243 | +
|
| 244 | + // If no host is set yet, the first authenticated user becomes host |
| 245 | + if (!existingHost && userEmail) { |
| 246 | + isCurrentUserHost = true; |
| 247 | + console.log(`[AGORA] No host exists yet, setting authenticated user as host`); |
| 248 | + } |
239 | 249 | } |
240 | 250 |
|
241 | 251 | // Add current user to participants |
242 | 252 | const userName = $session.data?.user?.name || `You (${uid})`; |
243 | 253 | participants = [ |
244 | 254 | ...participants |
245 | 255 | .filter((p) => p.userId !== uid.toString()) |
246 | | - .map((p) => ({ ...p, image: p.image ?? undefined })), |
| 256 | + .map((p) => ({ ...p })), |
247 | 257 | { |
248 | 258 | userId: uid.toString(), |
249 | 259 | name: userName, |
250 | | - image: $session.data?.user?.image ?? undefined |
| 260 | + image: $session.data?.user?.image ?? undefined, |
| 261 | + email: userEmail, |
| 262 | + isHost: isCurrentUserHost |
251 | 263 | } |
252 | 264 | ]; |
253 | 265 |
|
|
441 | 453 | } |
442 | 454 | }); |
443 | 455 |
|
444 | | - let isHost = $derived($session.data?.user?.id === creatorId); |
| 456 | + // Determine if current user is host |
| 457 | + let isHost = $derived(() => { |
| 458 | + const currentUserId = agoraClient?.uid?.toString(); |
| 459 | + const currentUserParticipant = participants.find(p => p.userId === currentUserId); |
| 460 | + return !!currentUserParticipant?.isHost; |
| 461 | + }); |
445 | 462 | </script> |
446 | 463 |
|
447 | 464 | <!-- The UI remains similar but updates for our simplified approach --> |
|
559 | 576 | </div> |
560 | 577 | {/if} |
561 | 578 | <span>{participant.name}</span> |
562 | | - {#if participant.userId === creatorId} |
| 579 | + {#if participant.isHost} |
563 | 580 | <Badge variant="secondary" class="ml-2">Host</Badge> |
564 | 581 | {/if} |
565 | 582 | </div> |
|
0 commit comments