This commit is contained in:
callebtc
2025-10-17 22:25:13 +02:00
parent 4d509ffda5
commit 6879906b71
3 changed files with 65 additions and 22 deletions
@@ -602,16 +602,24 @@ class MainActivity : ComponentActivity() {
com.bitchat.android.nostr.LocationNotesCounter.initialize( com.bitchat.android.nostr.LocationNotesCounter.initialize(
relayManager = { com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity) }, relayManager = { com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity) },
subscribe = { filter, id, handler -> subscribe = { filter, id, handler ->
// Use subscribeForGeohash for location notes (finds geo-local relays) // CRITICAL FIX: Extract geohash properly from filter (not from debug string!)
val geohashFromFilter = filter.getDebugDescription().substringAfter("#g=").substringBefore(")").substringBefore(",") val geohashFromFilter = filter.getGeohash() ?: run {
com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).subscribeForGeohash( Log.e("MainActivity", "Cannot extract geohash from filter for location notes counter")
geohash = geohashFromFilter, ""
filter = filter, }
id = id,
handler = handler, if (geohashFromFilter.isNotEmpty()) {
includeDefaults = true, com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).subscribeForGeohash(
nRelays = 5 geohash = geohashFromFilter,
) filter = filter,
id = id,
handler = handler,
includeDefaults = true,
nRelays = 5
)
} else {
id
}
}, },
unsubscribe = { id -> unsubscribe = { id ->
com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).unsubscribe(id) com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).unsubscribe(id)
@@ -623,13 +631,13 @@ class MainActivity : ComponentActivity() {
com.bitchat.android.nostr.LocationNotesManager.getInstance().initialize( com.bitchat.android.nostr.LocationNotesManager.getInstance().initialize(
relayManager = { com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity) }, relayManager = { com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity) },
subscribe = { filter, id, handler -> subscribe = { filter, id, handler ->
// Extract geohash from filter for geo-routing // CRITICAL FIX: Extract geohash properly from filter using getGeohash() method
val geohashFromFilter = filter.getDebugDescription() val geohashFromFilter = filter.getGeohash() ?: run {
.substringAfter("#g=") Log.e("MainActivity", "❌ Cannot extract geohash from filter for location notes")
.substringBefore(")") return@initialize id // Return subscription ID even on error
.substringBefore(",") }
Log.d("MainActivity", "Location Notes subscribing to geohash: $geohashFromFilter") Log.d("MainActivity", "📍 Location Notes subscribing to geohash: $geohashFromFilter")
com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).subscribeForGeohash( com.bitchat.android.nostr.NostrRelayManager.getInstance(this@MainActivity).subscribeForGeohash(
geohash = geohashFromFilter, geohash = geohashFromFilter,
@@ -157,6 +157,28 @@ class LocationNotesManager private constructor() {
return return
} }
val trimmed = content.trim()
if (trimmed.isEmpty()) {
return
}
// CRITICAL FIX: Get geo-specific relays for sending (matching iOS pattern)
// iOS: let relays = dependencies.relayLookup(geohash, TransportConfig.nostrGeoRelayCount)
val relays = try {
com.bitchat.android.nostr.RelayDirectory.closestRelaysForGeohash(currentGeohash, 5)
} catch (e: Exception) {
Log.e(TAG, "Failed to lookup relays for geohash $currentGeohash: ${e.message}")
emptyList()
}
// Check if we have relays (iOS pattern: guard !relays.isEmpty())
if (relays.isEmpty()) {
Log.w(TAG, "Send blocked - no geo relays for geohash: $currentGeohash")
_state.value = State.NO_RELAYS
_errorMessage.value = "No relays available"
return
}
val deriveIdentity = deriveIdentityFunc val deriveIdentity = deriveIdentityFunc
if (deriveIdentity == null) { if (deriveIdentity == null) {
Log.e(TAG, "Cannot send note - deriveIdentity not initialized") Log.e(TAG, "Cannot send note - deriveIdentity not initialized")
@@ -164,7 +186,7 @@ class LocationNotesManager private constructor() {
return return
} }
Log.d(TAG, "Sending note to geohash: $currentGeohash") Log.d(TAG, "Sending note to geohash: $currentGeohash via ${relays.size} geo relays")
scope.launch { scope.launch {
try { try {
@@ -174,7 +196,7 @@ class LocationNotesManager private constructor() {
val event = withContext(Dispatchers.IO) { val event = withContext(Dispatchers.IO) {
NostrProtocol.createGeohashTextNote( NostrProtocol.createGeohashTextNote(
content = content, content = trimmed,
geohash = currentGeohash, geohash = currentGeohash,
senderIdentity = identity, senderIdentity = identity,
nickname = nickname nickname = nickname
@@ -185,7 +207,7 @@ class LocationNotesManager private constructor() {
val localNote = Note( val localNote = Note(
id = event.id, id = event.id,
pubkey = event.pubkey, pubkey = event.pubkey,
content = content, content = trimmed,
createdAt = event.createdAt, createdAt = event.createdAt,
nickname = nickname nickname = nickname
) )
@@ -201,12 +223,17 @@ class LocationNotesManager private constructor() {
} }
} }
// Send to relays // CRITICAL FIX: Send to geo-specific relays (matching iOS pattern)
// iOS: dependencies.sendEvent(event, relays)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
sendEventFunc?.invoke(event, null) sendEventFunc?.invoke(event, relays)
} }
Log.d(TAG, "✅ Note sent successfully: ${event.id.take(16)}...") Log.d(TAG, "✅ Note sent successfully to ${relays.size} geo relays: ${event.id.take(16)}...")
// Clear any error messages on successful send
_errorMessage.value = null
_state.value = State.READY
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to send note: ${e.message}") Log.e(TAG, "Failed to send note: ${e.message}")
@@ -205,4 +205,12 @@ data class NostrFilter(
return "NostrFilter(${parts.joinToString(", ")})" return "NostrFilter(${parts.joinToString(", ")})"
} }
/**
* Get geohash value from g tag filter (if present)
* Returns the first geohash in the filter or null if none
*/
fun getGeohash(): String? {
return tagFilters?.get("g")?.firstOrNull()
}
} }