Fix roleOf extension. Creator are Owner Role only if privilegedCreatorRole is true.

This commit is contained in:
Benoit Marty
2025-11-20 17:29:01 +01:00
parent 423154e153
commit 966eab8796
3 changed files with 82 additions and 3 deletions

View File

@@ -53,7 +53,7 @@ fun aRoomInfo(
notificationCount: Long = 0,
userDefinedNotificationMode: RoomNotificationMode? = null,
hasRoomCall: Boolean = false,
roomPowerLevels: RoomPowerLevels = RoomPowerLevels(
roomPowerLevels: RoomPowerLevels? = RoomPowerLevels(
values = defaultRoomPowerLevelValues(),
users = persistentMapOf(),
),

View File

@@ -23,12 +23,12 @@ fun RoomInfo.getAvatarData(size: AvatarSize) = AvatarData(
/**
* Returns the role of the user in the room.
* If the user is a creator, returns [RoomMember.Role.Owner].
* If the user is a creator and [RoomInfo.privilegedCreatorRole] is true, returns [RoomMember.Role.Owner].
* Otherwise, checks the power levels and returns the corresponding role.
* If no specific power level is set for the user, defaults to [RoomMember.Role.User].
*/
fun RoomInfo.roleOf(userId: UserId): RoomMember.Role {
return if (creators.contains(userId)) {
return if (privilegedCreatorRole && creators.contains(userId)) {
RoomMember.Role.Owner(isCreator = true)
} else {
roomPowerLevels?.roleOf(userId) ?: RoomMember.Role.User

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2025 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.ui.model
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.api.room.RoomMember
import io.element.android.libraries.matrix.api.room.powerlevels.RoomPowerLevels
import io.element.android.libraries.matrix.test.A_USER_ID
import io.element.android.libraries.matrix.test.A_USER_ID_2
import io.element.android.libraries.matrix.test.A_USER_ID_3
import io.element.android.libraries.matrix.test.room.aRoomInfo
import io.element.android.libraries.matrix.test.room.defaultRoomPowerLevelValues
import kotlinx.collections.immutable.toImmutableMap
import org.junit.Test
class RoomInfoExtensionTest {
@Test
fun `roleOf returns Owner for creator with privilegedCreatorRole true`() {
val roomInfo = aRoomInfo(
privilegedCreatorRole = true,
roomCreators = listOf(A_USER_ID),
)
assertThat(roomInfo.roleOf(A_USER_ID)).isEqualTo(RoomMember.Role.Owner(isCreator = true))
}
@Test
fun `roleOf returns User for not creator with privilegedCreatorRole true`() {
val roomInfo = aRoomInfo(
privilegedCreatorRole = true,
roomCreators = listOf(A_USER_ID),
)
assertThat(roomInfo.roleOf(A_USER_ID_2)).isEqualTo(RoomMember.Role.User)
}
@Test
fun `roleOf returns User for creator with privilegedCreatorRole false`() {
val roomInfo = aRoomInfo(
privilegedCreatorRole = false,
roomCreators = listOf(A_USER_ID),
)
assertThat(roomInfo.roleOf(A_USER_ID)).isEqualTo(RoomMember.Role.User)
}
@Test
fun `roleOf returns role from the power level`() {
val roomInfo = aRoomInfo(
privilegedCreatorRole = false,
roomPowerLevels = RoomPowerLevels(
values = defaultRoomPowerLevelValues(),
users = mapOf(
A_USER_ID to 100L, // Admin
A_USER_ID_2 to 50L, // Moderator
A_USER_ID_3 to 0L, // User
).toImmutableMap(),
),
roomCreators = listOf(A_USER_ID),
)
assertThat(roomInfo.roleOf(A_USER_ID)).isEqualTo(RoomMember.Role.Admin)
assertThat(roomInfo.roleOf(A_USER_ID_2)).isEqualTo(RoomMember.Role.Moderator)
assertThat(roomInfo.roleOf(A_USER_ID_3)).isEqualTo(RoomMember.Role.User)
}
@Test
fun `roleOf returns User when the power level is null`() {
val roomInfo = aRoomInfo(
privilegedCreatorRole = false,
roomPowerLevels = null,
roomCreators = listOf(A_USER_ID),
)
assertThat(roomInfo.roleOf(A_USER_ID)).isEqualTo(RoomMember.Role.User)
assertThat(roomInfo.roleOf(A_USER_ID_2)).isEqualTo(RoomMember.Role.User)
assertThat(roomInfo.roleOf(A_USER_ID_3)).isEqualTo(RoomMember.Role.User)
}
}