fix(wallet): use full URL for account data check

- Get server name from userIdServerName()
- Construct full URL to Matrix account data endpoint
- Handle 404 response to detect missing backup
This commit is contained in:
Kayos 2026-03-29 05:23:18 -07:00
parent da589ae78f
commit ee439cb5a3

View file

@ -11,6 +11,7 @@ import dev.zacsweers.metro.ContributesBinding
import dev.zacsweers.metro.Inject
import io.element.android.features.wallet.api.backup.WalletBackupService
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.exception.ClientException
import timber.log.Timber
/**
@ -52,22 +53,38 @@ class WalletBackupServiceImpl @Inject constructor(
override suspend fun hasBackupWithoutKey(): Result<Boolean> {
return runCatching {
// Build the account data URL for the wallet secret
// Get server name from user ID (e.g., "sulkta.com" from "@user:sulkta.com")
val serverName = matrixClient.userIdServerName()
val userId = matrixClient.sessionId.value
val url = "/_matrix/client/v3/user/$userId/account_data/${WalletBackupService.SECRET_NAME}"
val secretName = WalletBackupService.SECRET_NAME
// Construct full URL to check account data
val url = "https://$serverName/_matrix/client/v3/user/$userId/account_data/$secretName"
Timber.d("Checking for wallet backup at: $url")
try {
// Try to fetch the account data - if it exists, we get content back
// Try to fetch the account data
val response = matrixClient.getUrl(url).getOrThrow()
// If we got a non-empty response, the backup exists
// Even if encrypted, the account data key existing means a backup was made
val content = response.decodeToString()
Timber.d("Account data check response: ${content.take(100)}")
// Check if it's a valid JSON object with content (not empty {} or error)
// If we got a response with content (not empty or error), backup exists
// The content will be encrypted - we just need to know it exists
content.isNotEmpty() && content != "{}" && !content.contains("\"errcode\"")
} catch (e: ClientException.Generic) {
// Check if it's a 404 (not found)
if (e.message?.contains("404") == true) {
Timber.d("No wallet backup found (404)")
false
} else {
Timber.w(e, "Error checking for wallet backup")
// On error, assume no backup to avoid blocking setup
false
}
} catch (e: Exception) {
Timber.d(e, "Account data not found or error checking")
// 404 or other error means no backup exists
Timber.w(e, "Error checking for wallet backup")
// On error, assume no backup to avoid blocking setup
false
}
}