Refactor participant metadata updates to avoid duplication (#1679)

* Refactor participant metadata updates to avoid duplication

* generated fakes
This commit is contained in:
David Zhao
2023-05-03 13:50:45 -07:00
committed by GitHub
parent 4bd20c77b7
commit 5fcd682fb0
6 changed files with 58 additions and 15 deletions

View File

@@ -649,6 +649,15 @@ func (r *Room) SetMetadata(metadata string) {
r.protoProxy.MarkDirty(true)
}
func (r *Room) UpdateParticipantMetadata(participant types.LocalParticipant, name string, metadata string) {
if metadata != "" {
participant.SetMetadata(metadata)
}
if name != "" {
participant.SetName(name)
}
}
func (r *Room) sendRoomUpdate() {
roomInfo := r.ToProto()
// Send update to participants

View File

@@ -76,12 +76,7 @@ func HandleParticipantSignal(room types.Room, participant types.LocalParticipant
case *livekit.SignalRequest_UpdateMetadata:
if participant.ClaimGrants().Video.GetCanUpdateOwnMetadata() {
if msg.UpdateMetadata.Metadata != "" {
participant.SetMetadata(msg.UpdateMetadata.Metadata)
}
if msg.UpdateMetadata.Name != "" {
participant.SetName(msg.UpdateMetadata.Name)
}
room.UpdateParticipantMetadata(participant, msg.UpdateMetadata.Name, msg.UpdateMetadata.Metadata)
}
}
return nil

View File

@@ -391,7 +391,7 @@ func (m *SubscriptionManager) reconcileWorker() {
}
}
func (m *SubscriptionManager) hasCapcityForSubscription(kind livekit.TrackType) bool {
func (m *SubscriptionManager) hasCapacityForSubscription(kind livekit.TrackType) bool {
switch kind {
case livekit.TrackType_VIDEO:
if m.params.SubscriptionLimitVideo > 0 && m.subscribedVideoCount.Load() >= m.params.SubscriptionLimitVideo {
@@ -413,7 +413,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error {
return ErrNoSubscribePermission
}
if kind, ok := s.getKind(); ok && !m.hasCapcityForSubscription(kind) {
if kind, ok := s.getKind(); ok && !m.hasCapacityForSubscription(kind) {
return ErrSubscriptionLimitExceeded
}
@@ -445,7 +445,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error {
return ErrTrackNotFound
}
s.trySetKind(track.Kind())
if !m.hasCapcityForSubscription(track.Kind()) {
if !m.hasCapacityForSubscription(track.Kind()) {
return ErrSubscriptionLimitExceeded
}

View File

@@ -357,6 +357,7 @@ type Room interface {
UpdateVideoLayers(participant Participant, updateVideoLayers *livekit.UpdateVideoLayers) error
ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIdentity, trackID livekit.TrackID) MediaResolverResult
GetLocalParticipants() []LocalParticipant
UpdateParticipantMetadata(participant LocalParticipant, name string, metadata string)
}
// MediaTrack represents a media track

View File

@@ -82,6 +82,13 @@ type FakeRoom struct {
syncStateReturnsOnCall map[int]struct {
result1 error
}
UpdateParticipantMetadataStub func(types.LocalParticipant, string, string)
updateParticipantMetadataMutex sync.RWMutex
updateParticipantMetadataArgsForCall []struct {
arg1 types.LocalParticipant
arg2 string
arg3 string
}
UpdateSubscriptionPermissionStub func(types.LocalParticipant, *livekit.SubscriptionPermission) error
updateSubscriptionPermissionMutex sync.RWMutex
updateSubscriptionPermissionArgsForCall []struct {
@@ -497,6 +504,40 @@ func (fake *FakeRoom) SyncStateReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRoom) UpdateParticipantMetadata(arg1 types.LocalParticipant, arg2 string, arg3 string) {
fake.updateParticipantMetadataMutex.Lock()
fake.updateParticipantMetadataArgsForCall = append(fake.updateParticipantMetadataArgsForCall, struct {
arg1 types.LocalParticipant
arg2 string
arg3 string
}{arg1, arg2, arg3})
stub := fake.UpdateParticipantMetadataStub
fake.recordInvocation("UpdateParticipantMetadata", []interface{}{arg1, arg2, arg3})
fake.updateParticipantMetadataMutex.Unlock()
if stub != nil {
fake.UpdateParticipantMetadataStub(arg1, arg2, arg3)
}
}
func (fake *FakeRoom) UpdateParticipantMetadataCallCount() int {
fake.updateParticipantMetadataMutex.RLock()
defer fake.updateParticipantMetadataMutex.RUnlock()
return len(fake.updateParticipantMetadataArgsForCall)
}
func (fake *FakeRoom) UpdateParticipantMetadataCalls(stub func(types.LocalParticipant, string, string)) {
fake.updateParticipantMetadataMutex.Lock()
defer fake.updateParticipantMetadataMutex.Unlock()
fake.UpdateParticipantMetadataStub = stub
}
func (fake *FakeRoom) UpdateParticipantMetadataArgsForCall(i int) (types.LocalParticipant, string, string) {
fake.updateParticipantMetadataMutex.RLock()
defer fake.updateParticipantMetadataMutex.RUnlock()
argsForCall := fake.updateParticipantMetadataArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
}
func (fake *FakeRoom) UpdateSubscriptionPermission(arg1 types.LocalParticipant, arg2 *livekit.SubscriptionPermission) error {
fake.updateSubscriptionPermissionMutex.Lock()
ret, specificReturn := fake.updateSubscriptionPermissionReturnsOnCall[len(fake.updateSubscriptionPermissionArgsForCall)]
@@ -683,6 +724,8 @@ func (fake *FakeRoom) Invocations() map[string][][]interface{} {
defer fake.simulateScenarioMutex.RUnlock()
fake.syncStateMutex.RLock()
defer fake.syncStateMutex.RUnlock()
fake.updateParticipantMetadataMutex.RLock()
defer fake.updateParticipantMetadataMutex.RUnlock()
fake.updateSubscriptionPermissionMutex.RLock()
defer fake.updateSubscriptionPermissionMutex.RUnlock()
fake.updateSubscriptionsMutex.RLock()

View File

@@ -590,12 +590,7 @@ func (r *RoomManager) handleRTCMessage(ctx context.Context, roomName livekit.Roo
}
pLogger.Debugw("updating participant", "metadata", rm.UpdateParticipant.Metadata,
"permission", rm.UpdateParticipant.Permission)
if rm.UpdateParticipant.Name != "" {
participant.SetName(rm.UpdateParticipant.Name)
}
if rm.UpdateParticipant.Metadata != "" {
participant.SetMetadata(rm.UpdateParticipant.Metadata)
}
room.UpdateParticipantMetadata(participant, rm.UpdateParticipant.Name, rm.UpdateParticipant.Metadata)
if rm.UpdateParticipant.Permission != nil {
participant.SetPermission(rm.UpdateParticipant.Permission)
}