Skip to content

fix: Make Client.findDM return a Result #4816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DefaultStartDMActionTest {
@Test
fun `when dm is found, assert state is updated with given room id`() = runTest {
val matrixClient = FakeMatrixClient().apply {
givenFindDmResult(A_ROOM_ID)
givenFindDmResult(Result.success(A_ROOM_ID))
}
val analyticsService = FakeAnalyticsService()
val action = createStartDMAction(matrixClient, analyticsService)
Expand All @@ -37,10 +37,23 @@ class DefaultStartDMActionTest {
assertThat(analyticsService.capturedEvents).isEmpty()
}

@Test
fun `when finding the dm fails, assert state is updated with given error`() = runTest {
val matrixClient = FakeMatrixClient().apply {
givenFindDmResult(Result.failure(AN_EXCEPTION))
}
val analyticsService = FakeAnalyticsService()
val action = createStartDMAction(matrixClient, analyticsService)
val state = mutableStateOf<AsyncAction<RoomId>>(AsyncAction.Uninitialized)
action.execute(aMatrixUser(), true, state)
assertThat(state.value).isEqualTo(AsyncAction.Failure(AN_EXCEPTION))
assertThat(analyticsService.capturedEvents).isEmpty()
}

@Test
fun `when dm is not found, assert dm is created, state is updated with given room id and analytics get called`() = runTest {
val matrixClient = FakeMatrixClient().apply {
givenFindDmResult(null)
givenFindDmResult(Result.success(null))
givenCreateDmResult(Result.success(A_ROOM_ID))
}
val analyticsService = FakeAnalyticsService()
Expand All @@ -54,7 +67,7 @@ class DefaultStartDMActionTest {
@Test
fun `when dm is not found, and createIfDmDoesNotExist is false, assert dm is not created and state is updated to confirmation state`() = runTest {
val matrixClient = FakeMatrixClient().apply {
givenFindDmResult(null)
givenFindDmResult(Result.success(null))
givenCreateDmResult(Result.success(A_ROOM_ID))
}
val analyticsService = FakeAnalyticsService()
Expand All @@ -69,7 +82,7 @@ class DefaultStartDMActionTest {
@Test
fun `when dm creation fails, assert state is updated with given error`() = runTest {
val matrixClient = FakeMatrixClient().apply {
givenFindDmResult(null)
givenFindDmResult(Result.success(null))
givenCreateDmResult(Result.failure(AN_EXCEPTION))
}
val analyticsService = FakeAnalyticsService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class UserProfilePresenter @AssistedInject constructor(
@Composable
private fun getDmRoomId(): State<RoomId?> {
return produceState<RoomId?>(initialValue = null) {
value = client.findDM(userId)
value = client.findDM(userId).getOrNull()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class UserProfilePresenterTest {
if (canFindRoom) {
givenGetRoomResult(A_ROOM_ID, room)
}
givenFindDmResult(dmRoom)
givenFindDmResult(Result.success(dmRoom))
}
val presenter = createUserProfilePresenter(
userId = A_USER_ID_2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ interface MatrixClient {
val ignoredUsersFlow: StateFlow<ImmutableList<UserId>>
suspend fun getJoinedRoom(roomId: RoomId): JoinedRoom?
suspend fun getRoom(roomId: RoomId): BaseRoom?
suspend fun findDM(userId: UserId): RoomId?
suspend fun findDM(userId: UserId): Result<RoomId?>
suspend fun ignoreUser(userId: UserId): Result<Unit>
suspend fun unignoreUser(userId: UserId): Result<Unit>
suspend fun createRoom(createRoomParams: CreateRoomParameters): Result<RoomId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ suspend fun MatrixClient.startDM(
userId: UserId,
createIfDmDoesNotExist: Boolean,
): StartDMResult {
val existingDM = findDM(userId)
return if (existingDM != null) {
StartDMResult.Success(existingDM, isNew = false)
} else if (createIfDmDoesNotExist) {
createDM(userId).fold(
{ StartDMResult.Success(it, isNew = true) },
{ StartDMResult.Failure(it) }
return findDM(userId)
.fold(
onSuccess = { existingDM ->
if (existingDM != null) {
StartDMResult.Success(existingDM, isNew = false)
} else if (createIfDmDoesNotExist) {
createDM(userId).fold(
{ StartDMResult.Success(it, isNew = true) },
{ StartDMResult.Failure(it) }
)
} else {
StartDMResult.DmDoesNotExist
}
},
onFailure = { error ->
StartDMResult.Failure(error)
}
)
} else {
StartDMResult.DmDoesNotExist
}
}

sealed interface StartDMResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ class RustMatrixClient(
}
}

override suspend fun findDM(userId: UserId): RoomId? = withContext(sessionDispatcher) {
innerClient.getDmRoom(userId.value)?.use { RoomId(it.id()) }
override suspend fun findDM(userId: UserId): Result<RoomId?> = withContext(sessionDispatcher) {
runCatchingExceptions {
innerClient.getDmRoom(userId.value)?.use { RoomId(it.id()) }
}
}

override suspend fun ignoreUser(userId: UserId): Result<Unit> = withContext(sessionDispatcher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FakeMatrixClient(

private var createRoomResult: Result<RoomId> = Result.success(A_ROOM_ID)
private var createDmResult: Result<RoomId> = Result.success(A_ROOM_ID)
private var findDmResult: RoomId? = A_ROOM_ID
private var findDmResult: Result<RoomId?> = Result.success(A_ROOM_ID)
private val getRoomResults = mutableMapOf<RoomId, BaseRoom>()
private val searchUserResults = mutableMapOf<String, Result<MatrixSearchUserResults>>()
private val getProfileResults = mutableMapOf<UserId, Result<MatrixUser>>()
Expand Down Expand Up @@ -133,7 +133,7 @@ class FakeMatrixClient(
return getRoomResults[roomId] as? JoinedRoom
}

override suspend fun findDM(userId: UserId): RoomId? {
override suspend fun findDM(userId: UserId): Result<RoomId?> {
return findDmResult
}

Expand Down Expand Up @@ -248,7 +248,7 @@ class FakeMatrixClient(
createDmResult = result
}

fun givenFindDmResult(result: RoomId?) {
fun givenFindDmResult(result: Result<RoomId?>) {
findDmResult = result
}

Expand Down
Loading