diff --git a/app/src/main/java/com/koalasat/pokey/models/NostrClient.kt b/app/src/main/java/com/koalasat/pokey/models/NostrClient.kt index ccca315..486bced 100644 --- a/app/src/main/java/com/koalasat/pokey/models/NostrClient.kt +++ b/app/src/main/java/com/koalasat/pokey/models/NostrClient.kt @@ -1,6 +1,8 @@ package com.koalasat.pokey.models import android.content.Context +import android.os.Handler +import android.os.Looper import android.util.Log import com.koalasat.pokey.Pokey import com.koalasat.pokey.database.AppDatabase @@ -17,12 +19,17 @@ import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.utils.TimeUtils import java.time.Instant +import java.util.concurrent.ConcurrentHashMap +import org.json.JSONException +import org.json.JSONObject object NostrClient { private var subscriptionNotificationId = "subscriptionNotificationId" private var subscriptionInboxId = "inboxRelays" private var subscriptionReadId = "readRelays" + private var usersNip05 = ConcurrentHashMap() + private var defaultRelayUrls = listOf( "wss://relay.damus.io", "wss://offchain.pub", @@ -211,6 +218,51 @@ object NostrClient { } } + fun getNip05Content(hexPubKey: String, onResponse: (JSONObject?) -> Unit) { + if (usersNip05.containsKey(hexPubKey)) { + Log.d("Pokey", "Recovering NIP05") + onResponse(usersNip05.getValue(hexPubKey)) + } else { + Log.d("Pokey", "Fetching NIP05") + val handler = Handler(Looper.getMainLooper()) + val timeoutRunnable = Runnable { + Log.d("Pokey", "NIP05 not found") + onResponse(null) + } + handler.postDelayed(timeoutRunnable, 5000) + + Client.sendFilterAndStopOnFirstResponse( + subscriptionReadId, + listOf( + TypedFilter( + types = EVENT_FINDER_TYPES, + filter = SincePerRelayFilter( + kinds = listOf(0), + authors = listOf(hexPubKey), + ), + ), + ), + onResponse = { event -> + if (event.pubKey == hexPubKey) { + Log.d("Pokey", "NIP05 found") + handler.removeCallbacks(timeoutRunnable) + if (event.content.isNotEmpty()) { + try { + val content = JSONObject(event.content) + usersNip05.put(event.pubKey, content) + onResponse(content) + } catch (e: JSONException) { + Log.d("Pokey", "Invalid NIP05 JSON: $e") + usersNip05.put(event.pubKey, JSONObject()) + onResponse(null) + } + } + } + }, + ) + } + } + private fun getInboxLists(context: Context) { val hexKey = Pokey.getInstance().getHexKey() if (hexKey.isEmpty()) return diff --git a/app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt b/app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt index e508be8..caa92b6 100644 --- a/app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt +++ b/app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt @@ -12,7 +12,7 @@ class BootReceiver : BroadcastReceiver() { if (!Pokey.isForegroundServiceEnabled(context)) return if (intent.action == Intent.ACTION_PACKAGE_REPLACED && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { - if (intent.dataString?.contains("com.greenart7c3.nostrsigner") == true) { + if (intent.dataString?.contains("com.koalasat.pokey") == true && Pokey.isForegroundServiceEnabled(context)) { Log.d("BootReceiver", "Starting ConnectivityService ACTION_PACKAGE_REPLACED") context.startForegroundService( Intent( @@ -25,22 +25,26 @@ class BootReceiver : BroadcastReceiver() { if (intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) { Log.d("BootReceiver", "Starting ConnectivityService ACTION_MY_PACKAGE_REPLACED") - context.startForegroundService( - Intent( - context, - NotificationsService::class.java, - ), - ) + if (Pokey.isForegroundServiceEnabled(context)) { + context.startForegroundService( + Intent( + context, + NotificationsService::class.java, + ), + ) + } } if (intent.action == Intent.ACTION_BOOT_COMPLETED) { Log.d("BootReceiver", "Starting ConnectivityService ACTION_BOOT_COMPLETED") - context.startForegroundService( - Intent( - context, - NotificationsService::class.java, - ), - ) + if (Pokey.isForegroundServiceEnabled(context)) { + context.startForegroundService( + Intent( + context, + NotificationsService::class.java, + ), + ) + } } } } diff --git a/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt b/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt index 3ce32ab..c97d1b6 100644 --- a/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt +++ b/app/src/main/java/com/koalasat/pokey/service/NotificationsService.kt @@ -340,13 +340,21 @@ class NotificationsService : Service() { if (title.isEmpty()) return@launch - displayNoteNotification(title, text, nip32Bech32, event) + NostrClient.getNip05Content(event.pubKey, onResponse = { + try { + var authorName = it?.getString("name") + if (authorName?.isNotEmpty() == true) { + title += " from $authorName" + } + } catch (e: JSONException) { } + displayNoteNotification(title, text, nip32Bech32, event) + }) } } - private fun displayNoteNotification(title: String, text: String, nip32Bech32: String, event: Event) { + private fun displayNoteNotification(title: String, text: String, authorBech32: String, event: Event) { val deepLinkIntent = Intent(Intent.ACTION_VIEW).apply { - data = Uri.parse("nostr:$nip32Bech32") + data = Uri.parse("nostr:$authorBech32") } val pendingIntent = PendingIntent.getActivity( this@NotificationsService,