Skip to content

Commit 529296a

Browse files
committed
fix: prefer bitkit contact profiles
1 parent b24c552 commit 529296a

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

app/src/main/java/to/bitkit/repositories/PubkyRepo.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,14 @@ class PubkyRepo @Inject constructor(
476476
}
477477
}
478478

479-
suspend fun fetchContactProfile(publicKey: String): Result<PubkyProfile> = runCatching {
480-
withContext(ioDispatcher) {
481-
val prefixedKey = publicKey.ensurePubkyPrefix()
482-
val ffiProfile = pubkyService.getProfile(prefixedKey)
483-
PubkyProfile.fromFfi(prefixedKey, ffiProfile)
484-
}
485-
}.onFailure {
486-
Logger.error("Failed to load contact profile '$publicKey'", it, context = TAG)
479+
suspend fun fetchContactProfile(publicKey: String): Result<PubkyProfile> {
480+
val prefixedKey = publicKey.ensurePubkyPrefix()
481+
return fetchRemoteProfile(prefixedKey)
482+
.map { it ?: PubkyProfile.placeholder(prefixedKey) }
483+
.recover {
484+
Logger.warn("Falling back to placeholder contact '$prefixedKey'", it, context = TAG)
485+
PubkyProfile.placeholder(prefixedKey)
486+
}
487487
}
488488

489489
suspend fun addContact(publicKey: String, existingProfile: PubkyProfile? = null): Result<Unit> = runCatching {

app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,27 @@ class PubkyRepoTest : BaseUnitTest() {
466466
}
467467

468468
@Test
469-
fun `fetchContactProfile should return profile on success`() = test {
470-
val contactKey = "pubky://contact3"
469+
fun `fetchContactProfile should return bitkit profile when available`() = test {
470+
val contactKey = "pubkycontact3"
471+
val strippedKey = contactKey.removePrefix("pubky")
472+
val json = """{"name":"Bob","bio":"Bio"}"""
473+
whenever(pubkyService.fetchFileString("pubky://$strippedKey${Env.profilePath}"))
474+
.thenReturn(json)
475+
476+
val result = sut.fetchContactProfile(contactKey)
477+
478+
assertTrue(result.isSuccess)
479+
assertEquals("Bob", result.getOrNull()?.name)
480+
verify(pubkyService, never()).getProfile(contactKey)
481+
}
482+
483+
@Test
484+
fun `fetchContactProfile should fall back to pubky profile when bitkit profile is missing`() = test {
485+
val contactKey = "pubkycontact3"
486+
val strippedKey = contactKey.removePrefix("pubky")
471487
val contactProfile = mock<CorePubkyProfile>()
488+
whenever(pubkyService.fetchFileString("pubky://$strippedKey${Env.profilePath}"))
489+
.thenThrow(RuntimeException("Missing bitkit profile"))
472490
whenever(contactProfile.name).thenReturn("Bob")
473491
whenever(contactProfile.bio).thenReturn("Bio")
474492
whenever(pubkyService.getProfile(contactKey)).thenReturn(contactProfile)
@@ -480,13 +498,17 @@ class PubkyRepoTest : BaseUnitTest() {
480498
}
481499

482500
@Test
483-
fun `fetchContactProfile should return failure on error`() = test {
484-
val contactKey = "pubky://failing"
485-
whenever(pubkyService.getProfile(contactKey)).thenThrow(RuntimeException("Failed"))
501+
fun `fetchContactProfile should fall back to placeholder when remote lookup fails`() = test {
502+
val contactKey = "pubkycontact3"
503+
val strippedKey = contactKey.removePrefix("pubky")
504+
whenever(pubkyService.fetchFileString("pubky://$strippedKey${Env.profilePath}"))
505+
.thenThrow(RuntimeException("Missing bitkit profile"))
506+
whenever(pubkyService.getProfile(contactKey)).thenThrow(RuntimeException("Missing pubky profile"))
486507

487508
val result = sut.fetchContactProfile(contactKey)
488509

489-
assertTrue(result.isFailure)
510+
assertTrue(result.isSuccess)
511+
assertEquals(PubkyProfile.placeholder(contactKey), result.getOrNull())
490512
}
491513

492514
@Test

0 commit comments

Comments
 (0)