From 5f44c56a908065587ae56973379f9e4bbb94a2f0 Mon Sep 17 00:00:00 2001 From: Islam <2553451+qalandarov@users.noreply.github.com> Date: Mon, 6 Oct 2025 16:16:11 +0100 Subject: [PATCH] PeerID 15/n: Bitchat Message & Packet accept in init (#754) --- bitchat/Models/BitchatMessage.swift | 20 ++++++++--- bitchat/Models/BitchatPacket.swift | 4 +-- bitchat/Models/PeerID.swift | 3 +- bitchat/Services/BLEService.swift | 2 +- bitchat/ViewModels/ChatViewModel.swift | 36 +++++++++---------- .../EndToEnd/PublicChatE2ETests.swift | 4 +-- .../Integration/IntegrationTests.swift | 2 +- bitchatTests/Mocks/MockBLEService.swift | 4 +-- .../Protocol/BinaryProtocolTests.swift | 2 +- bitchatTests/TestUtilities/TestHelpers.swift | 2 +- 10 files changed, 45 insertions(+), 34 deletions(-) diff --git a/bitchat/Models/BitchatMessage.swift b/bitchat/Models/BitchatMessage.swift index f52004f2..24b16692 100644 --- a/bitchat/Models/BitchatMessage.swift +++ b/bitchat/Models/BitchatMessage.swift @@ -42,7 +42,19 @@ final class BitchatMessage: Codable { case isPrivate, recipientNickname, senderPeerID, mentions, deliveryStatus } - init(id: String? = nil, sender: String, content: String, timestamp: Date, isRelay: Bool, originalSender: String? = nil, isPrivate: Bool = false, recipientNickname: String? = nil, senderPeerID: String? = nil, mentions: [String]? = nil, deliveryStatus: DeliveryStatus? = nil) { + init( + id: String? = nil, + sender: String, + content: String, + timestamp: Date, + isRelay: Bool, + originalSender: String? = nil, + isPrivate: Bool = false, + recipientNickname: String? = nil, + senderPeerID: PeerID? = nil, + mentions: [String]? = nil, + deliveryStatus: DeliveryStatus? = nil + ) { self.id = id ?? UUID().uuidString self.sender = sender self.content = content @@ -51,7 +63,7 @@ final class BitchatMessage: Codable { self.originalSender = originalSender self.isPrivate = isPrivate self.recipientNickname = recipientNickname - self.senderPeerID = PeerID(str: senderPeerID) + self.senderPeerID = senderPeerID self.mentions = mentions self.deliveryStatus = deliveryStatus ?? (isPrivate ? .sending : nil) } @@ -264,11 +276,11 @@ extension BitchatMessage { } } - var senderPeerID: String? + var senderPeerID: PeerID? if hasSenderPeerID && offset < dataCopy.count { let length = Int(dataCopy[offset]); offset += 1 if offset + length <= dataCopy.count { - senderPeerID = String(data: dataCopy[offset..= 2 { let hexByte = String(tempID.prefix(2)) if let byte = UInt8(hexByte, radix: 16) { diff --git a/bitchat/Models/PeerID.swift b/bitchat/Models/PeerID.swift index 20489fa3..b627ade1 100644 --- a/bitchat/Models/PeerID.swift +++ b/bitchat/Models/PeerID.swift @@ -69,8 +69,7 @@ extension PeerID { /// Convenience init to create PeerID by converting Data to String init?(data: Data) { - guard let str = String(data: data, encoding: .utf8) else { return nil } - self.init(str: str) + self.init(str: String(data: data, encoding: .utf8)) } /// Convenience init to "hide" hex-encoding implementation detail diff --git a/bitchat/Services/BLEService.swift b/bitchat/Services/BLEService.swift index 4accb7d0..7ccc3334 100644 --- a/bitchat/Services/BLEService.swift +++ b/bitchat/Services/BLEService.swift @@ -2791,7 +2791,7 @@ extension BLEService { let packet = BitchatPacket( type: MessageType.leave.rawValue, ttl: messageTTL, - senderID: myPeerID.id, + senderID: myPeerID, payload: Data(myNickname.utf8) ) broadcastPacket(packet) diff --git a/bitchat/ViewModels/ChatViewModel.swift b/bitchat/ViewModels/ChatViewModel.swift index 57ff483a..aa793bed 100644 --- a/bitchat/ViewModels/ChatViewModel.swift +++ b/bitchat/ViewModels/ChatViewModel.swift @@ -1009,7 +1009,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { content: content, timestamp: timestamp, isRelay: false, - senderPeerID: "nostr:\(event.pubkey.prefix(TransportConfig.nostrShortKeyDisplayLength))", + senderPeerID: PeerID(nostr: event.pubkey), mentions: mentions.isEmpty ? nil : mentions ) Task { @MainActor in @@ -1080,7 +1080,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { isRelay: false, isPrivate: true, recipientNickname: nickname, - senderPeerID: convKey, + senderPeerID: PeerID(str: convKey), deliveryStatus: .delivered(to: nickname, at: Date()) ) @@ -1424,7 +1424,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { content: trimmed, timestamp: Date(), isRelay: false, - senderPeerID: localSenderPeerID.id, + senderPeerID: localSenderPeerID, mentions: mentions.isEmpty ? nil : mentions ) @@ -1677,7 +1677,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { content: content, timestamp: min(rawTs, Date()), isRelay: false, - senderPeerID: "nostr:\(event.pubkey.prefix(TransportConfig.nostrShortKeyDisplayLength))", + senderPeerID: PeerID(nostr: event.pubkey), mentions: mentions.isEmpty ? nil : mentions ) @@ -1777,7 +1777,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { isRelay: false, isPrivate: true, recipientNickname: nickname, - senderPeerID: convKey, + senderPeerID: PeerID(str: convKey), deliveryStatus: .delivered(to: nickname, at: Date()) ) @@ -2127,7 +2127,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { content: content, timestamp: ts, isRelay: false, - senderPeerID: "nostr:\(event.pubkey.prefix(TransportConfig.nostrShortKeyDisplayLength))", + senderPeerID: PeerID(nostr: event.pubkey), mentions: mentions.isEmpty ? nil : mentions ) if !arr.contains(where: { $0.id == msg.id }) { @@ -2242,7 +2242,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: true, recipientNickname: recipientNickname, - senderPeerID: meshService.myPeerID.id, + senderPeerID: meshService.myPeerID, mentions: nil, deliveryStatus: .sending ) @@ -2299,7 +2299,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { isRelay: false, isPrivate: true, recipientNickname: nickname, - senderPeerID: meshService.myPeerID.id, + senderPeerID: meshService.myPeerID, deliveryStatus: .sending ) @@ -2397,7 +2397,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: true, recipientNickname: meshService.peerNickname(peerID: PeerID(str: peerID)), - senderPeerID: meshService.myPeerID.id + senderPeerID: meshService.myPeerID ) if privateChats[peerID] == nil { privateChats[peerID] = [] } privateChats[peerID]?.append(systemMessage) @@ -2506,7 +2506,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: message.originalSender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: message.senderPeerID == meshService.myPeerID ? meshService.myPeerID.id : peerID, // Update peer ID if it's from them + senderPeerID: message.senderPeerID == meshService.myPeerID ? meshService.myPeerID : PeerID(str: peerID), // Update peer ID if it's from them mentions: message.mentions, deliveryStatus: message.deliveryStatus ) @@ -2594,7 +2594,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: message.originalSender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: peerID, // Update to match current peer + senderPeerID: PeerID(str: peerID), // Update to match current peer mentions: message.mentions, deliveryStatus: message.deliveryStatus ) @@ -2887,7 +2887,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: true, recipientNickname: meshService.peerNickname(peerID: PeerID(str: peerID)), - senderPeerID: meshService.myPeerID.id + senderPeerID: meshService.myPeerID ) var chats = privateChats if chats[peerID] == nil { @@ -4450,7 +4450,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: true, recipientNickname: nickname, - senderPeerID: peerID, + senderPeerID: PeerID(str: peerID), mentions: pmMentions.isEmpty ? nil : pmMentions ) handlePrivateMessage(msg) @@ -4557,7 +4557,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: false, recipientNickname: nil, - senderPeerID: peerID, + senderPeerID: PeerID(str: peerID), mentions: publicMentions.isEmpty ? nil : publicMentions ) handlePublicMessage(msg) @@ -4658,7 +4658,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: msg.originalSender, isPrivate: msg.isPrivate, recipientNickname: msg.recipientNickname, - senderPeerID: msg.senderPeerID == meshService.myPeerID ? meshService.myPeerID.id : stableKeyHex, + senderPeerID: msg.senderPeerID == meshService.myPeerID ? meshService.myPeerID : PeerID(str: stableKeyHex), mentions: msg.mentions, deliveryStatus: msg.deliveryStatus ) @@ -5190,7 +5190,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { isRelay: false, isPrivate: true, recipientNickname: nickname, - senderPeerID: targetPeerID, + senderPeerID: PeerID(str: targetPeerID), deliveryStatus: .delivered(to: nickname, at: Date()) ) @@ -5530,7 +5530,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: nil, isPrivate: true, recipientNickname: nickname, - senderPeerID: tempPeerID, + senderPeerID: PeerID(str: tempPeerID), mentions: nil, deliveryStatus: .delivered(to: nickname, at: Date()) ) @@ -5693,7 +5693,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate { originalSender: message.originalSender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: message.senderPeerID?.id, + senderPeerID: message.senderPeerID, mentions: message.mentions, deliveryStatus: message.deliveryStatus ) diff --git a/bitchatTests/EndToEnd/PublicChatE2ETests.swift b/bitchatTests/EndToEnd/PublicChatE2ETests.swift index c88edb4d..b3a49a36 100644 --- a/bitchatTests/EndToEnd/PublicChatE2ETests.swift +++ b/bitchatTests/EndToEnd/PublicChatE2ETests.swift @@ -111,7 +111,7 @@ final class PublicChatE2ETests: XCTestCase { originalSender: message.sender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: message.senderPeerID?.id, + senderPeerID: message.senderPeerID, mentions: message.mentions ) @@ -450,7 +450,7 @@ final class PublicChatE2ETests: XCTestCase { originalSender: message.isRelay ? message.originalSender : message.sender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: message.senderPeerID?.id, + senderPeerID: message.senderPeerID, mentions: message.mentions ) diff --git a/bitchatTests/Integration/IntegrationTests.swift b/bitchatTests/Integration/IntegrationTests.swift index 43a32bfe..e0e9d18d 100644 --- a/bitchatTests/Integration/IntegrationTests.swift +++ b/bitchatTests/Integration/IntegrationTests.swift @@ -640,7 +640,7 @@ final class IntegrationTests: XCTestCase { originalSender: message.isRelay ? message.originalSender : message.sender, isPrivate: message.isPrivate, recipientNickname: message.recipientNickname, - senderPeerID: message.senderPeerID?.id, + senderPeerID: message.senderPeerID, mentions: message.mentions ) diff --git a/bitchatTests/Mocks/MockBLEService.swift b/bitchatTests/Mocks/MockBLEService.swift index eddb178f..6d564725 100644 --- a/bitchatTests/Mocks/MockBLEService.swift +++ b/bitchatTests/Mocks/MockBLEService.swift @@ -147,7 +147,7 @@ final class MockBLEService: NSObject { originalSender: nil, isPrivate: recipientID != nil, recipientNickname: nil, - senderPeerID: myPeerID, + senderPeerID: PeerID(str: myPeerID), mentions: mentions.isEmpty ? nil : mentions ) @@ -192,7 +192,7 @@ final class MockBLEService: NSObject { originalSender: nil, isPrivate: true, recipientNickname: recipientNickname, - senderPeerID: myPeerID, + senderPeerID: PeerID(str: myPeerID), mentions: nil ) diff --git a/bitchatTests/Protocol/BinaryProtocolTests.swift b/bitchatTests/Protocol/BinaryProtocolTests.swift index 084ead6b..eef9cbc2 100644 --- a/bitchatTests/Protocol/BinaryProtocolTests.swift +++ b/bitchatTests/Protocol/BinaryProtocolTests.swift @@ -251,7 +251,7 @@ final class BinaryProtocolTests: XCTestCase { originalSender: TestConstants.testNickname3, isPrivate: false, recipientNickname: nil, - senderPeerID: TestConstants.testPeerID1.id, + senderPeerID: TestConstants.testPeerID1, mentions: nil ) diff --git a/bitchatTests/TestUtilities/TestHelpers.swift b/bitchatTests/TestUtilities/TestHelpers.swift index 5c4e2377..c22fb871 100644 --- a/bitchatTests/TestUtilities/TestHelpers.swift +++ b/bitchatTests/TestUtilities/TestHelpers.swift @@ -44,7 +44,7 @@ final class TestHelpers { originalSender: nil, isPrivate: isPrivate, recipientNickname: recipientNickname, - senderPeerID: senderPeerID.id, + senderPeerID: senderPeerID, mentions: mentions ) }