Skip to content

Commit

Permalink
Check if relay sends the bunker events back
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Nov 18, 2024
1 parent e37b9b1 commit b01d838
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
40 changes: 25 additions & 15 deletions app/src/main/java/com/greenart7c3/nostrsigner/NostrSigner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ class NostrSigner : Application() {
applicationIOScope.cancel()
}

suspend fun fetchProfileData(account: Account, onPictureFound: (String) -> Unit) {
suspend fun fetchProfileData(
account: Account,
onPictureFound: (String) -> Unit,
) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") {
return
Expand All @@ -226,7 +229,25 @@ class NostrSigner : Application() {
val oneDayAgo = TimeUtils.oneDayAgo()
if (lastMetaData == 0L || oneDayAgo > lastMetaData) {
Log.d("NostrSigner", "Fetching profile data for ${account.signer.keyPair.pubKey.toNpub()}")
val listener = RelayListener(account, onPictureFound)
val listener = RelayListener(
account = account,
onReceiveEvent = { _, _, event ->
if (event.kind == MetadataEvent.KIND) {
(event as MetadataEvent).contactMetaData()?.let { metadata ->
metadata.name?.let { name ->
account.name = name
LocalPreferences.saveToEncryptedStorage(account = account, context = this)
}

metadata.profilePicture()?.let { url ->
LocalPreferences.saveProfileUrlToEncryptedStorage(url, account.signer.keyPair.pubKey.toNpub())
LocalPreferences.setLastMetadataUpdate(this, account.signer.keyPair.pubKey.toNpub(), TimeUtils.now())
onPictureFound(url)
}
}
}
},
)

val relays = listOf(
Relay(
Expand Down Expand Up @@ -285,7 +306,7 @@ class NostrSigner : Application() {

class RelayListener(
val account: Account,
val onPictureFound: (String) -> Unit,
val onReceiveEvent: (relay: Relay, subscriptionId: String, event: Event) -> Unit,
) : Relay.Listener {
override fun onAuth(relay: Relay, challenge: String) {
Log.d("RelayListener", "Received auth challenge $challenge from relay ${relay.url}")
Expand All @@ -301,18 +322,7 @@ class RelayListener(

override fun onEvent(relay: Relay, subscriptionId: String, event: Event, afterEOSE: Boolean) {
Log.d("RelayListener", "Received event ${event.toJson()} from subscription $subscriptionId afterEOSE: $afterEOSE")
(event as MetadataEvent).contactMetaData()?.let { metadata ->
metadata.name?.let { name ->
account.name = name
LocalPreferences.saveToEncryptedStorage(account = account, context = NostrSigner.getInstance())
}

metadata.profilePicture()?.let { url ->
LocalPreferences.saveProfileUrlToEncryptedStorage(url, account.signer.keyPair.pubKey.toNpub())
LocalPreferences.setLastMetadataUpdate(NostrSigner.getInstance(), account.signer.keyPair.pubKey.toNpub(), TimeUtils.now())
onPictureFound(url)
}
}
onReceiveEvent(relay, subscriptionId, event)
}

override fun onNotify(relay: Relay, description: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.NostrSigner
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.RelayListener
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.TimeUtils
import com.greenart7c3.nostrsigner.relays.AmberListenerSingleton
Expand All @@ -60,12 +61,15 @@ import com.vitorpamplona.ammolite.relays.Relay
import com.vitorpamplona.ammolite.relays.RelayPool
import com.vitorpamplona.ammolite.relays.RelaySetupInfo
import com.vitorpamplona.ammolite.relays.RelayStats
import com.vitorpamplona.ammolite.relays.TypedFilter
import com.vitorpamplona.ammolite.relays.filters.SincePerRelayFilter
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.RelayUrlFormatter
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.signers.NostrSignerInternal
import java.util.Base64
import java.util.UUID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -321,18 +325,47 @@ suspend fun onAddRelay(
addedWSS,
read = true,
write = true,
activeTypes = setOf(),
activeTypes = COMMON_FEED_TYPES,
forceProxy = if (isPrivateIp) false else account.useProxy,
)
RelayPool.addRelay(
relay,
)
var filterResult = false
val listener2 = RelayListener(
account,
onReceiveEvent = { _, _, event ->
if (event.kind == 24133 && event.id == signedEvent.id) {
filterResult = true
}
},
)
relay.register(
listener2,
)
relay.connect()
delay(2000)
delay(3000)
val result = Client.sendAndWaitForResponse(
signedEvent = signedEvent,
relayList = listOf(RelaySetupInfo(addedWSS, read = true, write = true, setOf())),
)
relay.sendFilter(
UUID.randomUUID().toString().substring(0, 4),
filters = listOf(
TypedFilter(
types = COMMON_FEED_TYPES,
filter = SincePerRelayFilter(
ids = listOf(event.id),
),
),
),
)
var count = 0
while (!filterResult && count < 10) {
delay(1000)
count++
}

AmberListenerSingleton.getListener()?.let { listener ->
Client.unsubscribe(listener)
}
Expand All @@ -341,7 +374,7 @@ suspend fun onAddRelay(
relay,
)

if (result) {
if (result && filterResult) {
relays2.add(
RelaySetupInfo(
addedWSS,
Expand All @@ -351,7 +384,13 @@ suspend fun onAddRelay(
),
)
onDone()
} else if (!filterResult) {
accountStateViewModel.toast(
context.getString(R.string.relay),
context.getString(R.string.relay_filter_failed),
)
}

textFieldRelay.value = TextFieldValue("")
}
isLoading.value = false
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,5 @@
<string name="is_requiring_some_permissions_please_review_them">%1$s is requiring some permissions, please review them.</string>
<string name="always_approve_this_permission">Always approve this permission</string>
<string name="is_requiring_to_sign_these_events_related_to_permission">%1$s is requiring to sign these events related to %2$s permission.</string>
<string name="relay_filter_failed">Relay does not support receiving Bunker events</string>
</resources>

0 comments on commit b01d838

Please sign in to comment.