summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjack <jackjackbits@users.noreply.github.com>2025-08-12 11:03:31 +0200
committerjack <jackjackbits@users.noreply.github.com>2025-08-12 11:03:31 +0200
commitf4b8168ef953b1324c11411adec60271eeae67ec (patch)
tree6e08f96cdd5d12e60a9687479d299ad750e1623c
parent63f05b5d7ead1f04ebaea8f9035aaea245cfd6ed (diff)
Remove dead code and simplify codebase
- Remove unused BinaryEncodable protocol and BinaryMessageType enum - Delete MockNoiseSession.swift (never used in tests) - Remove all relay detection code (hardcoded to false) - Removed isRelayConnected property from BitchatPeer - Removed relayConnected case from ConnectionState enum - Cleaned up relay-related UI indicators in ContentView - Removed relay status checks from ChatViewModel - Simplified peer connection logic by removing relay layer Total: 169 lines removed across 5 files
-rw-r--r--bitchat/Models/BitchatPeer.swift48
-rw-r--r--bitchat/Protocols/BinaryEncodingUtils.swift15
-rw-r--r--bitchat/ViewModels/ChatViewModel.swift2
-rw-r--r--bitchat/Views/ContentView.swift14
-rw-r--r--bitchatTests/Mocks/MockNoiseSession.swift101
5 files changed, 11 insertions, 169 deletions
diff --git a/bitchat/Models/BitchatPeer.swift b/bitchat/Models/BitchatPeer.swift
index b8b9f21..566bc73 100644
--- a/bitchat/Models/BitchatPeer.swift
+++ b/bitchat/Models/BitchatPeer.swift
@@ -18,7 +18,6 @@ struct BitchatPeer: Identifiable, Equatable {
// Connection state
enum ConnectionState {
case bluetoothConnected
- case relayConnected // Connected via mesh relay (another peer)
case nostrAvailable // Mutual favorite, reachable via Nostr
case offline // Not connected via any transport
}
@@ -26,8 +25,6 @@ struct BitchatPeer: Identifiable, Equatable {
var connectionState: ConnectionState {
if isConnected {
return .bluetoothConnected
- } else if isRelayConnected {
- return .relayConnected
} else if favoriteStatus?.isMutual == true {
// Mutual favorites can communicate via Nostr when offline
return .nostrAvailable
@@ -36,8 +33,6 @@ struct BitchatPeer: Identifiable, Equatable {
}
}
- var isRelayConnected: Bool = false // Set by PeerManager based on session state
-
var isFavorite: Bool {
favoriteStatus?.isFavorite ?? false
}
@@ -59,8 +54,6 @@ struct BitchatPeer: Identifiable, Equatable {
switch connectionState {
case .bluetoothConnected:
return "📻" // Radio icon for mesh connection
- case .relayConnected:
- return "🔗" // Chain link for relay connection
case .nostrAvailable:
return "🌐" // Purple globe for Nostr
case .offline:
@@ -78,15 +71,13 @@ struct BitchatPeer: Identifiable, Equatable {
noisePublicKey: Data,
nickname: String,
lastSeen: Date = Date(),
- isConnected: Bool = false,
- isRelayConnected: Bool = false
+ isConnected: Bool = false
) {
self.id = id
self.noisePublicKey = noisePublicKey
self.nickname = nickname
self.lastSeen = lastSeen
self.isConnected = isConnected
- self.isRelayConnected = isRelayConnected
// Load favorite status - will be set later by the manager
self.favoriteStatus = nil
@@ -156,28 +147,15 @@ class PeerManager: ObservableObject {
continue
}
- // Check if this peer is actually connected (not just known via relay)
+ // Check if this peer is actually connected
let isConnected = meshService.isPeerConnected(peerID)
- let isKnown = meshService.isPeerKnown(peerID)
- // In a mesh network, a peer can only be relay-connected if:
- // 1. We know about them (have received announce)
- // 2. We're not directly connected
- // 3. There are other peers that could relay (mesh peer count > 2)
- // For now, disable relay detection until we have proper relay tracking
- let isRelayConnected = false
-
- // Debug logging for relay connection detection
- if isKnown && !isConnected {
- SecureLogger.log("Peer \(nickname) (\(peerID)): isConnected=\(isConnected), isKnown=\(isKnown), isRelayConnected=\(isRelayConnected)",
- category: SecureLogger.session, level: .debug)
- }
// Skip disconnected peers unless they're favorites (handled later)
- if !isConnected && !isRelayConnected {
+ if !isConnected {
continue
}
- if isConnected || isRelayConnected {
+ if isConnected {
connectedNicknames.insert(nickname)
}
@@ -188,8 +166,7 @@ class PeerManager: ObservableObject {
id: peerID,
noisePublicKey: noiseKey,
nickname: nickname,
- isConnected: isConnected,
- isRelayConnected: isRelayConnected
+ isConnected: isConnected
)
// Set favorite status - check both by current noise key and by nickname
if let favoriteStatus = favoritesService.getFavoriteStatus(for: noiseKey) {
@@ -211,14 +188,14 @@ class PeerManager: ObservableObject {
allPeers.append(peer)
}
- // Add offline favorites (only those not currently connected/relay-connected AND that we actively favorite)
+ // Add offline favorites (only those not currently connected AND that we actively favorite)
for (favoriteKey, favorite) in favoritesService.favorites {
let favoriteID = favorite.peerNoisePublicKey.hexEncodedString()
- // Skip if this peer is already connected or relay-connected (by nickname)
+ // Skip if this peer is already connected (by nickname)
if connectedNicknames.contains(favorite.peerNickname) {
- SecureLogger.log(" - Skipping '\(favorite.peerNickname)' (key: \(favoriteKey.hexEncodedString())) - already connected/relay-connected",
+ SecureLogger.log(" - Skipping '\(favorite.peerNickname)' (key: \(favoriteKey.hexEncodedString())) - already connected",
category: SecureLogger.session, level: .debug)
continue
}
@@ -259,16 +236,12 @@ class PeerManager: ObservableObject {
!(peer.displayName == "Unknown" && peer.favoriteStatus == nil)
}
- // Sort: Connected first (direct then relay), then favorites, then alphabetical
+ // Sort: Connected first, then favorites, then alphabetical
allPeers.sort { lhs, rhs in
// Direct connections first
if lhs.isConnected != rhs.isConnected {
return lhs.isConnected
}
- // Then relay connections
- if lhs.isRelayConnected != rhs.isRelayConnected {
- return lhs.isRelayConnected
- }
// Then favorites
if lhs.isFavorite != rhs.isFavorite {
return lhs.isFavorite
@@ -321,13 +294,10 @@ class PeerManager: ObservableObject {
// Log each peer's status
for peer in allPeers {
- // Use the actual statusIcon from the peer which accounts for relay connections
let statusIcon: String
switch peer.connectionState {
case .bluetoothConnected:
statusIcon = "🟢"
- case .relayConnected:
- statusIcon = "🔗"
case .nostrAvailable:
statusIcon = "🌐"
case .offline:
diff --git a/bitchat/Protocols/BinaryEncodingUtils.swift b/bitchat/Protocols/BinaryEncodingUtils.swift
index f6d4ca9..575d207 100644
--- a/bitchat/Protocols/BinaryEncodingUtils.swift
+++ b/bitchat/Protocols/BinaryEncodingUtils.swift
@@ -221,18 +221,3 @@ extension Data {
}
}
-// MARK: - Binary Message Protocol
-
-protocol BinaryEncodable {
- func toBinaryData() -> Data
- static func fromBinaryData(_ data: Data) -> Self?
-}
-
-// MARK: - Message Type Registry
-
-enum BinaryMessageType: UInt8 {
- case deliveryAck = 0x01
- case readReceipt = 0x02
- case noiseIdentityAnnouncement = 0x09
- case noiseMessage = 0x0A
-} \ No newline at end of file
diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift
index 0827639..0a216dc 100644
--- a/bitchat/ViewModels/ChatViewModel.swift
+++ b/bitchat/ViewModels/ChatViewModel.swift
@@ -859,7 +859,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
// Check if this is a moon peer (we favorite them but they don't favorite us) AND they're offline
// Only require mutual favorites for offline Nostr messaging
if let peer = peerIndex[peerID],
- peer.isFavorite && !peer.theyFavoritedUs && !peer.isConnected && !peer.isRelayConnected {
+ peer.isFavorite && !peer.theyFavoritedUs && !peer.isConnected {
addSystemMessage("cannot start chat with \(peerNickname): mutual favorite required for offline messaging.")
return
}
diff --git a/bitchat/Views/ContentView.swift b/bitchat/Views/ContentView.swift
index 58ec850..abe3309 100644
--- a/bitchat/Views/ContentView.swift
+++ b/bitchat/Views/ContentView.swift
@@ -680,12 +680,6 @@ struct ContentView: View {
.font(.system(size: 10))
.foregroundColor(textColor)
.accessibilityLabel("Connected via mesh")
- case .relayConnected:
- // Chain link for relay connection
- Image(systemName: "link")
- .font(.system(size: 10))
- .foregroundColor(Color.blue)
- .accessibilityLabel("Connected via relay")
case .nostrAvailable:
// Purple globe for mutual favorites reachable via Nostr
Image(systemName: "globe")
@@ -901,7 +895,7 @@ struct ContentView: View {
let peerCounts = viewModel.allPeers.reduce(into: (others: 0, mesh: 0)) { counts, peer in
guard peer.id != viewModel.meshService.myPeerID else { return }
- let isMeshConnected = peer.isConnected || peer.isRelayConnected
+ let isMeshConnected = peer.isConnected
if isMeshConnected {
counts.mesh += 1
counts.others += 1
@@ -993,12 +987,6 @@ struct ContentView: View {
.font(.system(size: 14))
.foregroundColor(textColor)
.accessibilityLabel("Connected via mesh")
- case .relayConnected:
- // Chain link for relay connection
- Image(systemName: "link")
- .font(.system(size: 14))
- .foregroundColor(Color.blue)
- .accessibilityLabel("Connected via relay")
case .nostrAvailable:
// Purple globe for Nostr
Image(systemName: "globe")
diff --git a/bitchatTests/Mocks/MockNoiseSession.swift b/bitchatTests/Mocks/MockNoiseSession.swift
deleted file mode 100644
index 1dc2811..0000000
--- a/bitchatTests/Mocks/MockNoiseSession.swift
+++ /dev/null
@@ -1,101 +0,0 @@
-//
-// MockNoiseSession.swift
-// bitchatTests
-//
-// This is free and unencumbered software released into the public domain.
-// For more information, see <https://unlicense.org>
-//
-
-import Foundation
-import CryptoKit
-@testable import bitchat
-
-class MockNoiseSession: NoiseSession {
- var mockState: NoiseSessionState = .uninitialized
- var shouldFailHandshake = false
- var shouldFailEncryption = false
- var handshakeMessages: [Data] = []
- var encryptedData: [Data] = []
- var decryptedData: [Data] = []
-
- override func getState() -> NoiseSessionState {
- return mockState
- }
-
- override func isEstablished() -> Bool {
- return mockState == .established
- }
-
- override func startHandshake() throws -> Data {
- if shouldFailHandshake {
- mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
- throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
- }
-
- mockState = .handshaking
- let handshakeData = TestHelpers.generateRandomData(length: 32)
- handshakeMessages.append(handshakeData)
- return handshakeData
- }
-
- override func processHandshakeMessage(_ message: Data) throws -> Data? {
- if shouldFailHandshake {
- mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
- throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
- }
-
- handshakeMessages.append(message)
-
- // Simulate handshake completion after 2 messages
- if handshakeMessages.count >= 2 {
- mockState = .established
- return nil
- } else {
- let response = TestHelpers.generateRandomData(length: 48)
- handshakeMessages.append(response)
- return response
- }
- }
-
- override func encrypt(_ plaintext: Data) throws -> Data {
- if shouldFailEncryption {
- throw NoiseSessionError.notEstablished
- }
-
- guard mockState == .established else {
- throw NoiseSessionError.notEstablished
- }
-
- // Simple mock encryption: prepend magic bytes and append the data
- var encrypted = Data([0xDE, 0xAD, 0xBE, 0xEF])
- encrypted.append(plaintext)
- encryptedData.append(encrypted)
- return encrypted
- }
-
- override func decrypt(_ ciphertext: Data) throws -> Data {
- if shouldFailEncryption {
- throw NoiseSessionError.notEstablished
- }
-
- guard mockState == .established else {
- throw NoiseSessionError.notEstablished
- }
-
- // Simple mock decryption: remove magic bytes
- guard ciphertext.count > 4 else {
- throw TestError.testFailure("Invalid ciphertext")
- }
-
- let plaintext = ciphertext.dropFirst(4)
- decryptedData.append(plaintext)
- return plaintext
- }
-
- override func reset() {
- mockState = .uninitialized
- handshakeMessages.removeAll()
- encryptedData.removeAll()
- decryptedData.removeAll()
- }
-} \ No newline at end of file