diff --git a/Package.resolved b/Package.resolved index 074675b4..52faefaf 100644 --- a/Package.resolved +++ b/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/xmtp/libxmtp-swift", "state" : { - "branch" : "92274fe", - "revision" : "92274fe0dde1fc7f8f716ebcffa3d252813be56d" + "branch" : "503086d", + "revision" : "503086d91ba6c3420aca118e4812d62a004d17ee" } }, { diff --git a/Sources/XMTPiOS/ConversationV2.swift b/Sources/XMTPiOS/ConversationV2.swift index 6274dd78..d150506e 100644 --- a/Sources/XMTPiOS/ConversationV2.swift +++ b/Sources/XMTPiOS/ConversationV2.swift @@ -78,14 +78,11 @@ public struct ConversationV2 { } func prepareMessage(encodedContent: EncodedContent, options: SendOptions?) async throws -> PreparedMessage { - let codec = client.codecRegistry.find(for: options?.contentType) - let message = try await MessageV2.encode( client: client, content: encodedContent, topic: topic, - keyMaterial: keyMaterial, - codec: codec + keyMaterial: keyMaterial ) let topic = options?.ephemeral == true ? ephemeralTopic : topic @@ -122,6 +119,18 @@ public struct ConversationV2 { if let compression = options?.compression { encoded = try encoded.compress(compression) } + + func shouldPush(codec: Codec, content: Any) throws -> Bool? { + if let content = content as? Codec.T { + return try codec.shouldPush(content: content) + } else { + throw CodecError.invalidContent + } + } + + if let shouldPush = try shouldPush(codec: codec, content: content) { + encoded.shouldPush = shouldPush + } return try await prepareMessage(encodedContent: encoded, options: options) } @@ -236,8 +245,7 @@ public struct ConversationV2 { client: client, content: content, topic: topic, - keyMaterial: keyMaterial, - codec: codec + keyMaterial: keyMaterial ) let envelope = Envelope( diff --git a/Sources/XMTPiOS/Messages/MessageV2.swift b/Sources/XMTPiOS/Messages/MessageV2.swift index 5978caf9..d6499c58 100644 --- a/Sources/XMTPiOS/Messages/MessageV2.swift +++ b/Sources/XMTPiOS/Messages/MessageV2.swift @@ -80,9 +80,10 @@ extension MessageV2 { } } - static func encode(client: Client, content encodedContent: EncodedContent, topic: String, keyMaterial: Data, codec: Codec) async throws -> MessageV2 { + static func encode(client: Client, content encodedContent: EncodedContent, topic: String, keyMaterial: Data) async throws -> MessageV2 { let payload = try encodedContent.serializedData() - + let shouldPush = encodedContent.shouldPush + let date = Date() let header = MessageHeaderV2(topic: topic, created: date) let headerBytes = try header.serializedData() @@ -106,9 +107,6 @@ extension MessageV2 { let senderHmac = try Crypto.generateHmacSignature(secret: keyMaterial, info: infoEncoded, message: headerBytes) - let decoded = try codec.decode(content: encodedContent, client: client) - let shouldPush = try codec.shouldPush(content: decoded) - return MessageV2( headerBytes: headerBytes, diff --git a/Sources/XMTPiOS/Proto/message_contents/content.pb.swift b/Sources/XMTPiOS/Proto/message_contents/content.pb.swift index 6f588dd0..18049f3b 100644 --- a/Sources/XMTPiOS/Proto/message_contents/content.pb.swift +++ b/Sources/XMTPiOS/Proto/message_contents/content.pb.swift @@ -132,6 +132,9 @@ public struct Xmtp_MessageContents_EncodedContent { /// encoded content itself public var content: Data = Data() + + /// Indicates whether the content should be pushed. + public var shouldPush: Bool = false public var unknownFields = SwiftProtobuf.UnknownStorage() @@ -256,6 +259,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto 3: .same(proto: "fallback"), 5: .same(proto: "compression"), 4: .same(proto: "content"), + 6: .same(proto: "shouldPush"), ] public mutating func decodeMessage(decoder: inout D) throws { @@ -269,6 +273,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto case 3: try { try decoder.decodeSingularStringField(value: &self._fallback) }() case 4: try { try decoder.decodeSingularBytesField(value: &self.content) }() case 5: try { try decoder.decodeSingularEnumField(value: &self._compression) }() + case 6: try { try decoder.decodeSingularBoolField(value: &self.shouldPush) }() default: break } } @@ -294,7 +299,10 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto try { if let v = self._compression { try visitor.visitSingularEnumField(value: v, fieldNumber: 5) } }() - try unknownFields.traverse(visitor: &visitor) + if self.shouldPush != false { + try visitor.visitSingularBoolField(value: self.shouldPush, fieldNumber: 6) + } + try unknownFields.traverse(visitor: &visitor) } public static func ==(lhs: Xmtp_MessageContents_EncodedContent, rhs: Xmtp_MessageContents_EncodedContent) -> Bool { @@ -303,6 +311,7 @@ extension Xmtp_MessageContents_EncodedContent: SwiftProtobuf.Message, SwiftProto if lhs._fallback != rhs._fallback {return false} if lhs._compression != rhs._compression {return false} if lhs.content != rhs.content {return false} + if lhs.shouldPush != rhs.shouldPush {return false} if lhs.unknownFields != rhs.unknownFields {return false} return true } diff --git a/Tests/XMTPTests/CodecTests.swift b/Tests/XMTPTests/CodecTests.swift index 08ce1300..497f991d 100644 --- a/Tests/XMTPTests/CodecTests.swift +++ b/Tests/XMTPTests/CodecTests.swift @@ -96,8 +96,7 @@ class CodecTests: XCTestCase { client: aliceClient, content: messages[0].encodedContent, topic: aliceConversation.topic, - keyMaterial: Data(aliceConversation.keyMaterial!), - codec: NumberCodec() + keyMaterial: Data(aliceConversation.keyMaterial!) ) XCTAssertEqual(false, message.shouldPush) diff --git a/Tests/XMTPTests/ConversationTests.swift b/Tests/XMTPTests/ConversationTests.swift index 07e8b13f..ca920eba 100644 --- a/Tests/XMTPTests/ConversationTests.swift +++ b/Tests/XMTPTests/ConversationTests.swift @@ -230,8 +230,7 @@ class ConversationTests: XCTestCase { client: bobClient, content: encodedContent, topic: conversation.topic, - keyMaterial: conversation.keyMaterial, - codec: encoder + keyMaterial: conversation.keyMaterial ) ).serializedData() ) diff --git a/Tests/XMTPTests/MessageTests.swift b/Tests/XMTPTests/MessageTests.swift index b49a08a7..cad1ddcd 100644 --- a/Tests/XMTPTests/MessageTests.swift +++ b/Tests/XMTPTests/MessageTests.swift @@ -63,7 +63,7 @@ class MessageTests: XCTestCase { let sealedInvitation = try SealedInvitation.createV1(sender: alice.toV2(), recipient: bob.toV2().getPublicKeyBundle(), created: Date(), invitation: invitationv1) let encoder = TextCodec() let encodedContent = try encoder.encode(content: "Yo!", client: client) - let message1 = try await MessageV2.encode(client: client, content: encodedContent, topic: invitationv1.topic, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial, codec: encoder) + let message1 = try await MessageV2.encode(client: client, content: encodedContent, topic: invitationv1.topic, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial) let decoded = try MessageV2.decode("", "", message1, keyMaterial: invitationv1.aes256GcmHkdfSha256.keyMaterial, client: client) let result: String = try decoded.content()