✨(frontend) add client-side API call for participant permission updates
Implement frontend functionality to call backend endpoints for updating specific participant permissions through proper server-mediated requests.
This commit is contained in:
committed by
aleb_the_flash
parent
1539613bf8
commit
84c820bc60
@@ -0,0 +1,41 @@
|
||||
import { Participant, Track } from 'livekit-client'
|
||||
import { fetchApi } from '@/api/fetchApi'
|
||||
import { useRoomData } from '@/features/rooms/livekit/hooks/useRoomData'
|
||||
import Source = Track.Source
|
||||
|
||||
export const useParticipantPermissions = () => {
|
||||
const data = useRoomData()
|
||||
|
||||
const updateParticipantPermissions = async (
|
||||
participant: Participant,
|
||||
sources: Array<Source>
|
||||
) => {
|
||||
if (!data?.id) {
|
||||
throw new Error('Room id is not available')
|
||||
}
|
||||
|
||||
const newPermissions = {
|
||||
can_subscribe: participant.permissions?.canSubscribe,
|
||||
can_publish_data: participant.permissions?.canPublishData,
|
||||
can_update_metadata: participant.permissions?.canUpdateMetadata,
|
||||
can_subscribe_metrics: participant.permissions?.canSubscribeMetrics,
|
||||
can_publish: sources.length > 0,
|
||||
can_publish_sources: sources.map((source) => source.toUpperCase()),
|
||||
}
|
||||
|
||||
try {
|
||||
return fetchApi(`rooms/${data.id}/update-participant/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
participant_identity: participant.identity,
|
||||
permission: newPermissions,
|
||||
}),
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to update participant's permissions ${participant.identity}: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
)
|
||||
}
|
||||
}
|
||||
return { updateParticipantPermissions }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Participant, Track } from 'livekit-client'
|
||||
import { useParticipantPermissions } from './updateParticipantPermissions'
|
||||
type Source = Track.Source
|
||||
|
||||
export const useUpdateParticipantsPermissions = () => {
|
||||
const { updateParticipantPermissions } = useParticipantPermissions()
|
||||
|
||||
const updateParticipantsPermissions = (
|
||||
participants: Array<Participant>,
|
||||
sources: Array<Source>
|
||||
) => {
|
||||
try {
|
||||
const promises = participants.map((participant) =>
|
||||
updateParticipantPermissions(participant, sources)
|
||||
)
|
||||
return Promise.all(promises)
|
||||
} catch (error) {
|
||||
console.error('An error occurred while updating permissions :', error)
|
||||
throw new Error('An error occurred while updating permissions.')
|
||||
}
|
||||
}
|
||||
return { updateParticipantsPermissions }
|
||||
}
|
||||
Reference in New Issue
Block a user