davidliu
Committed by GitHub

Add RoomEvent.ParticipantAttributesChanged (#473)

* Add RoomEvent.ParticipantAttributesChanged

* fix test
... ... @@ -89,6 +89,22 @@ sealed class RoomEvent(val room: Room) : Event() {
val prevMetadata: String?,
) : RoomEvent(room)
/**
* When a participant's attributes are changed, fired for all participants
*/
class ParticipantAttributesChanged(
room: Room,
participant: Participant,
/**
* The attributes that have changed and their new associated values.
*/
val changedAttributes: Map<String, String>,
/**
* The old attributes prior to change.
*/
val oldAttributes: Map<String, String>,
) : RoomEvent(room)
class ParticipantNameChanged(
room: Room,
val participant: Participant,
... ...
... ... @@ -576,6 +576,17 @@ constructor(
)
}
is ParticipantEvent.AttributesChanged -> {
emitWhenConnected(
RoomEvent.ParticipantAttributesChanged(
this@Room,
it.participant,
it.changedAttributes,
it.oldAttributes,
),
)
}
is ParticipantEvent.NameChanged -> {
emitWhenConnected(
RoomEvent.ParticipantNameChanged(
... ... @@ -684,6 +695,17 @@ constructor(
)
}
is ParticipantEvent.AttributesChanged -> {
emitWhenConnected(
RoomEvent.ParticipantAttributesChanged(
this@Room,
it.participant,
it.changedAttributes,
it.oldAttributes,
),
)
}
is ParticipantEvent.NameChanged -> {
emitWhenConnected(
RoomEvent.ParticipantNameChanged(
... ...
/*
* Copyright 2023-2024 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.livekit.android.room
import io.livekit.android.events.RoomEvent
import io.livekit.android.test.MockE2ETest
import io.livekit.android.test.assert.assertIsClass
import io.livekit.android.test.events.EventCollector
import io.livekit.android.test.mock.TestData
import kotlinx.coroutines.ExperimentalCoroutinesApi
import livekit.LivekitRtc.ParticipantUpdate
import livekit.LivekitRtc.SignalResponse
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@ExperimentalCoroutinesApi
@RunWith(RobolectricTestRunner::class)
class RoomParticipantEventMockE2ETest : MockE2ETest() {
@Test
fun localParticipantAttributesChangedEvent() = runTest {
connect()
wsFactory.ws.clearRequests()
wsFactory.registerSignalRequestHandler { request ->
if (request.hasUpdateMetadata()) {
val newInfo = with(TestData.LOCAL_PARTICIPANT.toBuilder()) {
putAllAttributes(request.updateMetadata.attributesMap)
build()
}
val response = with(SignalResponse.newBuilder()) {
update = with(ParticipantUpdate.newBuilder()) {
addParticipants(newInfo)
build()
}
build()
}
wsFactory.receiveMessage(response)
return@registerSignalRequestHandler true
}
return@registerSignalRequestHandler false
}
val newAttributes = mapOf("attribute" to "changedValue")
val collector = EventCollector(room.events, coroutineRule.scope)
room.localParticipant.updateAttributes(newAttributes)
val events = collector.stopCollecting()
assertEquals(1, events.size)
assertIsClass(RoomEvent.ParticipantAttributesChanged::class.java, events.first())
}
}
... ...
... ... @@ -245,6 +245,7 @@ class LocalParticipantMockE2ETest : MockE2ETest() {
listOf(
RoomEvent.ParticipantMetadataChanged::class.java,
RoomEvent.ParticipantNameChanged::class.java,
RoomEvent.ParticipantAttributesChanged::class.java,
),
roomEvents,
)
... ...
... ... @@ -397,6 +397,10 @@ class CallViewModel(
room.sendSimulateScenario(Room.SimulateScenario.SERVER_LEAVE_FULL_RECONNECT)
}
fun updateAttribute(key: String, value: String) {
room.localParticipant.updateAttributes(mapOf(key to value))
}
fun reconnect() {
Timber.e { "Reconnecting." }
mutablePrimarySpeaker.value = null
... ...
... ... @@ -26,11 +26,38 @@ import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ExtendedFloatingActionButton
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
... ... @@ -111,6 +138,7 @@ class CallActivity : AppCompatActivity() {
onSimulateNodeFailure = { viewModel.simulateNodeFailure() },
onSimulateLeaveFullReconnect = { viewModel.simulateServerLeaveFullReconnect() },
fullReconnect = { viewModel.reconnect() },
onUpdateAttribute = { k, v -> viewModel.updateAttribute(k, v) },
)
}
}
... ... @@ -165,7 +193,8 @@ class CallActivity : AppCompatActivity() {
onSimulateMigration: () -> Unit = {},
onSimulateNodeFailure: () -> Unit = {},
fullReconnect: () -> Unit = {},
onSimulateLeaveFullReconnect: () -> Unit,
onSimulateLeaveFullReconnect: () -> Unit = {},
onUpdateAttribute: (key: String, value: String) -> Unit = { _, _ -> },
) {
AppTheme(darkTheme = true) {
ConstraintLayout(
... ... @@ -432,10 +461,11 @@ class CallActivity : AppCompatActivity() {
if (showDebugDialog) {
DebugMenuDialog(
onDismissRequest = { showDebugDialog = false },
simulateMigration = { onSimulateMigration() },
simulateNodeFailure = { onSimulateNodeFailure() },
simulateLeaveFullReconnect = { onSimulateLeaveFullReconnect() },
fullReconnect = { fullReconnect() },
simulateMigration = onSimulateMigration,
simulateNodeFailure = onSimulateNodeFailure,
simulateLeaveFullReconnect = onSimulateLeaveFullReconnect,
fullReconnect = fullReconnect,
onUpdateAttribute = onUpdateAttribute,
)
}
}
... ...
... ... @@ -41,6 +41,7 @@ fun DebugMenuDialog(
fullReconnect: () -> Unit = {},
simulateNodeFailure: () -> Unit = {},
simulateLeaveFullReconnect: () -> Unit = {},
onUpdateAttribute: (key: String, value: String) -> Unit = { _, _ -> },
) {
Dialog(onDismissRequest = onDismissRequest) {
Column(
... ... @@ -79,6 +80,19 @@ fun DebugMenuDialog(
}) {
Text("Reconnect to room")
}
Button(
onClick = {
attributeValue++
onUpdateAttribute(attributeKey, attributeValue.toString())
onDismissRequest()
},
) {
Text("Update Attribute")
}
}
}
}
val attributeKey = "key"
var attributeValue = 0
... ...