From 010a8b406070d471fccf59571bbf56baa98ce2e5 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Thu, 25 Jun 2026 08:12:27 +0000 Subject: [PATCH] feat: wipe database on logout (#25) Reviewed-on: https://git.reya.su/reya/coop-mobile/pulls/25 --- .../kotlin/su/reya/coop/screens/HomeScreen.kt | 2 +- .../commonMain/kotlin/su/reya/coop/Nostr.kt | 10 ++++++- .../kotlin/su/reya/coop/NostrViewModel.kt | 30 ++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/HomeScreen.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/HomeScreen.kt index f1e75b6..0eeb362 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/HomeScreen.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/HomeScreen.kt @@ -781,7 +781,7 @@ fun BottomMenuList( } Spacer(modifier = Modifier.size(16.dp)) FilledTonalButton( - onClick = { viewModel.logout() }, + onClick = { onDismiss { viewModel.logout() } }, colors = ButtonDefaults.filledTonalButtonColors( containerColor = MaterialTheme.colorScheme.error, contentColor = MaterialTheme.colorScheme.onError diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index 0ef6116..ca2ce5f 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -188,6 +188,14 @@ class Nostr { } } + suspend fun prune() { + try { + client?.database()?.wipe() + } catch (e: Exception) { + throw IllegalStateException("Failed to prune database: ${e.message}", e) + } + } + suspend fun setSigner(new: AsyncNostrSigner) { try { signer.switch(new) @@ -994,7 +1002,7 @@ class Nostr { try { val currentUser = signer.getPublicKeyAsync() ?: throw IllegalStateException("User not signed in") - + val kind = Kind.fromStd(KindStandard.CONTACT_LIST) val filter = Filter().kind(kind).author(currentUser).limit(1u) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt index 052c5d9..0e7e915 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt @@ -325,12 +325,34 @@ class NostrViewModel( fun logout() { viewModelScope.launch { - secretStore.clear("user_signer") - nostr.signer.switch(Keys.generate()) - _signerRequired.value = true + try { + _isBusy.value = true + // Reset the nostr signer and prune the database + nostr.signer.switch(Keys.generate()) + nostr.prune() + } catch (e: Exception) { + showError("Logout encountered an error: ${e.message}") + } finally { + // Clear credentials from persistent storage + secretStore.clear("user_signer") + + // Reset all UI states + resetInternalState() + + _isBusy.value = false + _signerRequired.value = true + } } } + private fun resetInternalState() { + _chatRooms.value = emptySet() + _contactList.value = emptySet() + _isPartialProcessedGiftWrap.value = false + _isRelayListEmpty.value = false + _isNotificationBannerDismissed.value = false + } + fun dismissNotificationBanner() { viewModelScope.launch { secretStore.set("notification_banner_dismissed", "true") @@ -821,7 +843,7 @@ class NostrViewModel( } return emptyList() } - + suspend fun verifyActivity(pubkey: PublicKey): Timestamp? { return try { nostr.verifyActivity(pubkey)