From 4ced0550354a1ff456e075050bcead6ec7a3b525 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 4 Feb 2026 17:38:01 -0500 Subject: [PATCH] Switches the metadata relay for the relay being used with the most amount of posts to be the relayhint --- .../vitorpamplona/amethyst/model/LocalCache.kt | 2 +- .../privateDM/datasource/FilterNip04DMs.kt | 18 ++++++++++++------ .../amethyst/commons/model/Note.kt | 2 +- .../amethyst/commons/model/User.kt | 15 ++++++++------- .../model/nip01Core/UserMetadataCache.kt | 2 -- .../desktop/cache/DesktopLocalCache.kt | 2 +- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index 06f6e4ab8..f35c140fa 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -506,7 +506,7 @@ object LocalCache : ILocalCache, ICacheProvider { } } - user.updateUserInfo(newUserMetadata, event, fallbackRelay) + user.updateUserInfo(newUserMetadata, event) if (relay != null) { user.addRelayBeingUsed(relay, event.createdAt) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/datasource/FilterNip04DMs.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/datasource/FilterNip04DMs.kt index 9901ff38d..768a32989 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/datasource/FilterNip04DMs.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/chats/privateDM/datasource/FilterNip04DMs.kt @@ -49,19 +49,25 @@ fun filterNip04DMs( val outbox = authorHomeRelayEvent?.writeRelaysNorm() + ?: LocalCache + .getUserIfExists(it) + ?.relaysBeingUsed + ?.keys + ?.ifEmpty { null } ?: LocalCache.relayHints.hintsForKey(it).ifEmpty { null } - ?: listOfNotNull( - LocalCache.getUserIfExists(it)?.metadataOrNull()?.relay, - ) + ?: emptyList() groupOutboxRelays.addAll(outbox) val inbox = authorHomeRelayEvent?.readRelaysNorm()?.ifEmpty { null } + ?: LocalCache + .getUserIfExists(it) + ?.relaysBeingUsed + ?.keys + ?.ifEmpty { null } ?: LocalCache.relayHints.hintsForKey(it).ifEmpty { null } - ?: listOfNotNull( - LocalCache.getUserIfExists(it)?.metadataOrNull()?.relay, - ) + ?: emptyList() groupInboxRelays.addAll(inbox) } diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/Note.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/Note.kt index d32d945c0..26fc11101 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/Note.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/Note.kt @@ -230,7 +230,7 @@ open class Note( return relays.firstOrNull() } else { - currentOutbox?.firstOrNull() ?: author?.metadataOrNull()?.relay + currentOutbox?.firstOrNull() ?: author?.mostUsedRelay() } } diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/User.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/User.kt index 395719ce2..82392c199 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/User.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/User.kt @@ -82,13 +82,19 @@ class User( fun outboxRelays() = authorRelayList()?.writeRelaysNorm() - fun relayHints() = authorRelayList()?.writeRelaysNorm()?.take(3) ?: listOfNotNull(metadataOrNull()?.relay) + fun relayHints() = + authorRelayList()?.writeRelaysNorm()?.take(3) ?: relaysBeingUsed.entries + .sortedByDescending { it.value.counter } + .take(3) + .map { it.key } fun inboxRelays() = authorRelayList()?.readRelaysNorm() fun dmInboxRelays() = dmInboxRelayList()?.relays()?.ifEmpty { null } ?: inboxRelays() - fun bestRelayHint() = authorRelayList()?.writeRelaysNorm()?.firstOrNull() ?: metadataOrNull()?.relay + fun bestRelayHint() = authorRelayList()?.writeRelaysNorm()?.firstOrNull() ?: mostUsedRelay() + + fun mostUsedRelay() = relaysBeingUsed.maxByOrNull { it.value.counter }?.key fun toPTag() = PTag(pubkeyHex, bestRelayHint()) @@ -188,7 +194,6 @@ class User( fun updateUserInfo( newUserInfo: UserMetadata, metaEvent: MetadataEvent, - relay: NormalizedRelayUrl?, ) { newUserInfo.cleanBlankNames() @@ -205,10 +210,6 @@ class User( metadata.newMetadata(newUserInfo, metaEvent) - if (relay != null) { - metadata.relay = relay - } - // doesn't create Nip05 unless needed. nip05StateOrNull()?.newMetadata(newUserInfo.nip05, metaEvent.pubKey) } diff --git a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip01Core/UserMetadataCache.kt b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip01Core/UserMetadataCache.kt index 7415cb940..00e6f3a16 100644 --- a/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip01Core/UserMetadataCache.kt +++ b/commons/src/commonMain/kotlin/com/vitorpamplona/amethyst/commons/model/nip01Core/UserMetadataCache.kt @@ -25,7 +25,6 @@ import com.vitorpamplona.amethyst.commons.model.ImmutableListOfLists import com.vitorpamplona.amethyst.commons.model.toImmutableListOfLists import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent import com.vitorpamplona.quartz.nip01Core.metadata.UserMetadata -import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip39ExtIdentities.IdentityClaimTag import com.vitorpamplona.quartz.nip39ExtIdentities.identityClaims import kotlinx.coroutines.flow.MutableStateFlow @@ -42,7 +41,6 @@ class UserInfo( @Stable class UserMetadataCache { val flow: MutableStateFlow = MutableStateFlow(null) - var relay: NormalizedRelayUrl? = null fun newMetadata( userInfo: UserMetadata, diff --git a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/cache/DesktopLocalCache.kt b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/cache/DesktopLocalCache.kt index ee005ca78..f2e46f82e 100644 --- a/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/cache/DesktopLocalCache.kt +++ b/desktopApp/src/jvmMain/kotlin/com/vitorpamplona/amethyst/desktop/cache/DesktopLocalCache.kt @@ -110,7 +110,7 @@ class DesktopLocalCache : ICacheProvider { if (user.metadata().shouldUpdateWith(event)) { val newUserMetadata = event.contactMetaData() if (newUserMetadata != null) { - user.updateUserInfo(newUserMetadata, event, null) + user.updateUserInfo(newUserMetadata, event) } } }