fix: exclude self from nickname disambiguation suffix

This commit is contained in:
callebtc
2026-01-17 10:54:54 +07:00
parent ad7034577d
commit f4dced3399
2 changed files with 10 additions and 3 deletions
@@ -47,7 +47,7 @@ class BluetoothMeshService(private val context: Context) {
// My peer identification - derived from persisted Noise identity fingerprint (first 16 hex chars) // My peer identification - derived from persisted Noise identity fingerprint (first 16 hex chars)
val myPeerID: String = encryptionService.getIdentityFingerprint().take(16) val myPeerID: String = encryptionService.getIdentityFingerprint().take(16)
private val peerManager = PeerManager() private val peerManager = PeerManager().apply { myPeerID = this@BluetoothMeshService.myPeerID }
private val fragmentManager = FragmentManager() private val fragmentManager = FragmentManager()
private val securityManager = SecurityManager(encryptionService, myPeerID) private val securityManager = SecurityManager(encryptionService, myPeerID)
private val storeForwardManager = StoreForwardManager() private val storeForwardManager = StoreForwardManager()
@@ -89,6 +89,9 @@ class PeerManager {
// Callback to check if a peer is directly connected (injected by BluetoothMeshService) // Callback to check if a peer is directly connected (injected by BluetoothMeshService)
var isPeerDirectlyConnected: ((String) -> Boolean)? = null var isPeerDirectlyConnected: ((String) -> Boolean)? = null
// My own Peer ID (to treat specially in disambiguation and cleanup)
var myPeerID: String? = null
// Coroutines // Coroutines
private val managerScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val managerScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
@@ -305,7 +308,9 @@ class PeerManager {
val info = peers[peerID] ?: return peerID val info = peers[peerID] ?: return peerID
val nick = info.nickname.trim() val nick = info.nickname.trim()
val isAmbiguous = peers.values.count { it.nickname.trim().equals(nick, ignoreCase = true) } > 1 val isAmbiguous = peers.values.count { it.nickname.trim().equals(nick, ignoreCase = true) } > 1
return if (isAmbiguous) "$nick#${peerID.takeLast(4)}" else nick
// Suffix is appended if ambiguous, UNLESS this is our own Peer ID
return if (isAmbiguous && peerID != myPeerID) "$nick#${peerID.takeLast(4)}" else nick
} }
/** /**
@@ -428,7 +433,9 @@ class PeerManager {
private fun cleanupStalePeers() { private fun cleanupStalePeers() {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val peersToRemove = peers.filterValues { (now - it.lastSeen) > stalePeerTimeoutMs } val peersToRemove = peers.filterValues {
it.id != myPeerID && (now - it.lastSeen) > stalePeerTimeoutMs
}
.keys .keys
.toList() .toList()