♻️(backend) replace LiveKit token metadata with attributes
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.
This commit is contained in:
committed by
aleb_the_flash
parent
f28da45b4c
commit
1268346405
@@ -110,7 +110,9 @@ def generate_token(
|
|||||||
.with_grants(video_grants)
|
.with_grants(video_grants)
|
||||||
.with_identity(identity)
|
.with_identity(identity)
|
||||||
.with_name(username or default_username)
|
.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()
|
return token.to_jwt()
|
||||||
|
|||||||
@@ -10,31 +10,16 @@ function isValidHsl(colorString: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getParticipantColor = (participant: Participant): string => {
|
export const getParticipantColor = (participant: Participant): string => {
|
||||||
const { metadata } = participant
|
const attributes = participant.attributes
|
||||||
|
|
||||||
if (!metadata) {
|
if (!attributes?.color) {
|
||||||
return DEFAULT_COLOR
|
return DEFAULT_COLOR
|
||||||
}
|
}
|
||||||
|
|
||||||
let parsedMetadata: unknown
|
if (!isValidHsl(attributes.color)) {
|
||||||
|
console.warn('Invalid color value:', attributes.color)
|
||||||
try {
|
|
||||||
parsedMetadata = JSON.parse(metadata)
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Invalid JSON in participant metadata:', error)
|
|
||||||
return DEFAULT_COLOR
|
return DEFAULT_COLOR
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parsedMetadata || typeof parsedMetadata !== 'object') {
|
return attributes.color
|
||||||
return DEFAULT_COLOR
|
|
||||||
}
|
|
||||||
|
|
||||||
const colorValue = (parsedMetadata as Record<string, unknown>)['color']
|
|
||||||
|
|
||||||
if (typeof colorValue !== 'string' || !isValidHsl(colorValue)) {
|
|
||||||
console.error('Invalid color value:', colorValue)
|
|
||||||
return DEFAULT_COLOR
|
|
||||||
}
|
|
||||||
|
|
||||||
return colorValue
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user