Avoids sending an NWC filter when there are no keys subscribed to it

This commit is contained in:
Vitor Pamplona
2025-05-06 08:56:47 -04:00
parent 49c131a5cd
commit a05a465f69

View File

@@ -37,25 +37,37 @@ class NWCPaymentQueryState(
class NWCPaymentFilterAssembler( class NWCPaymentFilterAssembler(
client: NostrClient, client: NostrClient,
) : QueryBasedSubscriptionOrchestrator<NWCPaymentQueryState>(client) { ) : QueryBasedSubscriptionOrchestrator<NWCPaymentQueryState>(client) {
fun createAccountMetadataFilter(keys: Set<NWCPaymentQueryState>): TypedFilter = fun createAccountMetadataFilter(keys: Set<NWCPaymentQueryState>): TypedFilter? {
TypedFilter( val fromAuthors = keys.mapTo(mutableSetOf()) { it.fromServiceHex }
val replyingToPayments = keys.mapTo(mutableSetOf()) { it.replyingToHex }
val aboutUsers = keys.mapTo(mutableSetOf()) { it.toUserHex }
if (fromAuthors.isEmpty()) return null
return TypedFilter(
types = setOf(FeedType.WALLET_CONNECT), types = setOf(FeedType.WALLET_CONNECT),
filter = filter =
SincePerRelayFilter( SincePerRelayFilter(
kinds = listOf(LnZapPaymentResponseEvent.KIND), kinds = listOf(LnZapPaymentResponseEvent.KIND),
authors = keys.map { it.fromServiceHex }, authors = fromAuthors.toList(),
tags = tags =
mapOf( mapOf(
"e" to keys.map { it.replyingToHex }, "e" to replyingToPayments.toList(),
"p" to keys.map { it.toUserHex }, "p" to aboutUsers.toList(),
), ),
limit = 1, limit = 1,
), ),
) )
}
val channel = requestNewSubscription() val channel = requestNewSubscription()
override fun updateSubscriptions(keys: Set<NWCPaymentQueryState>) { override fun updateSubscriptions(keys: Set<NWCPaymentQueryState>) {
channel.typedFilters = listOf(createAccountMetadataFilter(keys)) val filter = createAccountMetadataFilter(keys)
if (filter != null) {
channel.typedFilters = listOf(filter)
} else {
channel.typedFilters = null
}
} }
} }