Skip to content

Commit

Permalink
Use the relay from the request to send the response when no app is found
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 24, 2025
1 parent 6c922f4 commit 5bd1872
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import com.greenart7c3.nostrsigner.models.Permission
import com.greenart7c3.nostrsigner.models.SignerType
import com.greenart7c3.nostrsigner.service.NotificationUtils.sendNotification
import com.greenart7c3.nostrsigner.service.model.AmberEvent
import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES
import com.vitorpamplona.ammolite.relays.Relay
import com.vitorpamplona.ammolite.relays.RelaySetupInfo
import com.vitorpamplona.quartz.crypto.nip04.Nip04
import com.vitorpamplona.quartz.encoders.Hex
import com.vitorpamplona.quartz.encoders.toNpub
Expand Down Expand Up @@ -72,7 +75,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
}
}

fun consume(event: Event) {
fun consume(event: Event, relay: Relay) {
saveLog("New event ${event.toJson()}")

if (!notificationManager().areNotificationsEnabled()) {
Expand All @@ -94,13 +97,14 @@ class EventNotificationConsumer(private val applicationContext: Context) {

val taggedKey = event.taggedUsers().firstOrNull() ?: return
LocalPreferences.loadFromEncryptedStorage(applicationContext, Hex.decode(taggedKey).toNpub())?.let { acc ->
notify(event, acc)
notify(event, acc, relay)
}
}

private fun notify(
event: Event,
acc: Account,
relay: Relay,
) {
if (event.content.isEmpty()) return

Expand All @@ -120,13 +124,13 @@ class EventNotificationConsumer(private val applicationContext: Context) {
if (Nip04.isNIP04(event.content)) {
acc.signer.nip04Decrypt(event.content, event.pubKey) {
NostrSigner.getInstance().applicationIOScope.launch {
notify(event, acc, it, EncryptionType.NIP04)
notify(event, acc, it, EncryptionType.NIP04, relay)
}
}
} else {
acc.signer.nip44Decrypt(event.content, event.pubKey) {
NostrSigner.getInstance().applicationIOScope.launch {
notify(event, acc, it, EncryptionType.NIP44)
notify(event, acc, it, EncryptionType.NIP44, relay)
}
}
}
Expand All @@ -137,7 +141,9 @@ class EventNotificationConsumer(private val applicationContext: Context) {
acc: Account,
request: String,
encryptionType: EncryptionType,
relay: Relay,
) {
val responseRelay = listOf(RelaySetupInfo(relay.url, read = true, write = true, feedTypes = COMMON_FEED_TYPES))
val dao = NostrSigner.getInstance().getDatabase(acc.signer.keyPair.pubKey.toNpub()).applicationDao()
val notification = dao.getNotification(event.id)
if (notification != null) return
Expand Down Expand Up @@ -241,7 +247,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
acc,
bunkerRequest,
BunkerResponse(bunkerRequest.id, "", message),
applicationWithSecret?.application?.relays ?: emptyList(),
applicationWithSecret?.application?.relays ?: responseRelay,
onLoading = { },
onDone = {
if (!it) {
Expand All @@ -250,7 +256,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
acc,
bunkerRequest,
BunkerResponse(bunkerRequest.id, "", message),
applicationWithSecret?.application?.relays ?: emptyList(),
applicationWithSecret?.application?.relays ?: responseRelay,
onLoading = { },
onDone = { },
)
Expand Down Expand Up @@ -294,7 +300,7 @@ class EventNotificationConsumer(private val applicationContext: Context) {
if (type == SignerType.CONNECT) {
message = "$name ${bunkerPermission.toLocalizedString(applicationContext)}"
}
val relays = permission?.application?.relays ?: applicationWithSecret?.application?.relays ?: emptyList()
val relays = permission?.application?.relays ?: applicationWithSecret?.application?.relays ?: responseRelay

if (type == SignerType.INVALID) {
Log.d("EventNotificationConsumer", "Invalid request method ${bunkerRequest.method}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ object NotificationDataSource : NostrDataSource(NostrSigner.getInstance().client
checkNotInMainThread()
NotificationUtils.getOrCreateDMChannel(NostrSigner.getInstance().applicationContext)
AmberRelayStats.addReceived(relay.url)
eventNotificationConsumer.consume(event)
eventNotificationConsumer.consume(event, relay)
}

private val accountChannel =
Expand Down

0 comments on commit 5bd1872

Please sign in to comment.