Switches the metadata relay for the relay being used with the most amount of posts to be the relayhint

This commit is contained in:
Vitor Pamplona
2026-02-04 17:38:01 -05:00
parent a67fe859e8
commit 4ced055035
6 changed files with 23 additions and 18 deletions
@@ -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)
}
@@ -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)
}
@@ -230,7 +230,7 @@ open class Note(
return relays.firstOrNull()
} else {
currentOutbox?.firstOrNull() ?: author?.metadataOrNull()?.relay
currentOutbox?.firstOrNull() ?: author?.mostUsedRelay()
}
}
@@ -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)
}
@@ -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<UserInfo?> = MutableStateFlow(null)
var relay: NormalizedRelayUrl? = null
fun newMetadata(
userInfo: UserMetadata,
@@ -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)
}
}
}