From 126834640580960885bb3dde7ca0c7416ed0fd9e Mon Sep 17 00:00:00 2001 From: lebaudantoine Date: Wed, 27 Aug 2025 15:29:16 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(backend)=20replace=20LiveKit?= =?UTF-8?q?=20token=20metadata=20with=20attributes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switch from metadata to attributes when generating LiveKit tokens for more convenient dict-like structure handling during token creation and client-side reading. Attributes provide better data structure flexibility compared to metadata, simplifying both server-side token generation and client-side data access patterns. --- src/backend/core/utils.py | 4 ++- .../rooms/utils/getParticipantColor.ts | 25 ++++--------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/src/backend/core/utils.py b/src/backend/core/utils.py index ce6e17c9..1289cb8d 100644 --- a/src/backend/core/utils.py +++ b/src/backend/core/utils.py @@ -110,7 +110,9 @@ def generate_token( .with_grants(video_grants) .with_identity(identity) .with_name(username or default_username) - .with_metadata(json.dumps({"color": color, "room_admin": is_admin_or_owner})) + .with_attributes( + {"color": color, "room_admin": "true" if is_admin_or_owner else "false"} + ) ) return token.to_jwt() diff --git a/src/frontend/src/features/rooms/utils/getParticipantColor.ts b/src/frontend/src/features/rooms/utils/getParticipantColor.ts index b58d43be..72a6e4ef 100644 --- a/src/frontend/src/features/rooms/utils/getParticipantColor.ts +++ b/src/frontend/src/features/rooms/utils/getParticipantColor.ts @@ -10,31 +10,16 @@ function isValidHsl(colorString: string) { } export const getParticipantColor = (participant: Participant): string => { - const { metadata } = participant + const attributes = participant.attributes - if (!metadata) { + if (!attributes?.color) { return DEFAULT_COLOR } - let parsedMetadata: unknown - - try { - parsedMetadata = JSON.parse(metadata) - } catch (error) { - console.error('Invalid JSON in participant metadata:', error) + if (!isValidHsl(attributes.color)) { + console.warn('Invalid color value:', attributes.color) return DEFAULT_COLOR } - if (!parsedMetadata || typeof parsedMetadata !== 'object') { - return DEFAULT_COLOR - } - - const colorValue = (parsedMetadata as Record)['color'] - - if (typeof colorValue !== 'string' || !isValidHsl(colorValue)) { - console.error('Invalid color value:', colorValue) - return DEFAULT_COLOR - } - - return colorValue + return attributes.color }