mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 21:45:19 +00:00
* Sign public broadcasts; verify relayed messages via persisted signing keys; keep scheduled relays in sparse graphs and speed their jitter; persist announce signing key for offline auth; add short backoff after disconnect errors to reduce reconnect thrash * Add connected vs reachable model: retain peers after link drop, expire after reachability window; expose all peers in snapshots; compute isReachable in UI; add meshReachable state and sorting; avoid removing peers on link events; notify UI on stale removals * ContentView: handle new .meshReachable connection state in header icon switch (exhaustive switch fix) * Logs: tag relayed announces as 'Reachable via mesh' and annotate public message logs with (direct|mesh) path for easier field analysis * Fix syntax error: remove stray else/log inserted into writeOrEnqueue; keep logs clean * UI: use 'point.3.connected.trianglepath.dotted' for mesh-reachable; change people count to include connected+reachable (exclude Nostr-only) * UI: switch to 'point.3.filled.connected.trianglepath.dotted' for mesh-reachable icons in list and header * Reachability: reduce retention to 21s for all peers (verified and unverified) to minimize stale presence * mesh DMs/acks: route to reachable peers; queue READ/DELIVERED until handshake; add Transport.isPeerReachable; UI: hide offline non-mutuals; DM header: better name fallback + show transport + encryption icons; fix NostrTransport conformance * Verification sheet: compute encryption status and fingerprint using short mesh ID mapping (fix 'not encrypted/handshake' for DMs with stable key) * Announce cadence: faster discovery (4s), sparse 15±4s, dense 30±8s; initial 0.6s; post-subscribe 50ms; min-force 150ms; maintenance 5s; proactive announces on handshake + recent-traffic nudge * Relay: increase broadcast TTL cap in sparse graphs to 6; tighten jitter for handshake (10–35ms) and directed (20–60ms) relays * Range/robustness: store-and-forward for directed packets (15s) with flush on new links + periodic; announces: no subset + afterglow re-announce on first-seen; adaptive scanning: force ON when <=2 neighbors or recent traffic * Fix warnings: remove unused msgID and unused mutable var in directed spool flush * Announces: TTL 7 (sparse only) via RelayController; no fanout subset for announces; neighbor-change rebroadcast of last 2–3 announces. Fragments: faster pacing (5ms global, 4ms directed). * Peer list: real-time icon updates by publishing snapshots on connectivity checks; add unread message indicator (envelope) next to peers with unread DMs * UI: unread envelope uses orange; hasUnreadMessages checks Nostr conv key for peers with known Nostr pubkeys (geohash DM consistency) * Logs/robustness: debounce disconnect notifications (1.5s), debounce 'reconnected' logs (2s), add weak-link cooldown after timeouts on very weak RSSI (<= -90) * Peer icons: faster, accurate reachability\n\n- Run connectivity checks every maintenance tick (5s)\n- Publish peer snapshots on central unsubscribe for instant UI refresh\n- Lower inactivity timeout to 8s and disconnect debounce to 0.9s\n- Gate reachability on mesh-attached (>=1 direct link); no links => no reachable peers\n- Keep 21s retention for verified/unverified, but only when attached to mesh\n\nImproves list responsiveness when walking out of range and prevents stale 'reachable' states when isolated. --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
170 lines
8.2 KiB
Swift
170 lines
8.2 KiB
Swift
import SwiftUI
|
|
|
|
struct MeshPeerList: View {
|
|
@ObservedObject var viewModel: ChatViewModel
|
|
let textColor: Color
|
|
let secondaryTextColor: Color
|
|
let onTapPeer: (String) -> Void
|
|
let onToggleFavorite: (String) -> Void
|
|
let onShowFingerprint: (String) -> Void
|
|
@Environment(\.colorScheme) var colorScheme
|
|
|
|
@State private var orderedIDs: [String] = []
|
|
|
|
var body: some View {
|
|
if viewModel.allPeers.isEmpty {
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
Text("nobody around...")
|
|
.font(.system(size: 14, design: .monospaced))
|
|
.foregroundColor(secondaryTextColor)
|
|
.padding(.horizontal)
|
|
.padding(.top, 12)
|
|
}
|
|
} else {
|
|
let myPeerID = viewModel.meshService.myPeerID
|
|
let mapped: [(peer: BitchatPeer, isMe: Bool, hasUnread: Bool, enc: EncryptionStatus)] = viewModel.allPeers.map { peer in
|
|
let isMe = peer.id == myPeerID
|
|
let hasUnread = viewModel.hasUnreadMessages(for: peer.id)
|
|
let enc = viewModel.getEncryptionStatus(for: peer.id)
|
|
return (peer, isMe, hasUnread, enc)
|
|
}
|
|
// Stable visual order without mutating state here
|
|
let currentIDs = mapped.map { $0.peer.id }
|
|
let displayIDs = orderedIDs.filter { currentIDs.contains($0) } + currentIDs.filter { !orderedIDs.contains($0) }
|
|
let peers: [(peer: BitchatPeer, isMe: Bool, hasUnread: Bool, enc: EncryptionStatus)] = displayIDs.compactMap { id in
|
|
mapped.first(where: { $0.peer.id == id })
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 0) {
|
|
ForEach(0..<peers.count, id: \.self) { idx in
|
|
let item = peers[idx]
|
|
let peer = item.peer
|
|
let isMe = item.isMe
|
|
HStack(spacing: 4) {
|
|
let assigned = viewModel.colorForMeshPeer(id: peer.id, isDark: colorScheme == .dark)
|
|
let baseColor = isMe ? Color.orange : assigned
|
|
if isMe {
|
|
Image(systemName: "person.fill")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
} else if peer.isConnected {
|
|
// Mesh-connected peer: radio icon
|
|
Image(systemName: "antenna.radiowaves.left.and.right")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
} else if peer.isReachable {
|
|
// Mesh-reachable (relayed): point.3 icon
|
|
Image(systemName: "point.3.filled.connected.trianglepath.dotted")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
} else if peer.isMutualFavorite {
|
|
// Mutual favorite reachable via Nostr: globe icon (purple)
|
|
Image(systemName: "globe")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(.purple)
|
|
} else {
|
|
// Fallback icon for others (dimmed)
|
|
Image(systemName: "person")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(secondaryTextColor)
|
|
}
|
|
|
|
let displayName = isMe ? viewModel.nickname : peer.nickname
|
|
let (base, suffix) = splitSuffix(from: displayName)
|
|
HStack(spacing: 0) {
|
|
Text(base)
|
|
.font(.system(size: 14, design: .monospaced))
|
|
.foregroundColor(baseColor)
|
|
if !suffix.isEmpty {
|
|
let suffixColor = isMe ? Color.orange.opacity(0.6) : baseColor.opacity(0.6)
|
|
Text(suffix)
|
|
.font(.system(size: 14, design: .monospaced))
|
|
.foregroundColor(suffixColor)
|
|
}
|
|
}
|
|
|
|
if !isMe, viewModel.isPeerBlocked(peer.id) {
|
|
Image(systemName: "nosign")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(.red)
|
|
.help("Blocked")
|
|
}
|
|
|
|
if !isMe {
|
|
if peer.isConnected {
|
|
if let icon = item.enc.icon {
|
|
Image(systemName: icon)
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
}
|
|
} else {
|
|
// Offline: prefer showing verified badge from persisted fingerprints
|
|
if let fp = viewModel.getFingerprint(for: peer.id),
|
|
viewModel.verifiedFingerprints.contains(fp) {
|
|
Image(systemName: "checkmark.seal.fill")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
} else if let icon = item.enc.icon {
|
|
// Fallback to whatever status says (likely lock if we had a past session)
|
|
Image(systemName: icon)
|
|
.font(.system(size: 10))
|
|
.foregroundColor(baseColor)
|
|
}
|
|
}
|
|
}
|
|
|
|
Spacer()
|
|
|
|
// Unread message indicator for this peer
|
|
if !isMe, item.hasUnread {
|
|
Image(systemName: "envelope.fill")
|
|
.font(.system(size: 10))
|
|
.foregroundColor(.orange)
|
|
.help("New messages")
|
|
}
|
|
|
|
if !isMe {
|
|
Button(action: { onToggleFavorite(peer.id) }) {
|
|
Image(systemName: (peer.favoriteStatus?.isFavorite ?? false) ? "star.fill" : "star")
|
|
.font(.system(size: 12))
|
|
.foregroundColor((peer.favoriteStatus?.isFavorite ?? false) ? .yellow : secondaryTextColor)
|
|
}
|
|
.buttonStyle(.plain)
|
|
}
|
|
}
|
|
.padding(.horizontal)
|
|
.padding(.vertical, 4)
|
|
.padding(.top, idx == 0 ? 10 : 0)
|
|
.contentShape(Rectangle())
|
|
.onTapGesture { if !isMe { onTapPeer(peer.id) } }
|
|
.onTapGesture(count: 2) { if !isMe { onShowFingerprint(peer.id) } }
|
|
}
|
|
}
|
|
// Seed and update order outside result builder
|
|
.onAppear {
|
|
let currentIDs = mapped.map { $0.peer.id }
|
|
orderedIDs = currentIDs
|
|
}
|
|
.onChange(of: mapped.map { $0.peer.id }) { ids in
|
|
var newOrder = orderedIDs
|
|
newOrder.removeAll { !ids.contains($0) }
|
|
for id in ids where !newOrder.contains(id) { newOrder.append(id) }
|
|
if newOrder != orderedIDs { orderedIDs = newOrder }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper to split a trailing #abcd suffix
|
|
private func splitSuffix(from name: String) -> (String, String) {
|
|
guard name.count >= 5 else { return (name, "") }
|
|
let suffix = String(name.suffix(5))
|
|
if suffix.first == "#", suffix.dropFirst().allSatisfy({ c in
|
|
("0"..."9").contains(String(c)) || ("a"..."f").contains(String(c)) || ("A"..."F").contains(String(c))
|
|
}) {
|
|
let base = String(name.dropLast(5))
|
|
return (base, suffix)
|
|
}
|
|
return (name, "")
|
|
}
|