Unify the way we decide whether a room is a DM or a group room (#3100)

* Add centralised 'room is DM' check

Also add extension functions for `MatrixRoom` and `MatrixRoomInfo`.

* Use the centralised method and extension functions through the app, including:

- Room list.
- Room details screen.
- Invites.
- Notifications.

Replace most `isDirect` usages with `isDm`.

* Update screenshots

---------

Co-authored-by: ElementBot <benoitm+elementbot@element.io>
This commit is contained in:
Jorge Martin Espinosa 2024-07-10 18:28:46 +02:00 committed by GitHub
parent 1a03edbe63
commit 0be7058416
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 195 additions and 73 deletions

View file

@ -33,6 +33,7 @@ data class NotificationData(
val roomAvatarUrl: String?,
val roomDisplayName: String?,
val isDirect: Boolean,
val isDm: Boolean,
val isEncrypted: Boolean,
val isNoisy: Boolean,
val timestamp: Long,

View file

@ -57,9 +57,6 @@ interface MatrixRoom : Closeable {
val activeMemberCount: Long
val joinedMemberCount: Long
/** Whether the room is a direct message. */
val isDm: Boolean get() = isDirect && isOneToOne
val roomInfoFlow: Flow<MatrixRoomInfo>
val roomTypingMembersFlow: Flow<List<UserId>>

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* 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.element.android.libraries.matrix.api.room
/**
* Returns whether the room with the provided info is a DM.
* A DM is a room with at most 2 active members (one of them may have left).
*
* @param isDirect true if the room is direct
* @param activeMembersCount the number of active members in the room (joined or invited)
*/
fun isDm(isDirect: Boolean, activeMembersCount: Int): Boolean {
return isDirect && activeMembersCount <= 2
}
/**
* Returns whether the [MatrixRoom] is a DM.
*/
val MatrixRoom.isDm get() = isDm(isDirect, activeMemberCount.toInt())
/**
* Returns whether the [MatrixRoomInfo] is from a DM.
*/
val MatrixRoomInfo.isDm get() = isDm(isDirect, activeMembersCount.toInt())

View file

@ -21,6 +21,7 @@ import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.room.CurrentUserMembership
import io.element.android.libraries.matrix.api.room.MatrixRoom
import io.element.android.libraries.matrix.api.room.isDm
import io.element.android.libraries.matrix.api.room.toMatrixUser
import io.element.android.libraries.matrix.api.user.MatrixUser
import kotlinx.coroutines.flow.first

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* 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.element.android.libraries.matrix.api.room
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class RoomIsDmCheckTest {
@Test
fun `a room is a DM only if it has at most 2 members and is direct`() {
val isDirect = true
val activeMembersCount = 2
val isDm = isDm(isDirect, activeMembersCount)
assertThat(isDm).isTrue()
}
@Test
fun `a room can be a DM if it has also a single active user`() {
val isDirect = true
val activeMembersCount = 1
val isDm = isDm(isDirect, activeMembersCount)
assertThat(isDm).isTrue()
}
@Test
fun `a room is not a DM if it's not direct`() {
val isDirect = false
val activeMembersCount = 2
val isDm = isDm(isDirect, activeMembersCount)
assertThat(isDm).isFalse()
}
@Test
fun `a room is not a DM if it has more than 2 active users`() {
val isDirect = true
val activeMembersCount = 3
val isDm = isDm(isDirect, activeMembersCount)
assertThat(isDm).isFalse()
}
}