nostr: fix block feature (#427)

* fix block feature

* fix nostr nickname in sidebar
This commit is contained in:
callebtc
2025-09-14 17:30:47 +02:00
committed by GitHub
parent a4ef2ef29c
commit 277dbdf4e1
5 changed files with 64 additions and 32 deletions
@@ -22,7 +22,8 @@ class GeohashMessageHandler(
private val state: ChatState,
private val messageManager: MessageManager,
private val repo: GeohashRepository,
private val scope: CoroutineScope
private val scope: CoroutineScope,
private val dataManager: com.bitchat.android.ui.DataManager
) {
companion object { private const val TAG = "GeohashMessageHandler" }
@@ -56,8 +57,8 @@ class GeohashMessageHandler(
if (!NostrProofOfWork.validateDifficulty(event, pow.difficulty)) return@launch
}
// Blocked users check
if (com.bitchat.android.ui.DataManager(application).isGeohashUserBlocked(event.pubkey)) return@launch
// Blocked users check (use injected DataManager which has loaded state)
if (dataManager.isGeohashUserBlocked(event.pubkey)) return@launch
// Update repository (participants, nickname, teleport)
// Update repository on a background-safe path; repository will post updates to LiveData
@@ -14,7 +14,8 @@ import java.util.Date
*/
class GeohashRepository(
private val application: Application,
private val state: ChatState
private val state: ChatState,
private val dataManager: com.bitchat.android.ui.DataManager
) {
companion object { private const val TAG = "GeohashRepository" }
@@ -103,7 +104,8 @@ class GeohashRepository(
val e = it.next()
if (e.value.before(cutoff)) it.remove()
}
return participants.size
// exclude blocked users
return participants.keys.count { !dataManager.isGeohashUserBlocked(it) }
}
fun refreshGeohashPeople() {
@@ -122,8 +124,18 @@ class GeohashRepository(
if (e.value.before(cutoff)) it.remove()
}
geohashParticipants[geohash] = participants
val people = participants.map { (pubkeyHex, lastSeen) ->
val base = getCachedNickname(pubkeyHex) ?: "anon"
// exclude blocked users from people list
val people = participants.filterKeys { !dataManager.isGeohashUserBlocked(it) }
.map { (pubkeyHex, lastSeen) ->
// Use our actual nickname for self; otherwise use cached nickname or anon
val base = try {
val myHex = currentGeohash?.let { NostrIdentityBridge.deriveIdentity(it, application).publicKeyHex }
if (myHex != null && myHex.equals(pubkeyHex, true)) {
state.getNicknameValue() ?: "anon"
} else {
getCachedNickname(pubkeyHex) ?: "anon"
}
} catch (_: Exception) { getCachedNickname(pubkeyHex) ?: "anon" }
GeoPerson(
id = pubkeyHex.lowercase(),
displayName = base, // UI can add #hash if necessary
@@ -138,7 +150,8 @@ class GeohashRepository(
val cutoff = Date(System.currentTimeMillis() - 5 * 60 * 1000)
val counts = mutableMapOf<String, Int>()
for ((gh, participants) in geohashParticipants) {
val active = participants.values.count { !it.before(cutoff) }
val active = participants.filterKeys { !dataManager.isGeohashUserBlocked(it) }
.values.count { !it.before(cutoff) }
counts[gh] = active
}
// Use postValue for thread safety - this can be called from background threads
@@ -186,6 +199,7 @@ class GeohashRepository(
val participants = geohashParticipants[current] ?: emptyMap()
var count = 0
for ((k, t) in participants) {
if (dataManager.isGeohashUserBlocked(k)) continue
if (t.before(cutoff)) continue
val name = if (k.equals(lower, true)) base else (geoNicknames[k.lowercase()] ?: "anon")
if (name.equals(base, true)) { count++; if (count > 1) break }
@@ -207,6 +221,7 @@ class GeohashRepository(
val participants = geohashParticipants[sourceGeohash] ?: emptyMap()
var count = 0
for ((k, t) in participants) {
if (dataManager.isGeohashUserBlocked(k)) continue
if (t.before(cutoff)) continue
val name = if (k.equals(lower, true)) base else (geoNicknames[k.lowercase()] ?: "anon")
if (name.equals(base, true)) { count++; if (count > 1) break }
@@ -21,7 +21,8 @@ class NostrDirectMessageHandler(
private val privateChatManager: PrivateChatManager,
private val meshDelegateHandler: MeshDelegateHandler,
private val scope: CoroutineScope,
private val repo: GeohashRepository
private val repo: GeohashRepository,
private val dataManager: com.bitchat.android.ui.DataManager
) {
companion object { private const val TAG = "NostrDirectMessageHandler" }
@@ -58,6 +59,10 @@ class NostrDirectMessageHandler(
}
val (content, senderPubkey, rumorTimestamp) = decryptResult
// If sender is blocked for geohash contexts, drop any events from this pubkey
// Applies to both geohash DMs (geohash != "") and account DMs (geohash == "")
if (dataManager.isGeohashUserBlocked(senderPubkey)) return@launch
if (!content.startsWith("bitchat1:")) return@launch
val base64Content = content.removePrefix("bitchat1:")