mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 02:05:19 +00:00
PeerID 3/n: Remove dead code (#706)
* PeerID 2/n: Count and hex conversion done with `bare` * ChatVM: remove dead code `registerPeerPublicKey` * ChatVM: Remove dead `handlePeerFavoritedUs` * ChatVM: remove dead functions * PrivateChatManager: Remove dead code * BLEService: Remove dead code
This commit is contained in:
@@ -539,21 +539,6 @@ final class BLEService: NSObject {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// MARK: - Helper Functions for Peripheral Management
|
|
||||||
|
|
||||||
private func getConnectedPeripherals() -> [CBPeripheral] {
|
|
||||||
return peripherals.values
|
|
||||||
.filter { $0.isConnected }
|
|
||||||
.map { $0.peripheral }
|
|
||||||
}
|
|
||||||
|
|
||||||
private func getPeripheral(for peerID: String) -> CBPeripheral? {
|
|
||||||
guard let uuid = peerToPeripheralUUID[peerID],
|
|
||||||
let state = peripherals[uuid],
|
|
||||||
state.isConnected else { return nil }
|
|
||||||
return state.peripheral
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Core Public API
|
// MARK: - Core Public API
|
||||||
|
|
||||||
func startServices() {
|
func startServices() {
|
||||||
|
|||||||
@@ -53,87 +53,6 @@ final class PrivateChatManager: ObservableObject {
|
|||||||
selectedPeer = nil
|
selectedPeer = nil
|
||||||
selectedPeerFingerprint = nil
|
selectedPeerFingerprint = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a private message
|
|
||||||
func sendMessage(_ content: String, to peerID: String) {
|
|
||||||
guard let meshService = meshService,
|
|
||||||
let peerNickname = meshService.peerNickname(peerID: peerID) else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
let messageID = UUID().uuidString
|
|
||||||
|
|
||||||
// Create local message
|
|
||||||
let message = BitchatMessage(
|
|
||||||
id: messageID,
|
|
||||||
sender: meshService.myNickname,
|
|
||||||
content: content,
|
|
||||||
timestamp: Date(),
|
|
||||||
isRelay: false,
|
|
||||||
originalSender: nil,
|
|
||||||
isPrivate: true,
|
|
||||||
recipientNickname: peerNickname,
|
|
||||||
senderPeerID: meshService.myPeerID,
|
|
||||||
mentions: nil,
|
|
||||||
deliveryStatus: .sending
|
|
||||||
)
|
|
||||||
|
|
||||||
// Add to chat
|
|
||||||
if privateChats[peerID] == nil { privateChats[peerID] = [] }
|
|
||||||
privateChats[peerID]?.append(message)
|
|
||||||
// Enforce per-chat cap on local append
|
|
||||||
if var arr = privateChats[peerID], arr.count > privateChatCap {
|
|
||||||
let remove = arr.count - privateChatCap
|
|
||||||
arr.removeFirst(remove)
|
|
||||||
privateChats[peerID] = arr
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send via mesh service
|
|
||||||
meshService.sendPrivateMessage(content, to: peerID, recipientNickname: peerNickname, messageID: messageID)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle incoming private message
|
|
||||||
func handleIncomingMessage(_ message: BitchatMessage) {
|
|
||||||
guard let senderPeerID = message.senderPeerID else { return }
|
|
||||||
|
|
||||||
// Initialize chat if needed
|
|
||||||
if privateChats[senderPeerID] == nil {
|
|
||||||
privateChats[senderPeerID] = []
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deduplicate by ID: replace existing message if present, else append
|
|
||||||
if let idx = privateChats[senderPeerID]?.firstIndex(where: { $0.id == message.id }) {
|
|
||||||
privateChats[senderPeerID]?[idx] = message
|
|
||||||
} else {
|
|
||||||
privateChats[senderPeerID]?.append(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanitize chat to avoid duplicate IDs and sort by timestamp
|
|
||||||
sanitizeChat(for: senderPeerID)
|
|
||||||
// Enforce cap after sanitize
|
|
||||||
if var arr = privateChats[senderPeerID], arr.count > privateChatCap {
|
|
||||||
let remove = arr.count - privateChatCap
|
|
||||||
arr.removeFirst(remove)
|
|
||||||
privateChats[senderPeerID] = arr
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark as unread if not in this chat
|
|
||||||
if selectedPeer != senderPeerID {
|
|
||||||
unreadMessages.insert(senderPeerID)
|
|
||||||
|
|
||||||
// Avoid notifying for messages already marked as read (dup/resubscribe cases)
|
|
||||||
if !sentReadReceipts.contains(message.id) {
|
|
||||||
NotificationService.shared.sendPrivateMessageNotification(
|
|
||||||
from: message.sender,
|
|
||||||
message: message.content,
|
|
||||||
peerID: senderPeerID
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Send read receipt if viewing this chat
|
|
||||||
sendReadReceipt(for: message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove duplicate messages by ID and keep chronological order
|
/// Remove duplicate messages by ID and keep chronological order
|
||||||
func sanitizeChat(for peerID: String) {
|
func sanitizeChat(for peerID: String) {
|
||||||
@@ -173,48 +92,6 @@ final class PrivateChatManager: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update the selected peer if fingerprint matches (for reconnections)
|
|
||||||
func updateSelectedPeer(peers: [String: String]) {
|
|
||||||
guard let fingerprint = selectedPeerFingerprint else { return }
|
|
||||||
|
|
||||||
// Find peer with matching fingerprint
|
|
||||||
for (peerID, _) in peers {
|
|
||||||
if meshService?.getFingerprint(for: peerID) == fingerprint {
|
|
||||||
selectedPeer = peerID
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get chat messages for current context
|
|
||||||
func getCurrentMessages() -> [BitchatMessage] {
|
|
||||||
guard let peer = selectedPeer else { return [] }
|
|
||||||
return privateChats[peer] ?? []
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clear a private chat
|
|
||||||
func clearChat(with peerID: String) {
|
|
||||||
privateChats[peerID]?.removeAll()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle delivery acknowledgment
|
|
||||||
func handleDeliveryAck(messageID: String, from peerID: String) {
|
|
||||||
guard privateChats[peerID] != nil else { return }
|
|
||||||
|
|
||||||
if let index = privateChats[peerID]?.firstIndex(where: { $0.id == messageID }) {
|
|
||||||
privateChats[peerID]?[index].deliveryStatus = .delivered(to: "recipient", at: Date())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle read receipt
|
|
||||||
func handleReadReceipt(messageID: String, from peerID: String) {
|
|
||||||
guard privateChats[peerID] != nil else { return }
|
|
||||||
|
|
||||||
if let index = privateChats[peerID]?.firstIndex(where: { $0.id == messageID }) {
|
|
||||||
privateChats[peerID]?[index].deliveryStatus = .read(by: "recipient", at: Date())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
|
|
||||||
private func sendReadReceipt(for message: BitchatMessage) {
|
private func sendReadReceipt(for message: BitchatMessage) {
|
||||||
|
|||||||
@@ -1087,7 +1087,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
privateChats[convKey] = []
|
privateChats[convKey] = []
|
||||||
}
|
}
|
||||||
privateChats[convKey]?.append(msg)
|
privateChats[convKey]?.append(msg)
|
||||||
trimPrivateChatMessagesIfNeeded(for: convKey)
|
|
||||||
|
|
||||||
// pared back: omit view-state log
|
// pared back: omit view-state log
|
||||||
let isViewing = selectedPrivateChatPeer == convKey
|
let isViewing = selectedPrivateChatPeer == convKey
|
||||||
@@ -1295,57 +1294,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
|
|
||||||
// MARK: - Public Key and Identity Management
|
// MARK: - Public Key and Identity Management
|
||||||
|
|
||||||
// Called when we receive a peer's public key
|
|
||||||
@MainActor
|
|
||||||
func registerPeerPublicKey(peerID: String, publicKeyData: Data) {
|
|
||||||
let fingerprintStr = publicKeyData.sha256Fingerprint()
|
|
||||||
|
|
||||||
// Only register if not already registered
|
|
||||||
if peerIDToPublicKeyFingerprint[peerID] != fingerprintStr {
|
|
||||||
peerIDToPublicKeyFingerprint[peerID] = fingerprintStr
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update identity state manager with handshake completion
|
|
||||||
identityManager.updateHandshakeState(peerID: peerID, state: .completed(fingerprint: fingerprintStr))
|
|
||||||
|
|
||||||
// Update encryption status now that we have the fingerprint
|
|
||||||
updateEncryptionStatus(for: peerID)
|
|
||||||
|
|
||||||
// Check if we have a claimed nickname for this peer
|
|
||||||
let peerNicknames = meshService.getPeerNicknames()
|
|
||||||
if let nickname = peerNicknames[peerID], nickname != "Unknown" && nickname != "anon\(peerID.prefix(4))" {
|
|
||||||
// Update or create social identity with the claimed nickname
|
|
||||||
if var identity = identityManager.getSocialIdentity(for: fingerprintStr) {
|
|
||||||
identity.claimedNickname = nickname
|
|
||||||
identityManager.updateSocialIdentity(identity)
|
|
||||||
} else {
|
|
||||||
let newIdentity = SocialIdentity(
|
|
||||||
fingerprint: fingerprintStr,
|
|
||||||
localPetname: nil,
|
|
||||||
claimedNickname: nickname,
|
|
||||||
trustLevel: .casual,
|
|
||||||
isFavorite: false,
|
|
||||||
isBlocked: false,
|
|
||||||
notes: nil
|
|
||||||
)
|
|
||||||
identityManager.updateSocialIdentity(newIdentity)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this peer is the one we're in a private chat with
|
|
||||||
updatePrivateChatPeerIfNeeded()
|
|
||||||
|
|
||||||
// If we're in a private chat with this peer (by fingerprint), send pending read receipts
|
|
||||||
if let chatFingerprint = selectedPrivateChatFingerprint,
|
|
||||||
chatFingerprint == fingerprintStr {
|
|
||||||
// Send read receipts for any unread messages from this peer
|
|
||||||
// Use a small delay to ensure the connection is fully established
|
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + TransportConfig.uiReadReceiptRetryLongSeconds) { [weak self] in
|
|
||||||
self?.markPrivateMessagesAsRead(from: peerID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
func isPeerBlocked(_ peerID: String) -> Bool {
|
func isPeerBlocked(_ peerID: String) -> Bool {
|
||||||
return unifiedPeerService.isBlocked(peerID)
|
return unifiedPeerService.isBlocked(peerID)
|
||||||
@@ -1399,7 +1347,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
|
|
||||||
// Update all at once
|
// Update all at once
|
||||||
privateChats = chats // Trigger setter
|
privateChats = chats // Trigger setter
|
||||||
trimPrivateChatMessagesIfNeeded(for: currentPeerID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migrate unread status
|
// Migrate unread status
|
||||||
@@ -1837,7 +1784,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
privateChats[convKey] = []
|
privateChats[convKey] = []
|
||||||
}
|
}
|
||||||
privateChats[convKey]?.append(msg)
|
privateChats[convKey]?.append(msg)
|
||||||
trimPrivateChatMessagesIfNeeded(for: convKey)
|
|
||||||
|
|
||||||
let isViewing = selectedPrivateChatPeer == convKey
|
let isViewing = selectedPrivateChatPeer == convKey
|
||||||
let wasReadBefore = sentReadReceipts.contains(messageId)
|
let wasReadBefore = sentReadReceipts.contains(messageId)
|
||||||
@@ -2305,7 +2251,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
privateChats[peerID] = []
|
privateChats[peerID] = []
|
||||||
}
|
}
|
||||||
privateChats[peerID]?.append(message)
|
privateChats[peerID]?.append(message)
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
|
|
||||||
// Trigger UI update for sent message
|
// Trigger UI update for sent message
|
||||||
objectWillChange.send()
|
objectWillChange.send()
|
||||||
@@ -2362,7 +2307,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
privateChats[peerID]?.append(message)
|
privateChats[peerID]?.append(message)
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
objectWillChange.send()
|
objectWillChange.send()
|
||||||
|
|
||||||
// Resolve recipient hex from mapping
|
// Resolve recipient hex from mapping
|
||||||
@@ -2456,7 +2400,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
)
|
)
|
||||||
if privateChats[peerID] == nil { privateChats[peerID] = [] }
|
if privateChats[peerID] == nil { privateChats[peerID] = [] }
|
||||||
privateChats[peerID]?.append(systemMessage)
|
privateChats[peerID]?.append(systemMessage)
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
objectWillChange.send()
|
objectWillChange.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2951,7 +2894,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
}
|
}
|
||||||
chats[peerID]?.append(localNotification)
|
chats[peerID]?.append(localNotification)
|
||||||
privateChats = chats // Trigger setter
|
privateChats = chats // Trigger setter
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// In public chat - send to active public channel
|
// In public chat - send to active public channel
|
||||||
@@ -4238,10 +4180,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func trimPrivateChatMessagesIfNeeded(for peerID: String) {
|
|
||||||
// Handled by PrivateChatManager
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Message Management
|
// MARK: - Message Management
|
||||||
|
|
||||||
private func addMessage(_ message: BitchatMessage) {
|
private func addMessage(_ message: BitchatMessage) {
|
||||||
@@ -4251,10 +4189,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
trimMessagesIfNeeded()
|
trimMessagesIfNeeded()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func addPrivateMessage(_ message: BitchatMessage, for peerID: String) {
|
|
||||||
// Deprecated - messages are now added directly in didReceiveMessage to avoid double processing
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update encryption status in appropriate places, not during view updates
|
// Update encryption status in appropriate places, not during view updates
|
||||||
@MainActor
|
@MainActor
|
||||||
private func updateEncryptionStatus(for peerID: String) {
|
private func updateEncryptionStatus(for peerID: String) {
|
||||||
@@ -4925,36 +4859,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
return Array(Set(mentions)) // Remove duplicates
|
return Array(Set(mentions)) // Remove duplicates
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
func handlePeerFavoritedUs(peerID: String, favorited: Bool, nickname: String, nostrNpub: String? = nil) {
|
|
||||||
// Get peer's noise public key
|
|
||||||
guard let noisePublicKey = Data(hexString: peerID) else { return }
|
|
||||||
|
|
||||||
// Decode npub to hex if provided
|
|
||||||
var nostrPublicKey: String? = nil
|
|
||||||
if let npub = nostrNpub {
|
|
||||||
do {
|
|
||||||
let (hrp, data) = try Bech32.decode(npub)
|
|
||||||
if hrp == "npub" {
|
|
||||||
nostrPublicKey = data.hexEncodedString()
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
SecureLogger.error("Failed to decode Nostr npub: \(error)", category: .session)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update favorite status in persistence service
|
|
||||||
FavoritesPersistenceService.shared.updatePeerFavoritedUs(
|
|
||||||
peerNoisePublicKey: noisePublicKey,
|
|
||||||
favorited: favorited,
|
|
||||||
peerNickname: nickname,
|
|
||||||
peerNostrPublicKey: nostrPublicKey
|
|
||||||
)
|
|
||||||
|
|
||||||
// Update peer list to reflect the change
|
|
||||||
// UnifiedPeerService updates automatically via subscriptions
|
|
||||||
}
|
|
||||||
|
|
||||||
func isFavorite(fingerprint: String) -> Bool {
|
func isFavorite(fingerprint: String) -> Bool {
|
||||||
return identityManager.isFavorite(fingerprint: fingerprint)
|
return identityManager.isFavorite(fingerprint: fingerprint)
|
||||||
}
|
}
|
||||||
@@ -5343,7 +5247,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
}
|
}
|
||||||
// Sanitize to avoid duplicate IDs
|
// Sanitize to avoid duplicate IDs
|
||||||
privateChatManager.sanitizeChat(for: targetPeerID)
|
privateChatManager.sanitizeChat(for: targetPeerID)
|
||||||
trimPrivateChatMessagesIfNeeded(for: targetPeerID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
@@ -5362,7 +5265,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
privateChats[ephemeralPeerID]?[idx] = message
|
privateChats[ephemeralPeerID]?[idx] = message
|
||||||
} else {
|
} else {
|
||||||
privateChats[ephemeralPeerID]?.append(message)
|
privateChats[ephemeralPeerID]?.append(message)
|
||||||
trimPrivateChatMessagesIfNeeded(for: ephemeralPeerID)
|
|
||||||
}
|
}
|
||||||
privateChatManager.sanitizeChat(for: ephemeralPeerID)
|
privateChatManager.sanitizeChat(for: ephemeralPeerID)
|
||||||
}
|
}
|
||||||
@@ -5637,7 +5539,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
privateChats[tempPeerID] = []
|
privateChats[tempPeerID] = []
|
||||||
}
|
}
|
||||||
privateChats[tempPeerID]?.append(message)
|
privateChats[tempPeerID]?.append(message)
|
||||||
trimPrivateChatMessagesIfNeeded(for: tempPeerID)
|
|
||||||
|
|
||||||
// For unknown senders (no Noise key), skip sending Nostr ACKs
|
// For unknown senders (no Noise key), skip sending Nostr ACKs
|
||||||
|
|
||||||
@@ -5885,7 +5786,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
// Initialize with migrated messages
|
// Initialize with migrated messages
|
||||||
privateChats[peerID] = migratedMessages
|
privateChats[peerID] = migratedMessages
|
||||||
}
|
}
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
privateChatManager.sanitizeChat(for: peerID)
|
privateChatManager.sanitizeChat(for: peerID)
|
||||||
|
|
||||||
// Update selectedPrivateChatPeer if it was pointing to an old ID
|
// Update selectedPrivateChatPeer if it was pointing to an old ID
|
||||||
@@ -5971,7 +5871,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
|||||||
var chats = privateChats
|
var chats = privateChats
|
||||||
chats[peerID]?.append(messageToStore)
|
chats[peerID]?.append(messageToStore)
|
||||||
privateChats = chats
|
privateChats = chats
|
||||||
trimPrivateChatMessagesIfNeeded(for: peerID)
|
|
||||||
|
|
||||||
// UI updates via @Published reassignment above
|
// UI updates via @Published reassignment above
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user