Skip to content

Commit

Permalink
Merge pull request #43 from KoalaSat/display-nip05
Browse files Browse the repository at this point in the history
display nip05
  • Loading branch information
KoalaSat authored Nov 14, 2024
2 parents 80bf03b + ed6a74d commit 55d6876
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
52 changes: 52 additions & 0 deletions app/src/main/java/com/koalasat/pokey/models/NostrClient.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String, JSONObject>()

private var defaultRelayUrls = listOf(
"wss://relay.damus.io",
"wss://offchain.pub",
Expand Down Expand Up @@ -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
Expand Down
30 changes: 17 additions & 13 deletions app/src/main/java/com/koalasat/pokey/service/BootReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 55d6876

Please sign in to comment.