Skip to content

Commit 07af7cc

Browse files
authored
fix: Make Client.findDM return a Result (#4816)
1 parent fa2ac28 commit 07af7cc

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

features/createroom/impl/src/test/kotlin/io/element/android/features/createroom/impl/DefaultStartDMActionTest.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DefaultStartDMActionTest {
2727
@Test
2828
fun `when dm is found, assert state is updated with given room id`() = runTest {
2929
val matrixClient = FakeMatrixClient().apply {
30-
givenFindDmResult(A_ROOM_ID)
30+
givenFindDmResult(Result.success(A_ROOM_ID))
3131
}
3232
val analyticsService = FakeAnalyticsService()
3333
val action = createStartDMAction(matrixClient, analyticsService)
@@ -37,10 +37,23 @@ class DefaultStartDMActionTest {
3737
assertThat(analyticsService.capturedEvents).isEmpty()
3838
}
3939

40+
@Test
41+
fun `when finding the dm fails, assert state is updated with given error`() = runTest {
42+
val matrixClient = FakeMatrixClient().apply {
43+
givenFindDmResult(Result.failure(AN_EXCEPTION))
44+
}
45+
val analyticsService = FakeAnalyticsService()
46+
val action = createStartDMAction(matrixClient, analyticsService)
47+
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
48+
action.execute(aMatrixUser(), true, state)
49+
assertThat(state.value).isEqualTo(AsyncAction.Failure(AN_EXCEPTION))
50+
assertThat(analyticsService.capturedEvents).isEmpty()
51+
}
52+
4053
@Test
4154
fun `when dm is not found, assert dm is created, state is updated with given room id and analytics get called`() = runTest {
4255
val matrixClient = FakeMatrixClient().apply {
43-
givenFindDmResult(null)
56+
givenFindDmResult(Result.success(null))
4457
givenCreateDmResult(Result.success(A_ROOM_ID))
4558
}
4659
val analyticsService = FakeAnalyticsService()
@@ -54,7 +67,7 @@ class DefaultStartDMActionTest {
5467
@Test
5568
fun `when dm is not found, and createIfDmDoesNotExist is false, assert dm is not created and state is updated to confirmation state`() = runTest {
5669
val matrixClient = FakeMatrixClient().apply {
57-
givenFindDmResult(null)
70+
givenFindDmResult(Result.success(null))
5871
givenCreateDmResult(Result.success(A_ROOM_ID))
5972
}
6073
val analyticsService = FakeAnalyticsService()
@@ -69,7 +82,7 @@ class DefaultStartDMActionTest {
6982
@Test
7083
fun `when dm creation fails, assert state is updated with given error`() = runTest {
7184
val matrixClient = FakeMatrixClient().apply {
72-
givenFindDmResult(null)
85+
givenFindDmResult(Result.success(null))
7386
givenCreateDmResult(Result.failure(AN_EXCEPTION))
7487
}
7588
val analyticsService = FakeAnalyticsService()

features/userprofile/impl/src/main/kotlin/io/element/android/features/userprofile/impl/root/UserProfilePresenter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class UserProfilePresenter @AssistedInject constructor(
5555
@Composable
5656
private fun getDmRoomId(): State<RoomId?> {
5757
return produceState<RoomId?>(initialValue = null) {
58-
value = client.findDM(userId)
58+
value = client.findDM(userId).getOrNull()
5959
}
6060
}
6161

features/userprofile/impl/src/test/kotlin/io/element/android/features/userprofile/impl/UserProfilePresenterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class UserProfilePresenterTest {
141141
if (canFindRoom) {
142142
givenGetRoomResult(A_ROOM_ID, room)
143143
}
144-
givenFindDmResult(dmRoom)
144+
givenFindDmResult(Result.success(dmRoom))
145145
}
146146
val presenter = createUserProfilePresenter(
147147
userId = A_USER_ID_2,

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/MatrixClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface MatrixClient {
5555
val ignoredUsersFlow: StateFlow<ImmutableList<UserId>>
5656
suspend fun getJoinedRoom(roomId: RoomId): JoinedRoom?
5757
suspend fun getRoom(roomId: RoomId): BaseRoom?
58-
suspend fun findDM(userId: UserId): RoomId?
58+
suspend fun findDM(userId: UserId): Result<RoomId?>
5959
suspend fun ignoreUser(userId: UserId): Result<Unit>
6060
suspend fun unignoreUser(userId: UserId): Result<Unit>
6161
suspend fun createRoom(createRoomParams: CreateRoomParameters): Result<RoomId>

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ suspend fun MatrixClient.startDM(
1818
userId: UserId,
1919
createIfDmDoesNotExist: Boolean,
2020
): StartDMResult {
21-
val existingDM = findDM(userId)
22-
return if (existingDM != null) {
23-
StartDMResult.Success(existingDM, isNew = false)
24-
} else if (createIfDmDoesNotExist) {
25-
createDM(userId).fold(
26-
{ StartDMResult.Success(it, isNew = true) },
27-
{ StartDMResult.Failure(it) }
21+
return findDM(userId)
22+
.fold(
23+
onSuccess = { existingDM ->
24+
if (existingDM != null) {
25+
StartDMResult.Success(existingDM, isNew = false)
26+
} else if (createIfDmDoesNotExist) {
27+
createDM(userId).fold(
28+
{ StartDMResult.Success(it, isNew = true) },
29+
{ StartDMResult.Failure(it) }
30+
)
31+
} else {
32+
StartDMResult.DmDoesNotExist
33+
}
34+
},
35+
onFailure = { error ->
36+
StartDMResult.Failure(error)
37+
}
2838
)
29-
} else {
30-
StartDMResult.DmDoesNotExist
31-
}
3239
}
3340

3441
sealed interface StartDMResult {

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,10 @@ class RustMatrixClient(
297297
}
298298
}
299299

300-
override suspend fun findDM(userId: UserId): RoomId? = withContext(sessionDispatcher) {
301-
innerClient.getDmRoom(userId.value)?.use { RoomId(it.id()) }
300+
override suspend fun findDM(userId: UserId): Result<RoomId?> = withContext(sessionDispatcher) {
301+
runCatchingExceptions {
302+
innerClient.getDmRoom(userId.value)?.use { RoomId(it.id()) }
303+
}
302304
}
303305

304306
override suspend fun ignoreUser(userId: UserId): Result<Unit> = withContext(sessionDispatcher) {

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class FakeMatrixClient(
103103

104104
private var createRoomResult: Result<RoomId> = Result.success(A_ROOM_ID)
105105
private var createDmResult: Result<RoomId> = Result.success(A_ROOM_ID)
106-
private var findDmResult: RoomId? = A_ROOM_ID
106+
private var findDmResult: Result<RoomId?> = Result.success(A_ROOM_ID)
107107
private val getRoomResults = mutableMapOf<RoomId, BaseRoom>()
108108
private val searchUserResults = mutableMapOf<String, Result<MatrixSearchUserResults>>()
109109
private val getProfileResults = mutableMapOf<UserId, Result<MatrixUser>>()
@@ -133,7 +133,7 @@ class FakeMatrixClient(
133133
return getRoomResults[roomId] as? JoinedRoom
134134
}
135135

136-
override suspend fun findDM(userId: UserId): RoomId? {
136+
override suspend fun findDM(userId: UserId): Result<RoomId?> {
137137
return findDmResult
138138
}
139139

@@ -248,7 +248,7 @@ class FakeMatrixClient(
248248
createDmResult = result
249249
}
250250

251-
fun givenFindDmResult(result: RoomId?) {
251+
fun givenFindDmResult(result: Result<RoomId?>) {
252252
findDmResult = result
253253
}
254254

0 commit comments

Comments
 (0)