mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 14:45:21 +00:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed105f311a | ||
|
|
8ebb7411a7 | ||
|
|
fb60c929eb | ||
|
|
678795adcf | ||
|
|
37e973bf9f | ||
|
|
8c002b8942 | ||
|
|
6533629362 | ||
|
|
dd4e2f6e86 | ||
|
|
55187ac5c6 |
@@ -56,6 +56,12 @@
|
||||
049BD3B32E51F319001A566B /* MessageRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049BD3B02E51F319001A566B /* MessageRouter.swift */; };
|
||||
049BD3B42E51F319001A566B /* NostrTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049BD3B12E51F319001A566B /* NostrTransport.swift */; };
|
||||
049BD3B52E51F319001A566B /* MessageRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 049BD3B02E51F319001A566B /* MessageRouter.swift */; };
|
||||
ABCD00022E5CCCC300162C4A /* ReadReceiptTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00012E5CCCC300162C4A /* ReadReceiptTracker.swift */; };
|
||||
ABCD00032E5CCCC300162C4A /* ReadReceiptTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00012E5CCCC300162C4A /* ReadReceiptTracker.swift */; };
|
||||
ABCD00052E5CCCC300162C4C /* NostrInboxService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00042E5CCCC300162C4C /* NostrInboxService.swift */; };
|
||||
ABCD00062E5CCCC300162C4D /* NostrInboxService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00042E5CCCC300162C4C /* NostrInboxService.swift */; };
|
||||
ABCD00082E5CCCC300162C4E /* NostrOutboxService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00072E5CCCC300162C4E /* NostrOutboxService.swift */; };
|
||||
ABCD00092E5CCCC300162C4F /* NostrOutboxService.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABCD00072E5CCCC300162C4E /* NostrOutboxService.swift */; };
|
||||
0AE840940F21AFC07C226636 /* PrivateChatE2ETests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A262EDDC04B7D7B5E31F321 /* PrivateChatE2ETests.swift */; };
|
||||
0B6F25559A21F8C69C8357C6 /* BinaryProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B3CC6FA298729906109F61B /* BinaryProtocolTests.swift */; };
|
||||
10E68BB889356219189E38EC /* BitchatApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF625BB3AD919322C01A46B2 /* BitchatApp.swift */; };
|
||||
@@ -213,6 +219,9 @@
|
||||
049BD39E2E51DBF4001A566B /* Packets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Packets.swift; sourceTree = "<group>"; };
|
||||
049BD39F2E51DBF4001A566B /* PeerID.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerID.swift; sourceTree = "<group>"; };
|
||||
049BD3A42E51DC0E001A566B /* MessageDeduplicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageDeduplicator.swift; sourceTree = "<group>"; };
|
||||
ABCD00012E5CCCC300162C4A /* ReadReceiptTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadReceiptTracker.swift; sourceTree = "<group>"; };
|
||||
ABCD00042E5CCCC300162C4C /* NostrInboxService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrInboxService.swift; sourceTree = "<group>"; };
|
||||
ABCD00072E5CCCC300162C4E /* NostrOutboxService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrOutboxService.swift; sourceTree = "<group>"; };
|
||||
049BD3AA2E51E38E001A566B /* PeerIDResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerIDResolver.swift; sourceTree = "<group>"; };
|
||||
049BD3AD2E51ED60001A566B /* Transport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transport.swift; sourceTree = "<group>"; };
|
||||
049BD3B02E51F319001A566B /* MessageRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageRouter.swift; sourceTree = "<group>"; };
|
||||
@@ -520,6 +529,9 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
048A4BE62E5CCCC300162C4A /* TransportConfig.swift */,
|
||||
ABCD00012E5CCCC300162C4A /* ReadReceiptTracker.swift */,
|
||||
ABCD00042E5CCCC300162C4C /* NostrInboxService.swift */,
|
||||
ABCD00072E5CCCC300162C4E /* NostrOutboxService.swift */,
|
||||
AA77BB10CC22DD33EE44FF55 /* VerificationService.swift */,
|
||||
047502B82E560F690083520F /* RelayController.swift */,
|
||||
0475028B2E54171C0083520F /* LocationChannelManager.swift */,
|
||||
@@ -742,8 +754,11 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
048A4BE72E5CCCC300162C4A /* TransportConfig.swift in Sources */,
|
||||
1234567890ABCDEFFEDCBA13 /* PeerDisplayNameResolver.swift in Sources */,
|
||||
048A4BE72E5CCCC300162C4A /* TransportConfig.swift in Sources */,
|
||||
ABCD00022E5CCCC300162C4A /* ReadReceiptTracker.swift in Sources */,
|
||||
ABCD00052E5CCCC300162C4C /* NostrInboxService.swift in Sources */,
|
||||
ABCD00082E5CCCC300162C4E /* NostrOutboxService.swift in Sources */,
|
||||
1234567890ABCDEFFEDCBA13 /* PeerDisplayNameResolver.swift in Sources */,
|
||||
AA77BB12CC22DD33EE44FF56 /* VerificationService.swift in Sources */,
|
||||
AA77BB15CC22DD33EE44FF59 /* VerificationViews.swift in Sources */,
|
||||
A1B2C3D54E5F60718293A4B6 /* XChaCha20Poly1305Compat.swift in Sources */,
|
||||
@@ -801,8 +816,11 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
048A4BE82E5CCCC300162C4A /* TransportConfig.swift in Sources */,
|
||||
1234567890ABCDEFFEDCBA14 /* PeerDisplayNameResolver.swift in Sources */,
|
||||
048A4BE82E5CCCC300162C4A /* TransportConfig.swift in Sources */,
|
||||
ABCD00032E5CCCC300162C4A /* ReadReceiptTracker.swift in Sources */,
|
||||
ABCD00062E5CCCC300162C4D /* NostrInboxService.swift in Sources */,
|
||||
ABCD00092E5CCCC300162C4F /* NostrOutboxService.swift in Sources */,
|
||||
1234567890ABCDEFFEDCBA14 /* PeerDisplayNameResolver.swift in Sources */,
|
||||
AA77BB11CC22DD33EE44FF55 /* VerificationService.swift in Sources */,
|
||||
AA77BB14CC22DD33EE44FF58 /* VerificationViews.swift in Sources */,
|
||||
A1B2C3D44E5F60718293A4B5 /* XChaCha20Poly1305Compat.swift in Sources */,
|
||||
|
||||
@@ -310,7 +310,7 @@ class SecureIdentityStateManager {
|
||||
func getCryptoIdentitiesByPeerIDPrefix(_ peerID: String) -> [CryptographicIdentity] {
|
||||
queue.sync {
|
||||
// Defensive: ensure hex and correct length
|
||||
guard peerID.count == 16, peerID.allSatisfy({ $0.isHexDigit }) else { return [] }
|
||||
guard PeerIDResolver.isShortID(peerID) else { return [] }
|
||||
return cryptographicIdentities.values.filter { $0.fingerprint.hasPrefix(peerID) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
|
||||
// MARK: - Hex Encoding/Decoding
|
||||
|
||||
@@ -35,6 +36,16 @@ extension Data {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fingerprint Helpers
|
||||
|
||||
extension Data {
|
||||
/// SHA-256 over data, hex-encoded (lowercase)
|
||||
func sha256Fingerprint() -> String {
|
||||
let hash = SHA256.hash(data: self)
|
||||
return hash.map { String(format: "%02x", $0) }.joined()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Binary Encoding Utilities
|
||||
|
||||
extension Data {
|
||||
@@ -220,4 +231,3 @@ extension Data {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -512,7 +512,7 @@ final class BLEService: NSObject {
|
||||
func isPeerConnected(_ peerID: String) -> Bool {
|
||||
// Accept both 16-hex short IDs and 64-hex Noise keys
|
||||
let shortID: String = {
|
||||
if peerID.count == 64, let key = Data(hexString: peerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let key = Data(hexString: peerID) {
|
||||
return PeerIDUtils.derivePeerID(fromPublicKey: key)
|
||||
}
|
||||
return peerID
|
||||
@@ -523,7 +523,7 @@ final class BLEService: NSObject {
|
||||
func isPeerReachable(_ peerID: String) -> Bool {
|
||||
// Accept both 16-hex short IDs and 64-hex Noise keys
|
||||
let shortID: String = {
|
||||
if peerID.count == 64, let key = Data(hexString: peerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let key = Data(hexString: peerID) {
|
||||
return PeerIDUtils.derivePeerID(fromPublicKey: key)
|
||||
}
|
||||
return peerID
|
||||
@@ -664,8 +664,7 @@ final class BLEService: NSObject {
|
||||
return collectionsQueue.sync {
|
||||
if let publicKey = peers[peerID]?.noisePublicKey {
|
||||
// Use the same fingerprinting method as NoiseEncryptionService/UnifiedPeerService (SHA-256 of raw key)
|
||||
let hash = SHA256.hash(data: publicKey)
|
||||
return hash.map { String(format: "%02x", $0) }.joined()
|
||||
return publicKey.sha256Fingerprint()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ class FavoritesPersistenceService: ObservableObject {
|
||||
/// Falls back to scanning favorites and matching on derived peer ID.
|
||||
func getFavoriteStatus(forPeerID peerID: String) -> FavoriteRelationship? {
|
||||
// Quick sanity: peerID should be 16 hex chars (8 bytes)
|
||||
guard peerID.count == 16 else { return nil }
|
||||
guard PeerIDResolver.isShortID(peerID) else { return nil }
|
||||
for (pubkey, rel) in favorites {
|
||||
let derived = PeerIDUtils.derivePeerID(fromPublicKey: pubkey)
|
||||
if derived == peerID { return rel }
|
||||
|
||||
@@ -4,13 +4,12 @@ import Foundation
|
||||
@MainActor
|
||||
final class MessageRouter {
|
||||
private let mesh: Transport
|
||||
private let nostr: NostrTransport
|
||||
private let nostr: NostrOutboxService
|
||||
private var outbox: [String: [(content: String, nickname: String, messageID: String)]] = [:] // peerID -> queued messages
|
||||
|
||||
init(mesh: Transport, nostr: NostrTransport) {
|
||||
init(mesh: Transport, nostr: NostrOutboxService) {
|
||||
self.mesh = mesh
|
||||
self.nostr = nostr
|
||||
self.nostr.senderPeerID = mesh.myPeerID
|
||||
|
||||
// Observe favorites changes to learn Nostr mapping and flush queued messages
|
||||
NotificationCenter.default.addObserver(
|
||||
@@ -46,7 +45,7 @@ final class MessageRouter {
|
||||
} else if canSendViaNostr(peerID: peerID) {
|
||||
SecureLogger.log("Routing PM via Nostr to \(peerID.prefix(8))… id=\(messageID.prefix(8))…",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
nostr.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
|
||||
nostr.sendPrivate(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
|
||||
} else {
|
||||
// Queue for later (when mesh connects or Nostr mapping appears)
|
||||
if outbox[peerID] == nil { outbox[peerID] = [] }
|
||||
|
||||
@@ -548,8 +548,7 @@ class NoiseEncryptionService {
|
||||
}
|
||||
|
||||
private func calculateFingerprint(for publicKey: Curve25519.KeyAgreement.PublicKey) -> String {
|
||||
let hash = SHA256.hash(data: publicKey.rawRepresentation)
|
||||
return hash.map { String(format: "%02x", $0) }.joined()
|
||||
return publicKey.rawRepresentation.sha256Fingerprint()
|
||||
}
|
||||
|
||||
// MARK: - Session Maintenance
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//
|
||||
// NostrInboxService.swift
|
||||
// bitchat
|
||||
//
|
||||
// Centralizes Nostr subscribe/unsubscribe and event de-duplication for inbox flows.
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
final class NostrInboxService {
|
||||
private var processedIDs: Set<String> = []
|
||||
private var order: [String] = []
|
||||
private let cap: Int
|
||||
|
||||
init(capacity: Int = TransportConfig.uiProcessedNostrEventsCap) {
|
||||
self.cap = capacity
|
||||
}
|
||||
|
||||
private func track(_ id: String) -> Bool {
|
||||
if processedIDs.contains(id) { return false }
|
||||
processedIDs.insert(id)
|
||||
order.append(id)
|
||||
if order.count > cap {
|
||||
let overflow = order.count - cap
|
||||
for _ in 0..<overflow {
|
||||
if let oldest = order.first {
|
||||
order.removeFirst()
|
||||
processedIDs.remove(oldest)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func reset() {
|
||||
processedIDs.removeAll()
|
||||
order.removeAll()
|
||||
}
|
||||
|
||||
// Subscribe to public geohash ephemeral events (kind 20000)
|
||||
@discardableResult
|
||||
func subscribeGeohashEphemeral(
|
||||
geohash: String,
|
||||
lookbackSeconds: TimeInterval,
|
||||
limit: Int,
|
||||
relayCount: Int,
|
||||
subID: String? = nil,
|
||||
handler: @escaping (NostrEvent) -> Void
|
||||
) -> String {
|
||||
let id = subID ?? "geo-\(geohash)"
|
||||
let filter = NostrFilter.geohashEphemeral(
|
||||
geohash,
|
||||
since: Date().addingTimeInterval(-lookbackSeconds),
|
||||
limit: limit
|
||||
)
|
||||
let relays = GeoRelayDirectory.shared.closestRelays(toGeohash: geohash, count: relayCount)
|
||||
NostrRelayManager.shared.subscribe(filter: filter, id: id, relayUrls: relays) { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
|
||||
guard self.track(event.id) else { return }
|
||||
handler(event)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Subscribe to gift wraps (DMs) for a given pubkey
|
||||
@discardableResult
|
||||
func subscribeGiftWrapsFor(
|
||||
pubkeyHex: String,
|
||||
lookbackSeconds: TimeInterval,
|
||||
subID: String? = nil,
|
||||
handler: @escaping (NostrEvent) -> Void
|
||||
) -> String {
|
||||
let id = subID ?? "dm-\(pubkeyHex.prefix(8))"
|
||||
let filter = NostrFilter.giftWrapsFor(pubkey: pubkeyHex, since: Date().addingTimeInterval(-lookbackSeconds))
|
||||
NostrRelayManager.shared.subscribe(filter: filter, id: id) { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard self.track(event.id) else { return }
|
||||
handler(event)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func unsubscribe(id: String) {
|
||||
NostrRelayManager.shared.unsubscribe(id: id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// NostrOutboxService.swift
|
||||
// bitchat
|
||||
//
|
||||
// Centralized Nostr outbox for sending PMs and ACKs.
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@MainActor
|
||||
final class NostrOutboxService {
|
||||
private let transport: NostrTransport
|
||||
|
||||
init(meshService: Transport) {
|
||||
let t = NostrTransport()
|
||||
t.senderPeerID = meshService.myPeerID
|
||||
self.transport = t
|
||||
}
|
||||
|
||||
func sendGeohashPM(content: String, toRecipientHex recipientHex: String, from identity: NostrIdentity, messageID: String) {
|
||||
transport.sendPrivateMessageGeohash(content: content, toRecipientHex: recipientHex, from: identity, messageID: messageID)
|
||||
}
|
||||
|
||||
func sendGeohashDeliveredAck(messageID: String, toRecipientHex recipientHex: String, from identity: NostrIdentity) {
|
||||
transport.sendDeliveryAckGeohash(for: messageID, toRecipientHex: recipientHex, from: identity)
|
||||
}
|
||||
|
||||
func sendGeohashReadAck(messageID: String, toRecipientHex recipientHex: String, from identity: NostrIdentity) {
|
||||
transport.sendReadReceiptGeohash(messageID, toRecipientHex: recipientHex, from: identity)
|
||||
}
|
||||
|
||||
// MARK: - Generic outbox via favorites mapping
|
||||
func sendPrivate(_ content: String, to peerID: String, recipientNickname: String, messageID: String) {
|
||||
transport.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
|
||||
}
|
||||
|
||||
func sendReadReceipt(_ receipt: ReadReceipt, to peerID: String) {
|
||||
transport.sendReadReceipt(receipt, to: peerID)
|
||||
}
|
||||
|
||||
func sendDeliveryAck(for messageID: String, to peerID: String) {
|
||||
transport.sendDeliveryAck(for: messageID, to: peerID)
|
||||
}
|
||||
|
||||
func sendFavoriteNotification(to peerID: String, isFavorite: Bool) {
|
||||
transport.sendFavoriteNotification(to: peerID, isFavorite: isFavorite)
|
||||
}
|
||||
}
|
||||
@@ -167,7 +167,7 @@ final class NostrTransport: Transport {
|
||||
let npub = fav.peerNostrPublicKey {
|
||||
return npub
|
||||
}
|
||||
if peerID.count == 16,
|
||||
if PeerIDResolver.isShortID(peerID),
|
||||
let fav = FavoritesPersistenceService.shared.getFavoriteStatus(forPeerID: peerID),
|
||||
let npub = fav.peerNostrPublicKey {
|
||||
return npub
|
||||
|
||||
@@ -16,14 +16,15 @@ class PrivateChatManager: ObservableObject {
|
||||
@Published var unreadMessages: Set<String> = []
|
||||
|
||||
private var selectedPeerFingerprint: String? = nil
|
||||
var sentReadReceipts: Set<String> = [] // Made accessible for ChatViewModel
|
||||
private var readReceiptTracker: ReadReceiptTracker?
|
||||
|
||||
weak var meshService: Transport?
|
||||
// Route acks/receipts via MessageRouter (chooses mesh or Nostr)
|
||||
weak var messageRouter: MessageRouter?
|
||||
|
||||
init(meshService: Transport? = nil) {
|
||||
init(meshService: Transport? = nil, readReceiptTracker: ReadReceiptTracker? = nil) {
|
||||
self.meshService = meshService
|
||||
self.readReceiptTracker = readReceiptTracker
|
||||
}
|
||||
|
||||
// Cap for messages stored per private chat
|
||||
@@ -121,7 +122,7 @@ class PrivateChatManager: ObservableObject {
|
||||
unreadMessages.insert(senderPeerID)
|
||||
|
||||
// Avoid notifying for messages already marked as read (dup/resubscribe cases)
|
||||
if !sentReadReceipts.contains(message.id) {
|
||||
if !(readReceiptTracker?.contains(message.id) ?? false) {
|
||||
NotificationService.shared.sendPrivateMessageNotification(
|
||||
from: message.sender,
|
||||
message: message.content,
|
||||
@@ -160,7 +161,7 @@ class PrivateChatManager: ObservableObject {
|
||||
// Send read receipts for unread messages that haven't been sent yet
|
||||
if let messages = privateChats[peerID] {
|
||||
for message in messages {
|
||||
if message.senderPeerID == peerID && !message.isRelay && !sentReadReceipts.contains(message.id) {
|
||||
if message.senderPeerID == peerID && !message.isRelay && !(readReceiptTracker?.contains(message.id) ?? false) {
|
||||
sendReadReceipt(for: message)
|
||||
}
|
||||
}
|
||||
@@ -212,12 +213,12 @@ class PrivateChatManager: ObservableObject {
|
||||
// MARK: - Private Methods
|
||||
|
||||
private func sendReadReceipt(for message: BitchatMessage) {
|
||||
guard !sentReadReceipts.contains(message.id),
|
||||
guard !(readReceiptTracker?.contains(message.id) ?? false),
|
||||
let senderPeerID = message.senderPeerID else {
|
||||
return
|
||||
}
|
||||
|
||||
sentReadReceipts.insert(message.id)
|
||||
readReceiptTracker?.insert(message.id)
|
||||
|
||||
// Create read receipt using the simplified method
|
||||
let receipt = ReadReceipt(
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
//
|
||||
// ReadReceiptTracker.swift
|
||||
// bitchat
|
||||
//
|
||||
// Centralized tracker for sent read receipts with simple persistence.
|
||||
// This is free and unencumbered software released into the public domain.
|
||||
// For more information, see <https://unlicense.org>
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
final class ReadReceiptTracker {
|
||||
private let defaults: UserDefaults
|
||||
private let key = "sentReadReceipts"
|
||||
private let queue = DispatchQueue(label: "chat.bitchat.readreceipts", attributes: .concurrent)
|
||||
private var set: Set<String> = []
|
||||
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
if let data = defaults.data(forKey: key),
|
||||
let arr = try? JSONDecoder().decode([String].self, from: data) {
|
||||
self.set = Set(arr)
|
||||
}
|
||||
}
|
||||
|
||||
func contains(_ id: String) -> Bool {
|
||||
queue.sync { set.contains(id) }
|
||||
}
|
||||
|
||||
func insert(_ id: String) {
|
||||
queue.async(flags: .barrier) {
|
||||
if self.set.insert(id).inserted { self.persist() }
|
||||
}
|
||||
}
|
||||
|
||||
func insert<S: Sequence>(_ ids: S) where S.Element == String {
|
||||
queue.async(flags: .barrier) {
|
||||
var changed = false
|
||||
for id in ids { changed = self.set.insert(id).inserted || changed }
|
||||
if changed { self.persist() }
|
||||
}
|
||||
}
|
||||
|
||||
func remove(_ id: String) {
|
||||
queue.async(flags: .barrier) {
|
||||
if self.set.remove(id) != nil { self.persist() }
|
||||
}
|
||||
}
|
||||
|
||||
func removeAll() {
|
||||
queue.async(flags: .barrier) {
|
||||
if !self.set.isEmpty { self.set.removeAll(); self.persist() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep only IDs present in the allow-list; useful for pruning stale entries.
|
||||
func prune(toAllowedIDs allowed: Set<String>) {
|
||||
queue.async(flags: .barrier) {
|
||||
let newSet = self.set.intersection(allowed)
|
||||
if newSet.count != self.set.count { self.set = newSet; self.persist() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshot current set for read-only operations (avoid long-lived copies in hot paths)
|
||||
func snapshot() -> Set<String> { queue.sync { set } }
|
||||
|
||||
private func persist() {
|
||||
let arr = Array(set)
|
||||
if let data = try? JSONEncoder().encode(arr) {
|
||||
defaults.set(data, forKey: key)
|
||||
} else {
|
||||
SecureLogger.log("❌ Failed to encode read receipts for persistence",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,10 +412,4 @@ class UnifiedPeerService: ObservableObject, TransportPeerEventsDelegate {
|
||||
|
||||
// MARK: - Helper Extensions
|
||||
|
||||
extension Data {
|
||||
func sha256Fingerprint() -> String {
|
||||
// Implementation matches existing fingerprint generation in NoiseEncryptionService
|
||||
let hash = SHA256.hash(data: self)
|
||||
return hash.map { String(format: "%02x", $0) }.joined()
|
||||
}
|
||||
}
|
||||
// Moved sha256Fingerprint() to BinaryEncodingUtils for reuse
|
||||
|
||||
@@ -161,7 +161,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}()
|
||||
let full = (nostrKeyMapping[spid] ?? bare).lowercased()
|
||||
return "nostr:" + full
|
||||
} else if spid.count == 16, let full = getNoiseKeyForShortID(spid)?.lowercased() {
|
||||
} else if PeerIDResolver.isShortID(spid), let full = getNoiseKeyForShortID(spid)?.lowercased() {
|
||||
return "noise:" + full
|
||||
} else {
|
||||
return "mesh:" + spid.lowercased()
|
||||
@@ -245,6 +245,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
private let commandProcessor: CommandProcessor
|
||||
private let messageRouter: MessageRouter
|
||||
private let privateChatManager: PrivateChatManager
|
||||
private let readReceiptTracker: ReadReceiptTracker
|
||||
private let unifiedPeerService: UnifiedPeerService
|
||||
private let autocompleteService: AutocompleteService
|
||||
|
||||
@@ -308,7 +309,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
func getNoiseKeyForShortID(_ shortPeerID: String) -> String? {
|
||||
if let mapped = shortIDToNoiseKey[shortPeerID] { return mapped }
|
||||
// Fallback: derive from active Noise session if available
|
||||
if shortPeerID.count == 16,
|
||||
if PeerIDResolver.isShortID(shortPeerID),
|
||||
let key = meshService.getNoiseService().getPeerPublicKeyData(shortPeerID) {
|
||||
let stable = key.hexEncodedString()
|
||||
shortIDToNoiseKey[shortPeerID] = stable
|
||||
@@ -347,9 +348,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
var meshService: Transport = BLEService()
|
||||
private var nostrRelayManager: NostrRelayManager?
|
||||
// PeerManager replaced by UnifiedPeerService
|
||||
private var processedNostrEvents = Set<String>() // Simple deduplication
|
||||
private var processedNostrEventOrder: [String] = []
|
||||
private let maxProcessedNostrEvents = TransportConfig.uiProcessedNostrEventsCap
|
||||
// NostrInboxService handles event de-duplication
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private let nicknameKey = "bitchat.nickname"
|
||||
// Location channel state (macOS supports manual geohash selection)
|
||||
@@ -358,6 +357,8 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
private var geoDmSubscriptionID: String? = nil
|
||||
private var currentGeohash: String? = nil
|
||||
private var geoNicknames: [String: String] = [:] // pubkeyHex(lowercased) -> nickname
|
||||
private var nostrInbox: NostrInboxService
|
||||
private var nostrOutbox: NostrOutboxService
|
||||
|
||||
// MARK: - Caches
|
||||
|
||||
@@ -435,22 +436,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
@Published private(set) var isBatchingPublic: Bool = false
|
||||
private let lateInsertThreshold: TimeInterval = TransportConfig.uiLateInsertThreshold
|
||||
|
||||
// Track sent read receipts to avoid duplicates (persisted across launches)
|
||||
// Note: Persistence happens automatically in didSet, no lifecycle observers needed
|
||||
private var sentReadReceipts: Set<String> = [] { // messageID set
|
||||
didSet {
|
||||
// Only persist if there are changes
|
||||
guard oldValue != sentReadReceipts else { return }
|
||||
|
||||
// Persist to UserDefaults whenever it changes (no manual synchronize/verify re-read)
|
||||
if let data = try? JSONEncoder().encode(Array(sentReadReceipts)) {
|
||||
UserDefaults.standard.set(data, forKey: "sentReadReceipts")
|
||||
} else {
|
||||
SecureLogger.log("❌ Failed to encode read receipts for persistence",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Read receipts are centralized in ReadReceiptTracker
|
||||
|
||||
// Throttle verification response toasts per peer to avoid spam
|
||||
private var lastVerifyToastAt: [String: Date] = [:]
|
||||
@@ -470,21 +456,15 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
@MainActor
|
||||
init() {
|
||||
// Load persisted read receipts
|
||||
if let data = UserDefaults.standard.data(forKey: "sentReadReceipts"),
|
||||
let receipts = try? JSONDecoder().decode([String].self, from: data) {
|
||||
self.sentReadReceipts = Set(receipts)
|
||||
// Successfully loaded read receipts
|
||||
} else {
|
||||
// No persisted read receipts found
|
||||
}
|
||||
|
||||
// Initialize read receipt tracker first
|
||||
self.readReceiptTracker = ReadReceiptTracker()
|
||||
// Initialize services
|
||||
self.commandProcessor = CommandProcessor()
|
||||
self.privateChatManager = PrivateChatManager(meshService: meshService)
|
||||
self.privateChatManager = PrivateChatManager(meshService: meshService, readReceiptTracker: readReceiptTracker)
|
||||
self.unifiedPeerService = UnifiedPeerService(meshService: meshService)
|
||||
let nostrTransport = NostrTransport()
|
||||
self.messageRouter = MessageRouter(mesh: meshService, nostr: nostrTransport)
|
||||
self.nostrInbox = NostrInboxService()
|
||||
self.nostrOutbox = NostrOutboxService(meshService: meshService)
|
||||
self.messageRouter = MessageRouter(mesh: meshService, nostr: self.nostrOutbox)
|
||||
// Route receipts from PrivateChatManager through MessageRouter
|
||||
self.privateChatManager.messageRouter = self.messageRouter
|
||||
self.autocompleteService = AutocompleteService()
|
||||
@@ -712,6 +692,198 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
)
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Dependency-injected initializer (for testing and composition)
|
||||
@MainActor
|
||||
init(meshService: Transport,
|
||||
commandProcessor: CommandProcessor,
|
||||
privateChatManager: PrivateChatManager,
|
||||
unifiedPeerService: UnifiedPeerService,
|
||||
messageRouter: MessageRouter,
|
||||
readReceiptTracker: ReadReceiptTracker = ReadReceiptTracker()) {
|
||||
// Assign dependencies
|
||||
self.meshService = meshService
|
||||
self.commandProcessor = commandProcessor
|
||||
self.privateChatManager = privateChatManager
|
||||
self.unifiedPeerService = unifiedPeerService
|
||||
self.messageRouter = messageRouter
|
||||
self.readReceiptTracker = readReceiptTracker
|
||||
self.autocompleteService = AutocompleteService()
|
||||
self.nostrInbox = NostrInboxService()
|
||||
self.nostrOutbox = NostrOutboxService(meshService: meshService)
|
||||
|
||||
// Wire up dependencies
|
||||
self.commandProcessor.chatViewModel = self
|
||||
self.privateChatManager.messageRouter = self.messageRouter
|
||||
self.commandProcessor.meshService = meshService
|
||||
|
||||
// Subscribe to privateChatManager changes to trigger UI updates
|
||||
privateChatManager.objectWillChange
|
||||
.sink { [weak self] _ in
|
||||
self?.objectWillChange.send()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
loadNickname()
|
||||
loadVerifiedFingerprints()
|
||||
meshService.delegate = self
|
||||
|
||||
// Log fingerprint after a delay to ensure encryption service is ready
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + TransportConfig.uiStartupInitialDelaySeconds) { [weak self] in
|
||||
if let self = self {
|
||||
_ = self.getMyFingerprint()
|
||||
}
|
||||
}
|
||||
|
||||
// Set nickname before starting services
|
||||
meshService.setNickname(nickname)
|
||||
|
||||
// Start mesh service immediately
|
||||
meshService.startServices()
|
||||
|
||||
// Initialize Nostr services
|
||||
Task { @MainActor in
|
||||
nostrRelayManager = NostrRelayManager.shared
|
||||
SecureLogger.log("Initializing Nostr relay connections", category: SecureLogger.session, level: .debug)
|
||||
nostrRelayManager?.connect()
|
||||
|
||||
// Small delay to ensure read receipts are fully loaded
|
||||
try? await Task.sleep(nanoseconds: TransportConfig.uiStartupShortSleepNs)
|
||||
|
||||
// Set up Nostr message handling directly
|
||||
setupNostrMessageHandling()
|
||||
|
||||
// Attempt to flush any queued outbox after Nostr comes online
|
||||
messageRouter.flushAllOutbox()
|
||||
|
||||
// End startup phase after 2 seconds
|
||||
Task { @MainActor in
|
||||
try? await Task.sleep(nanoseconds: UInt64(TransportConfig.uiStartupPhaseDurationSeconds * 1_000_000_000))
|
||||
self.isStartupPhase = false
|
||||
}
|
||||
|
||||
// Bind unified peer service's peer list to our published property
|
||||
let cancellable = unifiedPeerService.$peers
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] peers in
|
||||
// Update peers on main thread for UI consistency
|
||||
guard let self = self else { return }
|
||||
Task { @MainActor in
|
||||
self.allPeers = peers
|
||||
// Build a quick index for getPeer(by:)
|
||||
var uniquePeers: [String: BitchatPeer] = [:]
|
||||
for peer in peers {
|
||||
if uniquePeers[peer.id] == nil {
|
||||
uniquePeers[peer.id] = peer
|
||||
}
|
||||
}
|
||||
self.peerIndex = uniquePeers
|
||||
if self.selectedPrivateChatFingerprint != nil {
|
||||
self.updatePrivateChatPeerIfNeeded()
|
||||
}
|
||||
}
|
||||
}
|
||||
self.cancellables.insert(cancellable)
|
||||
|
||||
// Resubscribe geohash on relay reconnect
|
||||
if let relayMgr = self.nostrRelayManager {
|
||||
relayMgr.$isConnected
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] connected in
|
||||
guard let self = self else { return }
|
||||
if connected {
|
||||
Task { @MainActor in
|
||||
self.resubscribeCurrentGeohash()
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &self.cancellables)
|
||||
}
|
||||
}
|
||||
|
||||
// Set up Noise encryption callbacks
|
||||
setupNoiseCallbacks()
|
||||
|
||||
// Observe location channel selection
|
||||
LocationChannelManager.shared.$selectedChannel
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] channel in
|
||||
guard let self = self else { return }
|
||||
Task { @MainActor in
|
||||
self.switchLocationChannel(to: channel)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
// Initialize with current selection
|
||||
Task { @MainActor in
|
||||
self.switchLocationChannel(to: LocationChannelManager.shared.selectedChannel)
|
||||
}
|
||||
|
||||
// Request notification permission
|
||||
NotificationService.shared.requestAuthorization()
|
||||
|
||||
// Listen for favorite status changes
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(handleFavoriteStatusChanged),
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil
|
||||
)
|
||||
|
||||
// Listen for peer status updates to refresh UI
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(handlePeerStatusUpdate),
|
||||
name: Notification.Name("peerStatusUpdated"),
|
||||
object: nil
|
||||
)
|
||||
|
||||
#if os(macOS)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appDidBecomeActive),
|
||||
name: NSApplication.didBecomeActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appWillResignActive),
|
||||
name: NSApplication.willResignActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appWillTerminate),
|
||||
name: NSApplication.willTerminateNotification,
|
||||
object: nil
|
||||
)
|
||||
#else
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appDidBecomeActive),
|
||||
name: UIApplication.didBecomeActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appWillResignActive),
|
||||
name: UIApplication.willResignActiveNotification,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(appWillTerminate),
|
||||
name: UIApplication.willTerminateNotification,
|
||||
object: nil
|
||||
)
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(userDidTakeScreenshot),
|
||||
name: UIApplication.userDidTakeScreenshotNotification,
|
||||
object: nil
|
||||
)
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Deinitialization
|
||||
|
||||
@@ -732,20 +904,15 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
startGeoParticipantsTimer()
|
||||
// Unsubscribe + resubscribe
|
||||
NostrRelayManager.shared.unsubscribe(id: subID)
|
||||
let filter = NostrFilter.geohashEphemeral(
|
||||
ch.geohash,
|
||||
since: Date().addingTimeInterval(-TransportConfig.nostrGeohashInitialLookbackSeconds),
|
||||
limit: TransportConfig.nostrGeohashInitialLimit
|
||||
)
|
||||
let subRelays = GeoRelayDirectory.shared.closestRelays(
|
||||
toGeohash: ch.geohash,
|
||||
count: TransportConfig.nostrGeoRelayCount
|
||||
)
|
||||
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
|
||||
nostrInbox.subscribeGeohashEphemeral(
|
||||
geohash: ch.geohash,
|
||||
lookbackSeconds: TransportConfig.nostrGeohashInitialLookbackSeconds,
|
||||
limit: TransportConfig.nostrGeohashInitialLimit,
|
||||
relayCount: TransportConfig.nostrGeoRelayCount,
|
||||
subID: subID
|
||||
) { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
|
||||
if self.processedNostrEvents.contains(event.id) { return }
|
||||
self.processedNostrEvents.insert(event.id)
|
||||
if let gh = self.currentGeohash,
|
||||
let myGeoIdentity = try? NostrIdentityBridge.deriveIdentity(forGeohash: gh),
|
||||
myGeoIdentity.publicKeyHex.lowercased() == event.pubkey.lowercased() {
|
||||
@@ -802,11 +969,12 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let id = try NostrIdentityBridge.deriveIdentity(forGeohash: ch.geohash)
|
||||
let dmSub = "geo-dm-\(ch.geohash)"
|
||||
geoDmSubscriptionID = dmSub
|
||||
let dmFilter = NostrFilter.giftWrapsFor(pubkey: id.publicKeyHex, since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds))
|
||||
NostrRelayManager.shared.subscribe(filter: dmFilter, id: dmSub) { [weak self] giftWrap in
|
||||
nostrInbox.subscribeGiftWrapsFor(
|
||||
pubkeyHex: id.publicKeyHex,
|
||||
lookbackSeconds: TransportConfig.nostrDMSubscribeLookbackSeconds,
|
||||
subID: dmSub
|
||||
) { [weak self] giftWrap in
|
||||
guard let self = self else { return }
|
||||
if self.processedNostrEvents.contains(giftWrap.id) { return }
|
||||
self.recordProcessedEvent(giftWrap.id)
|
||||
guard let (content, senderPubkey, rumorTs) = try? NostrProtocol.decryptPrivateMessage(giftWrap: giftWrap, recipientIdentity: id) else { return }
|
||||
guard content.hasPrefix("bitchat1:") else { return }
|
||||
guard let packetData = Self.base64URLDecode(String(content.dropFirst("bitchat1:".count))),
|
||||
@@ -822,9 +990,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let messageId = pm.messageID
|
||||
// Send delivery ACK immediately (once per message ID)
|
||||
if !self.sentGeoDeliveryAcks.contains(messageId) {
|
||||
let nt = NostrTransport()
|
||||
nt.senderPeerID = self.meshService.myPeerID
|
||||
nt.sendDeliveryAckGeohash(for: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.nostrOutbox.sendGeohashDeliveredAck(messageID: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.sentGeoDeliveryAcks.insert(messageId)
|
||||
}
|
||||
// Dedup storage
|
||||
@@ -833,7 +999,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let senderName = self.displayNameForNostrPubkey(senderPubkey)
|
||||
let isViewing = (self.selectedPrivateChatPeer == convKey)
|
||||
// pared back: omit view-state log
|
||||
let wasReadBefore = self.sentReadReceipts.contains(messageId)
|
||||
let wasReadBefore = self.readReceiptTracker.contains(messageId)
|
||||
let isRecentMessage = Date().timeIntervalSince(messageTimestamp) < 30
|
||||
let shouldMarkUnread = !wasReadBefore && !isViewing && isRecentMessage
|
||||
let msg = BitchatMessage(
|
||||
@@ -860,10 +1026,8 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
if isViewing {
|
||||
// pared back: omit pre-send READ log
|
||||
if !wasReadBefore {
|
||||
let nt = NostrTransport()
|
||||
nt.senderPeerID = self.meshService.myPeerID
|
||||
nt.sendReadReceiptGeohash(messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.sentReadReceipts.insert(messageId)
|
||||
self.nostrOutbox.sendGeohashReadAck(messageID: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.readReceiptTracker.insert(messageId)
|
||||
}
|
||||
} else {
|
||||
// Notify for truly unread and recent messages when not viewing
|
||||
@@ -993,7 +1157,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
// Ephemeral peer IDs are 8 bytes = 16 hex characters
|
||||
// Noise public keys are 32 bytes = 64 hex characters
|
||||
|
||||
if peerID.count == 64, let noisePublicKey = Data(hexString: peerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let noisePublicKey = Data(hexString: peerID) {
|
||||
// This is a stable Noise key hex (used in private chats)
|
||||
// Find the ephemeral peer ID for this Noise key
|
||||
let ephemeralPeerID = unifiedPeerService.peers.first { peer in
|
||||
@@ -1056,7 +1220,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
@MainActor
|
||||
func isFavorite(peerID: String) -> Bool {
|
||||
// Distinguish between ephemeral peer IDs (16 hex chars) and Noise public keys (64 hex chars)
|
||||
if peerID.count == 64, let noisePublicKey = Data(hexString: peerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let noisePublicKey = Data(hexString: peerID) {
|
||||
// This is a Noise public key
|
||||
if let status = FavoritesPersistenceService.shared.getFavoriteStatus(for: noisePublicKey) {
|
||||
return status.isFavorite
|
||||
@@ -1343,8 +1507,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
publicBuffer.removeAll(keepingCapacity: false)
|
||||
activeChannel = channel
|
||||
// Reset deduplication set and optionally hydrate timeline for mesh
|
||||
processedNostrEvents.removeAll()
|
||||
processedNostrEventOrder.removeAll()
|
||||
nostrInbox.reset()
|
||||
switch channel {
|
||||
case .mesh:
|
||||
messages = meshTimeline
|
||||
@@ -1388,19 +1551,16 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let subID = "geo-\(ch.geohash)"
|
||||
geoSubscriptionID = subID
|
||||
startGeoParticipantsTimer()
|
||||
let filter = NostrFilter.geohashEphemeral(
|
||||
ch.geohash,
|
||||
since: Date().addingTimeInterval(-TransportConfig.nostrGeohashInitialLookbackSeconds),
|
||||
limit: TransportConfig.nostrGeohashInitialLimit
|
||||
)
|
||||
let subRelays = GeoRelayDirectory.shared.closestRelays(toGeohash: ch.geohash, count: 5)
|
||||
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
|
||||
nostrInbox.subscribeGeohashEphemeral(
|
||||
geohash: ch.geohash,
|
||||
lookbackSeconds: TransportConfig.nostrGeohashInitialLookbackSeconds,
|
||||
limit: TransportConfig.nostrGeohashInitialLimit,
|
||||
relayCount: TransportConfig.nostrGeoRelayCount,
|
||||
subID: subID
|
||||
) { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
// Only handle ephemeral kind 20000 with matching tag
|
||||
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
|
||||
// Deduplicate
|
||||
if self.processedNostrEvents.contains(event.id) { return }
|
||||
self.recordProcessedEvent(event.id)
|
||||
// Log incoming tags for diagnostics
|
||||
let tagSummary = event.tags.map { "[" + $0.joined(separator: ",") + "]" }.joined(separator: ",")
|
||||
SecureLogger.log("GeoTeleport: recv pub=\(event.pubkey.prefix(8))… tags=\(tagSummary)",
|
||||
@@ -1477,12 +1637,8 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
// pared back logging: subscribe debug only
|
||||
SecureLogger.log("GeoDM: subscribing DMs pub=\(id.publicKeyHex.prefix(8))… sub=\(dmSub)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
let dmFilter = NostrFilter.giftWrapsFor(pubkey: id.publicKeyHex, since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds))
|
||||
NostrRelayManager.shared.subscribe(filter: dmFilter, id: dmSub) { [weak self] giftWrap in
|
||||
nostrInbox.subscribeGiftWrapsFor(pubkeyHex: id.publicKeyHex, lookbackSeconds: TransportConfig.nostrDMSubscribeLookbackSeconds, subID: dmSub) { [weak self] giftWrap in
|
||||
guard let self = self else { return }
|
||||
// Dedup basic
|
||||
if self.processedNostrEvents.contains(giftWrap.id) { return }
|
||||
self.recordProcessedEvent(giftWrap.id)
|
||||
// Decrypt with per-geohash identity
|
||||
guard let (content, senderPubkey, rumorTs) = try? NostrProtocol.decryptPrivateMessage(giftWrap: giftWrap, recipientIdentity: id) else {
|
||||
SecureLogger.log("GeoDM: failed decrypt giftWrap id=\(giftWrap.id.prefix(8))…",
|
||||
@@ -1507,10 +1663,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
category: SecureLogger.session, level: .info)
|
||||
// Send delivery ACK immediately (even if duplicate), once per messageID
|
||||
if !self.sentGeoDeliveryAcks.contains(messageId) {
|
||||
let nostrTransport = NostrTransport()
|
||||
nostrTransport.senderPeerID = self.meshService.myPeerID
|
||||
// pared back: omit pre-send log
|
||||
nostrTransport.sendDeliveryAckGeohash(for: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.nostrOutbox.sendGeohashDeliveredAck(messageID: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.sentGeoDeliveryAcks.insert(messageId)
|
||||
}
|
||||
// Duplicate check
|
||||
@@ -1518,7 +1671,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
for (_, arr) in self.privateChats { if arr.contains(where: { $0.id == messageId }) { return } }
|
||||
let senderName = self.displayNameForNostrPubkey(senderPubkey)
|
||||
let isViewing = (self.selectedPrivateChatPeer == convKey)
|
||||
let wasReadBefore = self.sentReadReceipts.contains(messageId)
|
||||
let wasReadBefore = self.readReceiptTracker.contains(messageId)
|
||||
let isRecentMessage = Date().timeIntervalSince(messageTimestamp) < 30
|
||||
let shouldMarkUnread = !wasReadBefore && !isViewing && isRecentMessage
|
||||
let msg = BitchatMessage(
|
||||
@@ -1542,10 +1695,8 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
if isViewing {
|
||||
// pared back: omit pre-send READ log
|
||||
if !wasReadBefore {
|
||||
let nostrTransport = NostrTransport()
|
||||
nostrTransport.senderPeerID = self.meshService.myPeerID
|
||||
nostrTransport.sendReadReceiptGeohash(messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.sentReadReceipts.insert(messageId)
|
||||
self.nostrOutbox.sendGeohashReadAck(messageID: messageId, toRecipientHex: senderPubkey, from: id)
|
||||
self.readReceiptTracker.insert(messageId)
|
||||
}
|
||||
} else {
|
||||
// pared back: omit defer READ log
|
||||
@@ -1749,7 +1900,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
//
|
||||
for (subID, gh) in geoSamplingSubs where toRemove.contains(gh) {
|
||||
NostrRelayManager.shared.unsubscribe(id: subID)
|
||||
nostrInbox.unsubscribe(id: subID)
|
||||
geoSamplingSubs.removeValue(forKey: subID)
|
||||
}
|
||||
|
||||
@@ -1757,13 +1908,13 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
for gh in toAdd {
|
||||
let subID = "geo-sample-\(gh)"
|
||||
geoSamplingSubs[subID] = gh
|
||||
let filter = NostrFilter.geohashEphemeral(
|
||||
gh,
|
||||
since: Date().addingTimeInterval(-TransportConfig.nostrGeohashSampleLookbackSeconds),
|
||||
limit: TransportConfig.nostrGeohashSampleLimit
|
||||
)
|
||||
let subRelays = GeoRelayDirectory.shared.closestRelays(toGeohash: gh, count: 5)
|
||||
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
|
||||
nostrInbox.subscribeGeohashEphemeral(
|
||||
geohash: gh,
|
||||
lookbackSeconds: TransportConfig.nostrGeohashSampleLookbackSeconds,
|
||||
limit: TransportConfig.nostrGeohashSampleLimit,
|
||||
relayCount: TransportConfig.nostrGeoRelayCount,
|
||||
subID: subID
|
||||
) { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
|
||||
// Update participants for this specific geohash
|
||||
@@ -1839,19 +1990,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
|
||||
// Dedup helper with small memory cap
|
||||
private func recordProcessedEvent(_ id: String) {
|
||||
processedNostrEvents.insert(id)
|
||||
processedNostrEventOrder.append(id)
|
||||
if processedNostrEventOrder.count > maxProcessedNostrEvents {
|
||||
let overflow = processedNostrEventOrder.count - maxProcessedNostrEvents
|
||||
for _ in 0..<overflow {
|
||||
if let old = processedNostrEventOrder.first {
|
||||
processedNostrEventOrder.removeFirst()
|
||||
processedNostrEvents.remove(old)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private func recordProcessedEvent(_ id: String) {}
|
||||
|
||||
/// Sends an encrypted private message to a specific peer.
|
||||
/// - Parameters:
|
||||
@@ -1915,9 +2054,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
SecureLogger.log("GeoDM: local send mid=\(messageID.prefix(8))… to=\(recipientHex.prefix(8))… conv=\(peerID)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
let nostrTransport = NostrTransport()
|
||||
nostrTransport.senderPeerID = meshService.myPeerID
|
||||
nostrTransport.sendPrivateMessageGeohash(content: content, toRecipientHex: recipientHex, from: id, messageID: messageID)
|
||||
self.nostrOutbox.sendGeohashPM(content: content, toRecipientHex: recipientHex, from: id, messageID: messageID)
|
||||
if let msgIdx = privateChats[peerID]?.firstIndex(where: { $0.id == messageID }) {
|
||||
privateChats[peerID]?[msgIdx].deliveryStatus = .sent
|
||||
}
|
||||
@@ -2142,7 +2279,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
// Never mark old messages as unread during consolidation
|
||||
if message.senderPeerID != meshService.myPeerID {
|
||||
let messageAge = Date().timeIntervalSince(message.timestamp)
|
||||
if messageAge < 60 && !sentReadReceipts.contains(message.id) {
|
||||
if messageAge < 60 && !readReceiptTracker.contains(message.id) {
|
||||
hasActualUnreadMessages = true
|
||||
}
|
||||
}
|
||||
@@ -2263,7 +2400,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
// Delegate to private chat manager but add already-acked messages first
|
||||
// This prevents duplicate read receipts
|
||||
// IMPORTANT: Only add messages WE sent to sentReadReceipts, not messages we received
|
||||
// IMPORTANT: Only add messages WE sent to readReceiptTracker, not messages we received
|
||||
if let messages = privateChats[peerID] {
|
||||
for message in messages {
|
||||
// Only track read receipts for messages WE sent (not received messages)
|
||||
@@ -2272,8 +2409,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
if let status = message.deliveryStatus {
|
||||
switch status {
|
||||
case .read, .delivered:
|
||||
sentReadReceipts.insert(message.id)
|
||||
privateChatManager.sentReadReceipts.insert(message.id)
|
||||
readReceiptTracker.insert(message.id)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -2627,13 +2763,11 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let id = try? NostrIdentityBridge.deriveIdentity(forGeohash: ch.geohash) {
|
||||
let messages = privateChats[peerID] ?? []
|
||||
for message in messages where message.senderPeerID == peerID && !message.isRelay {
|
||||
if !sentReadReceipts.contains(message.id) {
|
||||
if !readReceiptTracker.contains(message.id) {
|
||||
SecureLogger.log("GeoDM: sending READ for mid=\(message.id.prefix(8))… to=\(recipientHex.prefix(8))…",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
let nostrTransport = NostrTransport()
|
||||
nostrTransport.senderPeerID = meshService.myPeerID
|
||||
nostrTransport.sendReadReceiptGeohash(message.id, toRecipientHex: recipientHex, from: id)
|
||||
sentReadReceipts.insert(message.id)
|
||||
self.nostrOutbox.sendGeohashReadAck(messageID: message.id, toRecipientHex: recipientHex, from: id)
|
||||
readReceiptTracker.insert(message.id)
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -2671,12 +2805,12 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
// Check both the ephemeral peer ID and stable Noise key as sender
|
||||
if (message.senderPeerID == peerID || message.senderPeerID == noiseKeyHex) && !message.isRelay {
|
||||
// Skip if we already sent an ACK for this message
|
||||
if !sentReadReceipts.contains(message.id) {
|
||||
if !readReceiptTracker.contains(message.id) {
|
||||
// Use stable Noise key hex if available; else fall back to peerID
|
||||
let recipPeer = (Data(hexString: peerID) != nil) ? peerID : (unifiedPeerService.getPeer(by: peerID)?.noisePublicKey.hexEncodedString() ?? peerID)
|
||||
let receipt = ReadReceipt(originalMessageID: message.id, readerID: meshService.myPeerID, readerNickname: nickname)
|
||||
messageRouter.sendReadReceipt(receipt, to: recipPeer)
|
||||
sentReadReceipts.insert(message.id)
|
||||
readReceiptTracker.insert(message.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2804,7 +2938,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
selectedPrivateChatFingerprint = nil
|
||||
|
||||
// Clear read receipt tracking
|
||||
sentReadReceipts.removeAll()
|
||||
readReceiptTracker.removeAll()
|
||||
processedNostrAcks.removeAll()
|
||||
|
||||
// Clear all caches
|
||||
@@ -3603,7 +3737,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}()
|
||||
let full = nostrKeyMapping[spid]?.lowercased() ?? bare.lowercased()
|
||||
return getNostrPaletteColor(for: full, isDark: isDark)
|
||||
} else if spid.count == 16 {
|
||||
} else if PeerIDResolver.isShortID(spid) {
|
||||
// Mesh short ID
|
||||
return getPeerPaletteColor(for: spid, isDark: isDark)
|
||||
} else {
|
||||
@@ -3989,7 +4123,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
// Check if this might already be a nickname (not a hex peer ID)
|
||||
// Peer IDs are hex strings, so they only contain 0-9 and a-f
|
||||
let isHexID = peerID.allSatisfy { $0.isHexDigit }
|
||||
let isHexID = PeerIDResolver.isShortID(peerID) || PeerIDResolver.isNoiseKeyHex(peerID)
|
||||
if !isHexID {
|
||||
// If it's already a nickname, just return it
|
||||
return peerID
|
||||
@@ -4437,7 +4571,10 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
for message in messages {
|
||||
// Remove read receipts for messages FROM this peer (not TO this peer)
|
||||
if message.senderPeerID == peerID {
|
||||
sentReadReceipts.remove(message.id)
|
||||
// Prune single ID from tracker if needed
|
||||
// Note: ReadReceiptTracker persists asynchronously
|
||||
// Remove only if we want to allow resending READ later
|
||||
// For now keep as-is; leave this as a no-op
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4540,7 +4677,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
// Don't remove stable Noise key hexes (64 char hex strings) that have messages
|
||||
// These are used for Nostr messages when peer is offline
|
||||
if staleID.count == 64, staleID.allSatisfy({ $0.isHexDigit }) {
|
||||
if PeerIDResolver.isNoiseKeyHex(staleID) {
|
||||
if let messages = privateChats[staleID], !messages.isEmpty {
|
||||
// Keep this ID - it's a stable key with messages
|
||||
continue
|
||||
@@ -4579,10 +4716,10 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
|
||||
// Remove receipts for messages we no longer have
|
||||
let oldCount = sentReadReceipts.count
|
||||
sentReadReceipts = sentReadReceipts.intersection(validMessageIDs)
|
||||
|
||||
let removedCount = oldCount - sentReadReceipts.count
|
||||
let before = readReceiptTracker.snapshot()
|
||||
readReceiptTracker.prune(toAllowedIDs: validMessageIDs)
|
||||
let after = readReceiptTracker.snapshot()
|
||||
let removedCount = before.count - after.count
|
||||
if removedCount > 0 {
|
||||
SecureLogger.log("🧹 Cleaned up \(removedCount) old read receipts",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
@@ -4768,12 +4905,11 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
category: SecureLogger.session, level: .debug)
|
||||
|
||||
// Subscribe to Nostr messages
|
||||
let filter = NostrFilter.giftWrapsFor(
|
||||
pubkey: currentIdentity.publicKeyHex,
|
||||
since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds) // Last 24 hours
|
||||
)
|
||||
|
||||
nostrRelayManager?.subscribe(filter: filter, id: "chat-messages") { [weak self] event in
|
||||
let _ = nostrInbox.subscribeGiftWrapsFor(
|
||||
pubkeyHex: currentIdentity.publicKeyHex,
|
||||
lookbackSeconds: TransportConfig.nostrDMSubscribeLookbackSeconds,
|
||||
subID: "chat-messages"
|
||||
) { [weak self] event in
|
||||
Task { @MainActor in
|
||||
self?.handleNostrMessage(event)
|
||||
}
|
||||
@@ -4782,9 +4918,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
@MainActor
|
||||
private func handleNostrMessage(_ giftWrap: NostrEvent) {
|
||||
// Simple deduplication
|
||||
if processedNostrEvents.contains(giftWrap.id) { return }
|
||||
processedNostrEvents.insert(giftWrap.id)
|
||||
// De-duplication handled by NostrInboxService
|
||||
|
||||
// Client-side filtering: ignore messages older than 24 hours
|
||||
// Add 15 minutes buffer since gift wrap timestamps are randomized ±15 minutes
|
||||
@@ -4869,7 +5003,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
if messageExistsLocally { return }
|
||||
|
||||
let wasReadBefore = sentReadReceipts.contains(messageId)
|
||||
let wasReadBefore = readReceiptTracker.contains(messageId)
|
||||
|
||||
// Is viewing?
|
||||
var isViewingThisChat = false
|
||||
@@ -4946,17 +5080,17 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.id {
|
||||
unreadPrivateMessages.remove(ephemeralPeerID)
|
||||
}
|
||||
if !sentReadReceipts.contains(messageId) {
|
||||
if !readReceiptTracker.contains(messageId) {
|
||||
if let key = actualSenderNoiseKey {
|
||||
let receipt = ReadReceipt(originalMessageID: messageId, readerID: meshService.myPeerID, readerNickname: nickname)
|
||||
SecureLogger.log("Viewing chat; sending READ ack for \(messageId.prefix(8))… via router", category: SecureLogger.session, level: .debug)
|
||||
messageRouter.sendReadReceipt(receipt, to: key.hexEncodedString())
|
||||
sentReadReceipts.insert(messageId)
|
||||
readReceiptTracker.insert(messageId)
|
||||
} else if let id = try? NostrIdentityBridge.getCurrentNostrIdentity() {
|
||||
let nt = NostrTransport()
|
||||
nt.senderPeerID = meshService.myPeerID
|
||||
nt.sendReadReceiptGeohash(messageId, toRecipientHex: senderPubkey, from: id)
|
||||
sentReadReceipts.insert(messageId)
|
||||
readReceiptTracker.insert(messageId)
|
||||
SecureLogger.log("Viewing chat; sent READ ack directly to Nostr pub=\(senderPubkey.prefix(8))… for mid=\(messageId.prefix(8))…", category: SecureLogger.session, level: .debug)
|
||||
}
|
||||
}
|
||||
@@ -5107,7 +5241,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
var noiseKey: Data? = nil
|
||||
|
||||
// First try as hex-encoded Noise key (64 chars)
|
||||
if peerID.count == 64 {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID) {
|
||||
noiseKey = Data(hexString: peerID)
|
||||
}
|
||||
|
||||
@@ -5157,7 +5291,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
|
||||
// Check if we've read this message before (in a previous session)
|
||||
let wasReadBefore = sentReadReceipts.contains(messageId)
|
||||
let wasReadBefore = readReceiptTracker.contains(messageId)
|
||||
|
||||
// Try to find sender by checking all known peers for nickname matches
|
||||
// This is a fallback when we receive Nostr messages from someone not in favorites
|
||||
@@ -5505,7 +5639,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
// IMPORTANT: Also consolidate messages from stable Noise key if this is an ephemeral peer
|
||||
// This ensures Nostr messages appear in BLE chats
|
||||
if peerID.count == 16 { // This is an ephemeral peer ID (8 bytes = 16 hex chars)
|
||||
if PeerIDResolver.isShortID(peerID) { // This is an ephemeral peer ID (8 bytes = 16 hex chars)
|
||||
if let peer = unifiedPeerService.getPeer(by: peerID) {
|
||||
let stableKeyHex = peer.noisePublicKey.hexEncodedString()
|
||||
|
||||
@@ -5576,7 +5710,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
if selectedPrivateChatPeer != peerID {
|
||||
unreadPrivateMessages.insert(peerID)
|
||||
// Avoid notifying for messages that have been marked read already (resubscribe/dup cases)
|
||||
if !sentReadReceipts.contains(message.id) {
|
||||
if !readReceiptTracker.contains(message.id) {
|
||||
NotificationService.shared.sendPrivateMessageNotification(
|
||||
from: message.sender,
|
||||
message: message.content,
|
||||
@@ -5592,7 +5726,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
cleanupStaleUnreadPeerIDs()
|
||||
|
||||
// Send read receipt if needed
|
||||
if !sentReadReceipts.contains(message.id) {
|
||||
if !readReceiptTracker.contains(message.id) {
|
||||
let receipt = ReadReceipt(
|
||||
originalMessageID: message.id,
|
||||
readerID: meshService.myPeerID,
|
||||
@@ -5612,7 +5746,7 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
|
||||
self.sendReadReceipt(receipt, to: recipientID, originalTransport: originalTransport)
|
||||
}
|
||||
sentReadReceipts.insert(message.id)
|
||||
readReceiptTracker.insert(message.id)
|
||||
}
|
||||
|
||||
// Mark other messages as read
|
||||
|
||||
@@ -1182,7 +1182,7 @@ struct ContentView: View {
|
||||
private func privateHeaderContent(for privatePeerID: String) -> some View {
|
||||
// Prefer short (mesh) ID whenever available for encryption/session status; keep stable key for display resolution only.
|
||||
let headerPeerID: String = {
|
||||
if privatePeerID.count == 64 {
|
||||
if PeerIDResolver.isNoiseKeyHex(privatePeerID) {
|
||||
// Map stable Noise key to short ID if we know it (even if not directly connected)
|
||||
if let short = viewModel.getShortIDForNoiseKey(privatePeerID) { return short }
|
||||
}
|
||||
@@ -1207,14 +1207,14 @@ struct ContentView: View {
|
||||
if let fav = FavoritesPersistenceService.shared.getFavoriteStatus(for: Data(hexString: headerPeerID) ?? Data()),
|
||||
!fav.peerNickname.isEmpty { return fav.peerNickname }
|
||||
// Fallback: resolve from persisted social identity via fingerprint mapping
|
||||
if headerPeerID.count == 16 {
|
||||
if PeerIDResolver.isShortID(headerPeerID) {
|
||||
let candidates = SecureIdentityStateManager.shared.getCryptoIdentitiesByPeerIDPrefix(headerPeerID)
|
||||
if let id = candidates.first,
|
||||
let social = SecureIdentityStateManager.shared.getSocialIdentity(for: id.fingerprint) {
|
||||
if let pet = social.localPetname, !pet.isEmpty { return pet }
|
||||
if !social.claimedNickname.isEmpty { return social.claimedNickname }
|
||||
}
|
||||
} else if headerPeerID.count == 64, let keyData = Data(hexString: headerPeerID) {
|
||||
} else if PeerIDResolver.isNoiseKeyHex(headerPeerID), let keyData = Data(hexString: headerPeerID) {
|
||||
let fp = keyData.sha256Fingerprint()
|
||||
if let social = SecureIdentityStateManager.shared.getSocialIdentity(for: fp) {
|
||||
if let pet = social.localPetname, !pet.isEmpty { return pet }
|
||||
@@ -1298,7 +1298,7 @@ struct ContentView: View {
|
||||
if !privatePeerID.hasPrefix("nostr_") {
|
||||
// Use short peer ID if available for encryption status (sessions keyed by short ID)
|
||||
let statusPeerID: String = {
|
||||
if privatePeerID.count == 64, let short = viewModel.getShortIDForNoiseKey(privatePeerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(privatePeerID), let short = viewModel.getShortIDForNoiseKey(privatePeerID) {
|
||||
return short
|
||||
}
|
||||
return headerPeerID
|
||||
|
||||
@@ -43,14 +43,14 @@ struct FingerprintView: View {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
// Prefer short mesh ID for session/encryption status
|
||||
let statusPeerID: String = {
|
||||
if peerID.count == 64, let short = viewModel.getShortIDForNoiseKey(peerID) { return short }
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let short = viewModel.getShortIDForNoiseKey(peerID) { return short }
|
||||
return peerID
|
||||
}()
|
||||
// Resolve a friendly name
|
||||
let peerNickname: String = {
|
||||
if let p = viewModel.getPeer(byID: statusPeerID) { return p.displayName }
|
||||
if let name = viewModel.meshService.peerNickname(peerID: statusPeerID) { return name }
|
||||
if peerID.count == 64, let data = Data(hexString: peerID) {
|
||||
if PeerIDResolver.isNoiseKeyHex(peerID), let data = Data(hexString: peerID) {
|
||||
if let fav = FavoritesPersistenceService.shared.getFavoriteStatus(for: data), !fav.peerNickname.isEmpty { return fav.peerNickname }
|
||||
let fp = data.sha256Fingerprint()
|
||||
if let social = SecureIdentityStateManager.shared.getSocialIdentity(for: fp) {
|
||||
|
||||
Reference in New Issue
Block a user