From 1d1aff0ca9286a91819185e8e1c8ec08f938e738 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Sat, 1 Oct 2022 10:14:06 +0100 Subject: [PATCH] allowing the profile call to continue when receiving a 404 instead of crashing - this case is known by the spec for when a profile doesn't exist yet - 403s still crash as fetching your own profile information should never result in access denied --- .../room/internal/DefaultProfileService.kt | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/matrix/services/profile/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultProfileService.kt b/matrix/services/profile/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultProfileService.kt index 5a39c53..7d4067e 100644 --- a/matrix/services/profile/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultProfileService.kt +++ b/matrix/services/profile/src/main/kotlin/app/dapk/st/matrix/room/internal/DefaultProfileService.kt @@ -5,6 +5,8 @@ import app.dapk.st.matrix.common.* import app.dapk.st.matrix.http.MatrixHttpClient import app.dapk.st.matrix.room.ProfileService import app.dapk.st.matrix.room.ProfileStore +import io.ktor.client.call.* +import io.ktor.client.plugins.* import kotlinx.coroutines.flow.first import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -29,12 +31,29 @@ internal class DefaultProfileService( private suspend fun fetchMe(): ProfileService.Me { val credentials = credentialsStore.credentials()!! val userId = credentials.userId - val result = httpClient.execute(profileRequest(userId)) - return ProfileService.Me( - userId, - result.displayName, - result.avatarUrl?.convertMxUrToUrl(credentials.homeServer)?.let { AvatarUrl(it) }, - homeServerUrl = credentials.homeServer, + return runCatching { httpClient.execute(profileRequest(userId)) }.fold( + onSuccess = { + ProfileService.Me( + userId, + it.displayName, + it.avatarUrl?.convertMxUrToUrl(credentials.homeServer)?.let { AvatarUrl(it) }, + homeServerUrl = credentials.homeServer, + ) + }, + onFailure = { + when { + it is ClientRequestException && it.response.status.value == 404 -> { + ProfileService.Me( + userId, + displayName = null, + avatarUrl = null, + homeServerUrl = credentials.homeServer, + ) + } + + else -> throw it + } + } ) } }