Refactor: BitchatMessage (#610)

* Extract BitchatMessage into a separate file

* Convert `fromBinaryPayload` to `convenience init?`

* Extract message dedup into an extension

* Remove dead `formatMessageContent`

* Minor refactor of timestamp and username formatting

* Remove dead `getSenderColor`
This commit is contained in:
Islam
2025-09-15 15:00:12 +02:00
committed by GitHub
parent 347ce5ece4
commit ea8d51a36b
11 changed files with 392 additions and 449 deletions
+1 -1
View File
@@ -215,7 +215,7 @@ final class BLEServiceTests: XCTestCase {
let expectation = XCTestExpectation(description: "Delivery handler called")
service.packetDeliveryHandler = { packet in
if let msg = BitchatMessage.fromBinaryPayload(packet.payload) {
if let msg = BitchatMessage(packet.payload) {
XCTAssertEqual(msg.content, "Test delivery")
expectation.fulfill()
}
@@ -139,7 +139,7 @@ final class PrivateChatE2ETests: XCTestCase {
alice.packetDeliveryHandler = { packet in
// Encrypt outgoing private messages
if packet.type == 0x01,
let message = BitchatMessage.fromBinaryPayload(packet.payload),
let message = BitchatMessage(packet.payload),
message.isPrivate {
do {
let encrypted = try aliceManager.encrypt(packet.payload, for: TestConstants.testPeerID2)
@@ -164,7 +164,7 @@ final class PrivateChatE2ETests: XCTestCase {
if packet.type == 0x02 {
do {
let decrypted = try bobManager.decrypt(packet.payload, from: TestConstants.testPeerID1)
if let message = BitchatMessage.fromBinaryPayload(decrypted) {
if let message = BitchatMessage(decrypted) {
XCTAssertEqual(message.content, TestConstants.testMessage1)
XCTAssertTrue(message.isPrivate)
expectation.fulfill()
@@ -98,7 +98,7 @@ final class PublicChatE2ETests: XCTestCase {
// Set up relay in Bob
bob.packetDeliveryHandler = { packet in
// Bob should relay to Charlie
if let message = BitchatMessage.fromBinaryPayload(packet.payload),
if let message = BitchatMessage(packet.payload),
message.sender == TestConstants.testNickname1 {
// Create relay message
@@ -437,7 +437,7 @@ final class PublicChatE2ETests: XCTestCase {
// Check if should relay
guard packet.ttl > 1 else { return }
if let message = BitchatMessage.fromBinaryPayload(packet.payload) {
if let message = BitchatMessage(packet.payload) {
// Don't relay own messages
guard message.senderPeerID != node.peerID else { return }
@@ -531,7 +531,7 @@ final class IntegrationTests: XCTestCase {
// Setup encryption at Alice
nodes["Alice"]!.packetDeliveryHandler = { packet in
if packet.type == 0x01,
let message = BitchatMessage.fromBinaryPayload(packet.payload),
let message = BitchatMessage(packet.payload),
message.isPrivate && packet.recipientID != nil {
// Encrypt private messages
if let encrypted = try? self.noiseManagers["Alice"]!.encrypt(packet.payload, for: TestConstants.testPeerID2) {
@@ -553,7 +553,7 @@ final class IntegrationTests: XCTestCase {
nodes["Bob"]!.packetDeliveryHandler = { packet in
if packet.type == 0x02 {
if let decrypted = try? self.noiseManagers["Bob"]!.decrypt(packet.payload, from: TestConstants.testPeerID1),
let message = BitchatMessage.fromBinaryPayload(decrypted) {
let message = BitchatMessage(decrypted) {
bobDecrypted = message.content == "Secret message"
expectation.fulfill()
}
@@ -626,7 +626,7 @@ final class IntegrationTests: XCTestCase {
node.packetDeliveryHandler = { packet in
guard packet.ttl > 1 else { return }
if let message = BitchatMessage.fromBinaryPayload(packet.payload) {
if let message = BitchatMessage(packet.payload) {
guard message.senderPeerID != node.peerID else { return }
let relayMessage = BitchatMessage(
+1 -1
View File
@@ -309,7 +309,7 @@ final class MockBLEService: NSObject {
func simulateIncomingPacket(_ packet: BitchatPacket) {
// Process through the actual handling logic
if let message = BitchatMessage.fromBinaryPayload(packet.payload) {
if let message = BitchatMessage(packet.payload) {
var shouldDeliver = false
seenLock.lock()
if !seenMessageIDs.contains(message.id) {
@@ -197,7 +197,7 @@ final class BinaryProtocolTests: XCTestCase {
return
}
guard let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
guard let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to decode message from binary")
return
}
@@ -219,7 +219,7 @@ final class BinaryProtocolTests: XCTestCase {
)
guard let payload = message.toBinaryPayload(),
let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to encode/decode private message")
return
}
@@ -233,7 +233,7 @@ final class BinaryProtocolTests: XCTestCase {
let message = TestHelpers.createTestMessage(mentions: mentions)
guard let payload = message.toBinaryPayload(),
let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to encode/decode message with mentions")
return
}
@@ -256,7 +256,7 @@ final class BinaryProtocolTests: XCTestCase {
)
guard let payload = message.toBinaryPayload(),
let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to encode/decode relay message")
return
}
@@ -294,7 +294,7 @@ final class BinaryProtocolTests: XCTestCase {
let message = TestHelpers.createTestMessage(content: largeContent)
guard let payload = message.toBinaryPayload(),
let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to handle large message")
return
}
@@ -307,7 +307,7 @@ final class BinaryProtocolTests: XCTestCase {
let emptyMessage = TestHelpers.createTestMessage(content: "")
guard let payload = emptyMessage.toBinaryPayload(),
let decodedMessage = BitchatMessage.fromBinaryPayload(payload) else {
let decodedMessage = BitchatMessage(payload) else {
XCTFail("Failed to handle empty message")
return
}