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
This commit is contained in:
Adam Brown 2022-10-01 10:14:06 +01:00 committed by Adam Brown
parent 9d4f0f4197
commit 1d1aff0ca9
1 changed files with 25 additions and 6 deletions

View File

@ -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
}
}
)
}
}