Merge pull request #1853 from vitorpamplona/claude/add-relay-event-counters-c7stK

Add NIP-45 COUNT query support to relay management UI
This commit is contained in:
Vitor Pamplona
2026-03-15 19:58:19 -04:00
committed by GitHub
24 changed files with 593 additions and 11 deletions
@@ -188,6 +188,14 @@ fun MappedAllRelayListView(
val proxyRelays by proxyViewModel.relays.collectAsStateWithLifecycle()
val relayFeedsFeedState by relayFeedsViewModel.relays.collectAsStateWithLifecycle()
val outboxCounts by nip65ViewModel.homeCountResults.collectAsStateWithLifecycle()
val inboxCounts by nip65ViewModel.notifCountResults.collectAsStateWithLifecycle()
val dmCounts by dmViewModel.countResults.collectAsStateWithLifecycle()
val privateHomeCounts by privateOutboxViewModel.countResults.collectAsStateWithLifecycle()
val proxyCounts by proxyViewModel.countResults.collectAsStateWithLifecycle()
val indexerCounts by indexerViewModel.countResults.collectAsStateWithLifecycle()
val searchCounts by searchViewModel.countResults.collectAsStateWithLifecycle()
Scaffold(
topBar = {
SavingTopBar(
@@ -261,7 +269,7 @@ fun MappedAllRelayListView(
SettingsCategoryFirstModifier,
)
}
renderNip65HomeItems(homeFeedState, nip65ViewModel, accountViewModel, nav)
renderNip65HomeItems(homeFeedState, nip65ViewModel, accountViewModel, nav, outboxCounts)
item {
SettingsCategory(
@@ -270,7 +278,7 @@ fun MappedAllRelayListView(
SettingsCategorySpacingModifier,
)
}
renderNip65NotifItems(notifFeedState, nip65ViewModel, accountViewModel, nav)
renderNip65NotifItems(notifFeedState, nip65ViewModel, accountViewModel, nav, inboxCounts)
item {
SettingsCategoryWithButton(
@@ -282,7 +290,7 @@ fun MappedAllRelayListView(
},
)
}
renderDMItems(dmFeedState, dmViewModel, accountViewModel, nav)
renderDMItems(dmFeedState, dmViewModel, accountViewModel, nav, dmCounts)
item {
SettingsCategory(
@@ -291,7 +299,7 @@ fun MappedAllRelayListView(
SettingsCategorySpacingModifier,
)
}
renderPrivateOutboxItems(privateOutboxFeedState, privateOutboxViewModel, accountViewModel, nav)
renderPrivateOutboxItems(privateOutboxFeedState, privateOutboxViewModel, accountViewModel, nav, privateHomeCounts)
item {
SettingsCategory(
@@ -300,7 +308,7 @@ fun MappedAllRelayListView(
SettingsCategorySpacingModifier,
)
}
renderProxyItems(proxyRelays, proxyViewModel, accountViewModel, nav)
renderProxyItems(proxyRelays, proxyViewModel, accountViewModel, nav, proxyCounts)
item {
SettingsCategory(
@@ -320,7 +328,7 @@ fun MappedAllRelayListView(
ResetIndexerRelays(indexerViewModel)
}
}
renderIndexerItems(indexerRelays, indexerViewModel, accountViewModel, nav)
renderIndexerItems(indexerRelays, indexerViewModel, accountViewModel, nav, indexerCounts)
item {
SettingsCategoryWithButton(
@@ -331,7 +339,7 @@ fun MappedAllRelayListView(
ResetSearchRelays(searchViewModel)
}
}
renderSearchItems(searchFeedState, searchViewModel, accountViewModel, nav)
renderSearchItems(searchFeedState, searchViewModel, accountViewModel, nav, searchCounts)
item {
SettingsCategory(
@@ -63,6 +63,7 @@ fun BasicRelaySetupInfoClickableRow(
onClick: () -> Unit,
nip11CachedRetriever: Nip11CachedRetriever,
modifier: Modifier = Modifier,
countResult: RelayCountResult? = null,
accountViewModel: AccountViewModel,
nav: INav,
) {
@@ -104,6 +105,11 @@ fun BasicRelaySetupInfoClickableRow(
UsedBy(item, accountViewModel, nav)
RelayEventCountRow(
countResult = countResult,
modifier = ReactionRowHeightChatMaxWidth,
)
RelayStatusRow(
item = item,
onClick = onClick,
@@ -32,6 +32,7 @@ fun BasicRelaySetupInfoDialog(
item: BasicRelaySetupInfo,
nip11CachedRetriever: Nip11CachedRetriever,
onDelete: ((BasicRelaySetupInfo) -> Unit)?,
countResult: RelayCountResult? = null,
accountViewModel: AccountViewModel,
nav: INav,
) {
@@ -43,6 +44,7 @@ fun BasicRelaySetupInfoDialog(
onClick = { nav.nav(Route.RelayInfo(item.relay.url)) },
nip11CachedRetriever = nip11CachedRetriever,
modifier = HalfVertPadding,
countResult = countResult,
accountViewModel = accountViewModel,
nav = nav,
)
@@ -26,6 +26,7 @@ import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.service.replace
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.queryCountSuspend
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
@@ -40,6 +41,9 @@ abstract class BasicRelaySetupInfoModel : ViewModel() {
private val _relays = MutableStateFlow<List<BasicRelaySetupInfo>>(emptyList())
val relays = _relays.asStateFlow()
private val _countResults = MutableStateFlow<Map<NormalizedRelayUrl, RelayCountResult>>(emptyMap())
val countResults = _countResults.asStateFlow()
var hasModified = false
fun init(accountViewModel: AccountViewModel) {
@@ -50,12 +54,15 @@ abstract class BasicRelaySetupInfoModel : ViewModel() {
fun load() {
clear()
loadRelayDocuments()
loadCounts()
}
abstract fun getRelayList(): List<NormalizedRelayUrl>?
abstract suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>)
open fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> = emptyList()
fun create() {
if (hasModified) {
accountViewModel.launchSigner {
@@ -79,6 +86,40 @@ abstract class BasicRelaySetupInfoModel : ViewModel() {
}
}
private fun loadCounts() {
_countResults.value = emptyMap()
val client = account.client
val relayList = _relays.value
if (relayList.isEmpty()) return
relayList.forEach { item ->
val filters = countFilters(item.relay)
if (filters.isEmpty()) return@forEach
filters.forEach { countFilter ->
viewModelScope.launch(Dispatchers.IO) {
val result = client.queryCountSuspend(item.relay, countFilter.filter)
if (result != null) {
_countResults.update { currentMap ->
val current = currentMap[item.relay] ?: RelayCountResult()
val entries = current.counts.toMutableList()
val newEntry =
RelayCountResult.CountEntry(
label = countFilter.label,
count = result.count,
approximate = result.approximate,
)
val existing = entries.indexOfFirst { it.label == countFilter.label }
if (existing >= 0) entries[existing] = newEntry else entries.add(newEntry)
currentMap + (item.relay to RelayCountResult(entries))
}
}
}
}
}
}
open fun relayListBuilder(): List<BasicRelaySetupInfo> {
val relayList = getRelayList() ?: emptyList()
@@ -0,0 +1,105 @@
/*
* 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.ui.screen.loggedIn.relays.common
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Storage
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.service.countToHumanReadable
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.Font10SP
import com.vitorpamplona.amethyst.ui.theme.Size10Modifier
import com.vitorpamplona.amethyst.ui.theme.allGoodColor
private val PillShape = RoundedCornerShape(12.dp)
@Composable
fun RelayEventCountRow(
countResult: RelayCountResult?,
modifier: Modifier,
) {
if (countResult == null || countResult.counts.isEmpty()) return
val pillColor = MaterialTheme.colorScheme.allGoodColor
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start,
modifier = modifier,
) {
countResult.counts.forEachIndexed { index, entry ->
if (index > 0) {
Spacer(modifier = Modifier.width(6.dp))
}
val countText =
if (entry.approximate) {
"~${countToHumanReadable(entry.count, stringRes(entry.label))}"
} else {
countToHumanReadable(entry.count, stringRes(entry.label))
}
Row(
verticalAlignment = Alignment.CenterVertically,
modifier =
Modifier
.clip(PillShape)
.border(width = 1.dp, color = pillColor.copy(alpha = 0.4f), shape = PillShape)
.background(pillColor.copy(alpha = 0.1f))
.padding(horizontal = 6.dp, vertical = 2.dp),
) {
Icon(
imageVector = Icons.Default.Storage,
contentDescription = stringRes(R.string.relay_event_count),
modifier = Size10Modifier,
tint = pillColor,
)
Spacer(modifier = Modifier.width(3.dp))
Text(
text = countText,
maxLines = 1,
fontSize = Font10SP,
fontWeight = FontWeight.Medium,
color = pillColor,
)
}
}
}
}
@@ -0,0 +1,41 @@
/*
* 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.ui.screen.loggedIn.relays.common
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
@Immutable
data class RelayCountResult(
val counts: List<CountEntry> = emptyList(),
) {
@Immutable
data class CountEntry(
val label: Int,
val count: Int,
val approximate: Boolean = false,
)
}
data class CountFilter(
val label: Int,
val filter: Filter,
)
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun DMRelayList(
@@ -64,12 +66,14 @@ fun LazyListScope.renderDMItems(
postViewModel: DMRelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "DM" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -21,8 +21,13 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.dm
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip04Dm.messages.PrivateDmEvent
import com.vitorpamplona.quartz.nip59Giftwrap.wraps.GiftWrapEvent
@Stable
class DMRelayListViewModel : BasicRelaySetupInfoModel() {
@@ -31,4 +36,16 @@ class DMRelayListViewModel : BasicRelaySetupInfoModel() {
override suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>) {
account.saveDMRelayList(urlList)
}
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = R.string.dms,
filter =
Filter(
kinds = listOf(GiftWrapEvent.KIND, PrivateDmEvent.KIND),
tags = mapOf("p" to listOf(account.pubKey)),
),
),
)
}
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun IndexerRelayList(
@@ -65,12 +67,14 @@ fun LazyListScope.renderIndexerItems(
postViewModel: IndexerRelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Indexer" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -21,8 +21,13 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.indexer
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.metadata.MetadataEvent
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip65RelayList.AdvertisedRelayListEvent
@Stable
class IndexerRelayListViewModel : BasicRelaySetupInfoModel() {
@@ -33,4 +38,16 @@ class IndexerRelayListViewModel : BasicRelaySetupInfoModel() {
override suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>) {
account.saveIndexerRelayList(urlList)
}
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = R.string.profiles,
filter = Filter(kinds = listOf(MetadataEvent.KIND)),
),
CountFilter(
label = R.string.relay_settings_lower,
filter = Filter(kinds = listOf(AdvertisedRelayListEvent.KIND)),
),
)
}
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun PrivateOutboxRelayList(
@@ -64,12 +66,14 @@ fun LazyListScope.renderPrivateOutboxItems(
postViewModel: PrivateOutboxRelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Outbox" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -21,7 +21,10 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.nip37
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Stable
@@ -33,4 +36,12 @@ class PrivateOutboxRelayListViewModel : BasicRelaySetupInfoModel() {
override suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>) {
account.savePrivateOutboxRelayList(urlList)
}
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = R.string.events,
filter = Filter(authors = listOf(account.pubKey)),
),
)
}
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun Nip65RelayList(
@@ -107,12 +109,14 @@ fun LazyListScope.renderNip65HomeItems(
postViewModel: Nip65RelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Nip65Home" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteHomeRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -133,12 +137,14 @@ fun LazyListScope.renderNip65NotifItems(
postViewModel: Nip65RelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Nip65Notif" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteNotifRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -24,11 +24,16 @@ import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.service.replace
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.queryCountSuspend
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip65RelayList.tags.AdvertisedRelayInfo
import com.vitorpamplona.quartz.nip65RelayList.tags.AdvertisedRelayType
import kotlinx.coroutines.Dispatchers
@@ -48,6 +53,12 @@ class Nip65RelayListViewModel : ViewModel() {
private val _notificationRelays = MutableStateFlow<List<BasicRelaySetupInfo>>(emptyList())
val notificationRelays = _notificationRelays.asStateFlow()
private val _homeCountResults = MutableStateFlow<Map<NormalizedRelayUrl, RelayCountResult>>(emptyMap())
val homeCountResults = _homeCountResults.asStateFlow()
private val _notifCountResults = MutableStateFlow<Map<NormalizedRelayUrl, RelayCountResult>>(emptyMap())
val notifCountResults = _notifCountResults.asStateFlow()
var hasModified = false
fun init(accountViewModel: AccountViewModel) {
@@ -58,6 +69,7 @@ class Nip65RelayListViewModel : ViewModel() {
fun load() {
clear()
loadRelayDocuments()
loadCounts()
}
fun create() {
@@ -111,6 +123,51 @@ class Nip65RelayListViewModel : ViewModel() {
}
}
private fun loadCounts() {
_homeCountResults.value = emptyMap()
_notifCountResults.value = emptyMap()
val client = Amethyst.instance.client
_homeRelays.value.forEach { item ->
viewModelScope.launch(Dispatchers.IO) {
val result = client.queryCountSuspend(item.relay, Filter(authors = listOf(account.pubKey)))
if (result != null) {
val countResult =
RelayCountResult(
listOf(
RelayCountResult.CountEntry(
label = R.string.events,
count = result.count,
approximate = result.approximate,
),
),
)
_homeCountResults.update { it + (item.relay to countResult) }
}
}
}
_notificationRelays.value.forEach { item ->
viewModelScope.launch(Dispatchers.IO) {
val result = client.queryCountSuspend(item.relay, Filter(tags = mapOf("p" to listOf(account.pubKey))))
if (result != null) {
val countResult =
RelayCountResult(
listOf(
RelayCountResult.CountEntry(
label = R.string.events,
count = result.count,
approximate = result.approximate,
),
),
)
_notifCountResults.update { it + (item.relay to countResult) }
}
}
}
}
fun clear() {
hasModified = false
_homeRelays.update {
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun ProxyRelayList(
@@ -65,12 +67,14 @@ fun LazyListScope.renderProxyItems(
postViewModel: ProxyRelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Proxy" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -35,10 +35,12 @@ import com.vitorpamplona.amethyst.ui.navigation.navs.rememberExtendedNav
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayCountResult
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.RelayUrlEditField
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.relaySetupInfoBuilder
import com.vitorpamplona.amethyst.ui.theme.FeedPadding
import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Composable
fun SearchRelayList(
@@ -65,12 +67,14 @@ fun LazyListScope.renderSearchItems(
postViewModel: SearchRelayListViewModel,
accountViewModel: AccountViewModel,
nav: INav,
countResults: Map<NormalizedRelayUrl, RelayCountResult> = emptyMap(),
) {
itemsIndexed(feedState, key = { _, item -> "Search" + item.relay.url }) { index, item ->
BasicRelaySetupInfoDialog(
item,
onDelete = { postViewModel.deleteRelay(item) },
nip11CachedRetriever = Amethyst.instance.nip11Cache,
countResult = countResults[item.relay],
accountViewModel = accountViewModel,
nav = nav,
)
@@ -21,7 +21,10 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.search
import androidx.compose.runtime.Stable
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.BasicRelaySetupInfoModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.relays.common.CountFilter
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
@Stable
@@ -33,4 +36,12 @@ class SearchRelayListViewModel : BasicRelaySetupInfoModel() {
override suspend fun saveRelayList(urlList: List<NormalizedRelayUrl>) {
account.saveSearchRelayList(urlList)
}
override fun countFilters(relayUrl: NormalizedRelayUrl): List<CountFilter> =
listOf(
CountFilter(
label = R.string.events,
filter = Filter(),
),
)
}
+5
View File
@@ -776,6 +776,7 @@
<string name="write_to_relay">Write to Relay</string>
<string name="write_to_relay_description">The amount in bytes that was sent to this relay, including filters and events</string>
<string name="read_from_relay_description">The amount in bytes that was received from this relay, including filters and events</string>
<string name="relay_event_count">Events stored</string>
<string name="an_error_occurred_trying_to_get_relay_information">An error occurred trying to get relay information from %1$s</string>
<string name="owner">Owner</string>
<string name="used_by">Used By</string>
@@ -1801,4 +1802,8 @@
<string name="uptime">%1$d%% uptime</string>
<string name="namecoin_settings">Namecoin Settings</string>
<string name="ots_explorer_settings">Bitcoin Explorer (OTS)</string>
<string name="events">events</string>
<string name="dms">DMs</string>
<string name="profiles">profiles</string>
<string name="relay_settings_lower">relay settings</string>
</resources>
@@ -90,6 +90,10 @@ object MessageKSerializer : KSerializer<Message> {
is CountMessage -> {
add(CountResultKSerializer.serializeToElement(value.result))
}
is EoseMessage -> {
add(JsonPrimitive(value.subId))
}
}
}
jsonEncoder.encodeJsonElement(element)
@@ -0,0 +1,136 @@
/*
* 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.quartz.nip01Core.relay.client.accessories
import com.vitorpamplona.quartz.nip01Core.relay.client.INostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.IRelayClientListener
import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
import com.vitorpamplona.quartz.nip01Core.relay.client.single.newSubId
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.CountMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.CountResult
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.Message
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.withTimeoutOrNull
/**
* Sends a NIP-45 COUNT query to a single relay and suspends until
* the result arrives or the timeout expires.
*
* @param relay Target relay to query.
* @param filter The filter to count against.
* @param timeoutMs How long to wait for a response (default 15 s).
* @return The [CountResult], or `null` on timeout.
*/
suspend fun INostrClient.queryCountSuspend(
relay: NormalizedRelayUrl,
filter: Filter,
timeoutMs: Long = 15_000,
): CountResult? {
val subId = newSubId()
val resultChannel = Channel<CountResult>(UNLIMITED)
val listener =
object : IRelayClientListener {
override fun onIncomingMessage(
relay: IRelayClient,
msgStr: String,
msg: Message,
) {
if (msg is CountMessage && msg.queryId == subId) {
resultChannel.trySend(msg.result)
}
}
}
subscribe(listener)
queryCount(subId = subId, filters = mapOf(relay to listOf(filter)))
val result =
withTimeoutOrNull(timeoutMs) {
resultChannel.receive()
}
close(subId)
unsubscribe(listener)
resultChannel.close()
return result
}
/**
* Sends NIP-45 COUNT queries to multiple relays in parallel
* (one filter per relay) and suspends until all results arrive
* or the timeout expires.
*
* @param filters Map of relay -> filter to count.
* @param timeoutMs How long to wait for all responses (default 15 s).
* @return Map of relay -> [CountResult] for every relay that responded in time.
*/
suspend fun INostrClient.queryCountSuspend(
filters: Map<NormalizedRelayUrl, List<Filter>>,
timeoutMs: Long = 15_000,
): Map<NormalizedRelayUrl, CountResult> {
if (filters.isEmpty()) return emptyMap()
val subIdToRelay = mutableMapOf<String, NormalizedRelayUrl>()
val resultChannel = Channel<Pair<NormalizedRelayUrl, CountResult>>(UNLIMITED)
val listener =
object : IRelayClientListener {
override fun onIncomingMessage(
relay: IRelayClient,
msgStr: String,
msg: Message,
) {
if (msg is CountMessage) {
val relayUrl = subIdToRelay[msg.queryId] ?: return
resultChannel.trySend(relayUrl to msg.result)
}
}
}
subscribe(listener)
filters.forEach { (relay, filterList) ->
val subId = newSubId()
subIdToRelay[subId] = relay
queryCount(subId = subId, filters = mapOf(relay to filterList))
}
val results = mutableMapOf<NormalizedRelayUrl, CountResult>()
withTimeoutOrNull(timeoutMs) {
while (results.size < filters.size) {
val (relay, result) = resultChannel.receive()
results[relay] = result
}
}
subIdToRelay.keys.forEach { close(it) }
unsubscribe(listener)
resultChannel.close()
return results
}
@@ -25,6 +25,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.IRelayClientLis
import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.AuthMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.ClosedMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.CountMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.EoseMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.EventMessage
import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.Message
@@ -62,6 +63,7 @@ class RelayLogger(
is OkMessage -> if (debugReceiving) Log.d(logTag, "OK: ${msg.eventId} ${msg.success} ${msg.message}")
is AuthMessage -> if (debugReceiving) Log.d(logTag, "Auth: ${msg.challenge}")
is NotifyMessage -> if (debugReceiving) Log.d(logTag, "Notify: ${msg.message}")
is CountMessage -> if (debugReceiving) Log.d(logTag, "Count: ${msg.result.count} approx: ${msg.result.approximate}")
is ClosedMessage -> Log.w(logTag, "Closed: ${msg.subId} ${msg.message}")
}
}
@@ -26,8 +26,8 @@ class CountResultDeserializer {
companion object {
fun fromJson(jsonObject: JsonNode): CountResult =
CountResult(
count = jsonObject.get("count").asInt(),
approximate = jsonObject.get("approximate").asBoolean(),
count = jsonObject.get("count")?.asInt() ?: 0,
approximate = jsonObject.get("approximate")?.asBoolean() ?: false,
)
}
}
@@ -72,8 +72,8 @@ class MessageSerializer : StdSerializer<Message>(Message::class.java) {
countSerializer.serialize(msg.result, gen, provider)
}
else -> {
null
is EoseMessage -> {
gen.writeString(msg.subId)
}
}
@@ -0,0 +1,93 @@
/*
* 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.quartz.nip01Core.relay
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
import com.vitorpamplona.quartz.nip01Core.relay.client.accessories.queryCountSuspend
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.normalizeRelayUrl
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
class NostrClientQueryCountTest : BaseNostrClientTest() {
val fiatjaf = "wss://pyramid.fiatjaf.com".normalizeRelayUrl()
val utxo = "wss://news.utxo.one".normalizeRelayUrl()
val metadata = Filter(kinds = listOf(0))
val outboxRelays = Filter(kinds = listOf(10002))
@Test
fun testQueryCountSuspend() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result = client.queryCountSuspend(relay = fiatjaf, filter = metadata)
assertTrue((result?.count ?: 0) > 1)
client.disconnect()
appScope.cancel()
}
@Test
fun testQueryCountSuspendAllEvents() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result = client.queryCountSuspend(relay = fiatjaf, filter = Filter())
assertTrue((result?.count ?: 0) > 1)
client.disconnect()
appScope.cancel()
}
@Test
fun testQueryCountSuspendMultipleRelays() =
runBlocking {
val appScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
val client = NostrClient(socketBuilder, appScope)
val result =
client.queryCountSuspend(
filters =
mapOf(
fiatjaf to listOf(metadata, outboxRelays),
utxo to listOf(metadata, outboxRelays),
),
)
result.forEach { url, result ->
println("${url.url}: ${result.count}")
assertTrue(result.count > 1)
}
client.disconnect()
appScope.cancel()
}
}