✨(frontend) notify participant when her was muted
Make sure a participant is notified when another one muted her. It will help display a notification to alert the participant.
This commit is contained in:
committed by
aleb_the_flash
parent
fc36ae8b49
commit
bd4fcc2a5e
@@ -1,5 +1,6 @@
|
|||||||
export enum NotificationType {
|
export enum NotificationType {
|
||||||
ParticipantJoined = 'participantJoined',
|
ParticipantJoined = 'participantJoined',
|
||||||
HandRaised = 'handRaised',
|
HandRaised = 'handRaised',
|
||||||
|
ParticipantMuted = 'participantMuted',
|
||||||
// todo - implement message received notification
|
// todo - implement message received notification
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,23 @@ import Source = Track.Source
|
|||||||
import { fetchServerApi } from './fetchServerApi'
|
import { fetchServerApi } from './fetchServerApi'
|
||||||
import { buildServerApiUrl } from './buildServerApiUrl'
|
import { buildServerApiUrl } from './buildServerApiUrl'
|
||||||
import { useRoomData } from '../hooks/useRoomData'
|
import { useRoomData } from '../hooks/useRoomData'
|
||||||
|
import { useRoomContext } from '@livekit/components-react'
|
||||||
|
import { NotificationType } from '@/features/notifications/NotificationType'
|
||||||
|
|
||||||
export const useMuteParticipant = () => {
|
export const useMuteParticipant = () => {
|
||||||
const data = useRoomData()
|
const data = useRoomData()
|
||||||
|
const room = useRoomContext()
|
||||||
|
|
||||||
const muteParticipant = (participant: Participant) => {
|
const notifyParticipant = async (participant: Participant) => {
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const data = encoder.encode(NotificationType.ParticipantMuted)
|
||||||
|
await room.localParticipant.publishData(data, {
|
||||||
|
reliable: true,
|
||||||
|
destinationIdentities: [participant.identity],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const muteParticipant = async (participant: Participant) => {
|
||||||
if (!data || !data?.livekit) {
|
if (!data || !data?.livekit) {
|
||||||
throw new Error('Room data is not available')
|
throw new Error('Room data is not available')
|
||||||
}
|
}
|
||||||
@@ -18,22 +30,33 @@ export const useMuteParticipant = () => {
|
|||||||
if (!trackSid) {
|
if (!trackSid) {
|
||||||
throw new Error('Missing audio track')
|
throw new Error('Missing audio track')
|
||||||
}
|
}
|
||||||
return fetchServerApi(
|
|
||||||
buildServerApiUrl(
|
try {
|
||||||
data.livekit.url,
|
const response = await fetchServerApi(
|
||||||
'twirp/livekit.RoomService/MutePublishedTrack'
|
buildServerApiUrl(
|
||||||
),
|
data.livekit.url,
|
||||||
data.livekit.token,
|
'twirp/livekit.RoomService/MutePublishedTrack'
|
||||||
{
|
),
|
||||||
method: 'POST',
|
data.livekit.token,
|
||||||
body: JSON.stringify({
|
{
|
||||||
room: data.livekit.room,
|
method: 'POST',
|
||||||
identity: participant.identity,
|
body: JSON.stringify({
|
||||||
muted: true,
|
room: data.livekit.room,
|
||||||
track_sid: trackSid,
|
identity: participant.identity,
|
||||||
}),
|
muted: true,
|
||||||
}
|
track_sid: trackSid,
|
||||||
)
|
}),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
await notifyParticipant(participant)
|
||||||
|
|
||||||
|
return response
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`Failed to mute participant ${participant.identity}: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return { muteParticipant }
|
return { muteParticipant }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user