securemail: inject collaborators into inbound, sender-key and identity paths
- Commit
b99491a4ace6671b54ba54de5353c82174c8e35a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 11:41 UTC (29 days ago)
- Parents
ab80d3e
Follow-up to the MailStore seam: InboundStore.store, SenderKeys.resolve and Identities.ensureMigrated/ forDelivery were hard-wired to MailRepository.get, the Keystore passphrase and the real relay/WKD fetchers, so decrypt-on-arrival, key resolution and identity migration could not run in JVM tests (SQLCipher vault plus real network). Extract internal cores taking a MailStore, the resolved identity/passphrase/settings, and fetcher lambdas; every public entry point wires in exactly the same production collaborators, and MailViewModel now resolves delivery/sender identities through its injected store. No behavior changes. (cherry picked from commit d55b57de31ebae6d78433f1e1ead7053f233ef3b)
Diff
securemail/src/main/java/com/vayunmathur/securemail/data/Identities.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/data/Identities.kt b/securemail/src/main/java/com/vayunmathur/securemail/data/Identities.ktindex f2ad501b92..78f0021d9f 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/data/Identities.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/data/Identities.kt@@ -18,13 +18,20 @@ object Identities { */ suspend fun ensureMigrated(context: Context) { val app = context.applicationContext- val dao = MailRepository.get(app).identityDao()+ ensureMigrated(+ dao = MailRepository.get(app).identityDao(),+ settings = MailConfig(app).settings.first(),+ secretArmor = runCatching { KeyManager(app).getSecretArmor() }.getOrNull(),+ )+ }++ /** Store-injected core of [ensureMigrated] (same idempotent backfill). */+ internal suspend fun ensureMigrated(dao: IdentityDao, settings: MailSettings, secretArmor: String?) { if (dao.count() > 0) return migrateLock.withLock { if (dao.count() > 0) return- val settings = MailConfig(app).settings.first() if (!settings.isConfigured) return- val secret = runCatching { KeyManager(app).getSecretArmor() }.getOrNull() ?: return+ val secret = secretArmor ?: return dao.upsert( MailIdentity( address = settings.address,@@ -46,10 +53,13 @@ object Identities { /** The identity a message was delivered to, falling back to the primary. */ suspend fun forDelivery(context: Context, deliveredTo: String?): MailIdentity? { ensureMigrated(context)- val dao = MailRepository.get(context.applicationContext).identityDao()- return deliveredTo?.let { dao.get(it.lowercase()) } ?: dao.primary()+ return forDelivery(MailRepository.get(context.applicationContext).identityDao(), deliveredTo) } + /** Store-injected [forDelivery] without the migration step (callers that already migrated). */+ internal suspend fun forDelivery(dao: IdentityDao, deliveredTo: String?): MailIdentity? =+ deliveredTo?.let { dao.get(it.lowercase()) } ?: dao.primary()+ /** Signs a relay SUB challenge payload with [address]'s key; null when unavailable. */ suspend fun sign(context: Context, address: String, payload: ByteArray): ByteArray? { val app = context.applicationContext
securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.ktindex 5d2bfc6cca..d8999a5278 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt@@ -223,7 +223,15 @@ class MailViewModel( MailSocket.connectedFlow .onEach { up -> state = state.copy(socketConnected = up) } .launchIn(viewModelScope)- viewModelScope.launch(Dispatchers.IO) { Identities.ensureMigrated(getApplication()) }+ viewModelScope.launch(Dispatchers.IO) {+ // Through the injected store (not the static repository) so a+ // test ViewModel never opens the SQLCipher vault.+ Identities.ensureMigrated(+ dao = repo.identityDao(),+ settings = config.settings.first(),+ secretArmor = runCatching { keyManager.getSecretArmor() }.getOrNull(),+ )+ } } // ------------------------------------------------------------------@@ -392,7 +400,7 @@ class MailViewModel( // S2K passphrase loads transparently from Keystore-wrapped // storage (sealed at claim time) — never asked for again. val passphrase = requirePassphrase()- val ownSecret = Identities.forDelivery(getApplication(), stored.deliveredTo)+ val ownSecret = Identities.forDelivery(repo.identityDao(), stored.deliveredTo) ?.secretArmor ?: return@runCatching val raw = Base64.decode(stored.blobBase64, Base64.NO_WRAP) val pgpBytes = extractPgpPayload(raw) ?: raw@@ -600,7 +608,7 @@ class MailViewModel( /** The own identity to send from ([from] or the primary); rejects disabled aliases. */ private suspend fun senderIdentity(from: String?): MailIdentity {- val identity = Identities.forDelivery(getApplication(), from)+ val identity = Identities.forDelivery(repo.identityDao(), from) ?: error("Set up your address first.") if (identity.disabled) error("${identity.address} is disabled — enable it in Settings to send.") return identity