diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt index a61a1b0203..e4533b8f0b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListState.kt @@ -89,8 +89,8 @@ class BlossomServerListState( }.onStart { emit(mergeServerList(flow.value)) }.onEach { servers -> - if (servers.none { it == settings.defaultFileServer }) { - settings.changeDefaultFileServer(servers.firstOrNull() ?: DEFAULT_MEDIA_SERVERS[0]) + resetTargetOrNull(flow.value, servers, settings.defaultFileServer)?.let { + settings.changeDefaultFileServer(it) } }.flowOn(Dispatchers.IO) .stateIn( @@ -127,3 +127,25 @@ class BlossomServerListState( alt: String, ): BlossomAuthorizationEvent = BlossomAuthorizationEvent.createDeleteAuth(hash, alt, signer) } + +/** + * Decides whether the persisted default file server must be reset, and to what. + * + * Returns the new default server, or `null` when no change should happen. + * + * The guard on [rawList] being non-empty is what prevents the startup race: before the user's + * [BlossomServersEvent] (kind 10063) loads from cache/relay, [rawList] is empty and [merged] is the + * transient [DEFAULT_MEDIA_SERVERS] fallback. Resetting against that fallback would clobber the + * locally-saved pick on every launch. Only reset once a real, loaded list is in hand and it no + * longer contains the current pick (e.g. the user removed it from their list). + */ +fun resetTargetOrNull( + rawList: List, + merged: List, + current: ServerName, +): ServerName? = + if (rawList.isNotEmpty() && merged.none { it == current }) { + merged.firstOrNull() ?: DEFAULT_MEDIA_SERVERS[0] + } else { + null + } diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListResetTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListResetTest.kt new file mode 100644 index 0000000000..a70d509f9b --- /dev/null +++ b/amethyst/src/test/java/com/vitorpamplona/amethyst/model/nipB7Blossom/BlossomServerListResetTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.model.nipB7Blossom + +import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerName +import com.vitorpamplona.amethyst.ui.actions.mediaServers.ServerType +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class BlossomServerListResetTest { + private fun server(host: String) = ServerName(host, "https://$host/", ServerType.Blossom) + + private val a = server("a.example") + private val b = server("b.example") + private val c = server("c.example") + private val custom = server("my-custom.example") + + @Test + fun transientDefaultEmissionDoesNotClobberSavedPick() { + // Regression guard: before the user's BlossomServersEvent loads, the raw + // published list is empty and `merged` is the DEFAULT_MEDIA_SERVERS fallback. + // The saved custom pick must NOT be reset. + val result = + resetTargetOrNull( + rawList = emptyList(), + merged = listOf(a, b, c), + current = custom, + ) + + assertNull(result) + } + + @Test + fun loadedListWithoutCurrentResetsToFirst() { + // The user removed their current default from the published list -> reset. + val result = + resetTargetOrNull( + rawList = listOf("a.example", "b.example", "c.example"), + merged = listOf(a, b, c), + current = custom, + ) + + assertEquals(a, result) + } + + @Test + fun loadedListContainingCurrentDoesNotReset() { + val result = + resetTargetOrNull( + rawList = listOf("a.example", "b.example", "c.example"), + merged = listOf(a, b, c), + current = b, + ) + + assertNull(result) + } +}